Formula for reciprocal of a factorial

1.2k Views Asked by At

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.

1

There are 1 best solutions below

0
On

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

a^p = a (mod p)

This leads to:

a^(p-2) = (1/a) (mod p)