I'm looking for a way to find $n! \pmod{p}$ efficiently. I did it with simple for loop and moduling as we add the number
result = (result*i)%p
But $n$ can be up to $10^{11}$, so this is too slow. I was wondering if there is anyway to do this that doesn't require a loop. P is prime
Strictly speaking, this isn't an answer, but let me explain why I think there isn't a fast method for that: Imagine we have an algorithm computing $r(n,m)=n! \% m$ quickly, for $n\ge\sqrt{m}$. If $m$ is composite, it has a factor $f\le\sqrt{m}$, and that factor would divide $n!$, and thus $r(n,m)$, and the gcd of $m$ and $r(n,m)$, i.e. we'd have a fast algorithm for factoring $m$.
This isn't strict, because it doesn't exlude the existence of an algorithm working only for prime $m$, and failing (without indicating a factor) otherwise.