Fill factorial array so that we can find all factorial of r , n and n - r
long [ ] fac = new long [ ( int ) n + 1 ] ; fac [ 0 ] = 1 ; int i ; for ( i = 1 ; i <= n ; i ++ ) fac [ i ] = fac [ i - 1 ] * i % p ; return ( fac [ ( int ) n ] * modInverse ( fac [ r ] , p ) % p * modInverse ( fac [ ( int ) n - r ] , p ) % p ) % p ; }