Function to calculate and return x ^ n in log ( n ) time using Binary Exponentiation
function binExp ( base , power ) { var x = 1 ; while ( power != 0 ) { if ( power % 2 == 1 ) x = ( ( ( x % mod ) * ( base % mod ) ) % mod ) ; base = ( ( ( base % mod ) * ( base % mod ) ) % mod ) ; power = parseInt ( power / 2 ) ; } return x ; }