Function to calculate the factorial of a number
static long fact ( long num ) { long result = 1 ; for ( long i = 1 ; i <= num ; ++ i ) { result = ( ( result % mod ) * ( i % mod ) ) % mod ; } return result ; }