Calculate nCr mod p.

How to compute nCr % p for large data Efficiently?

Read here @rashedcs - Best known algos for calculating nCr % M

Precompute the factorial till n. Then you can find n!,r! and (n-r)! (all mod p) in order 1.

The property further useful is (ab)mod p = ((a mod p)(b mod p))mod p.

Then using inverse modulo find the result.To read more about inverse modulo refer link : http://mathworld.wolfram.com/ModularInverse.html

You can check this out for the various implementations of cominations when mod is prime or any general number.

//