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