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