I was looking at some code here - https://www.codechef.com/viewsolution/6075682 when I came across this statement to calculate reciprocal of a factorial-
invFact(n) = fact(n)^(mod-2)
where mod is modulo$(10^9+7)$. The return value has to be modulo$(10^9+7)$ as well. I don't get the mathematical logic behind this formula. Can somebody guide me? Note: invFact(n) = 1/n! here.
I found that this is the consequence of Fermat's Little theorem.
Fermat's little theorem states that if p is a prime number, then for any integer a, the number a p − a is an integer multiple of p. In the notation of modular arithmetic, this is expressed as
This leads to: