Fill factorial array so that we can find all factorial of r , n and n - r
var fac = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; ; fac [ 0 ] = 1 ; for ( var i = 1 ; i <= n ; i ++ ) fac [ i ] = fac [ i - 1 ] * i % p ; return ( fac [ parseInt ( n ) ] * modInverse ( fac [ parseInt ( r ) ] , p ) % p * modInverse ( fac [ parseInt ( n ) - parseInt ( r ) ] , p ) % p ) % p ; }