Function to calculate ( x ^ y ) % p in O ( log y )
function power ( x , y , p ) { var result = 1 ; while ( y > 0 ) { if ( y & 1 == 1 ) result = ( result * x ) % p ; x = ( x * x ) % p ; y >>= 1 ; } return result ; }