Recursive function to return ( x ^ n ) % m
def modexp ( x , n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif ( n % 2 == 0 ) : NEW_LINE INDENT return modexp ( ( x * x ) % m , n // 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( x * modexp ( ( x * x ) % m , ( n - 1 ) / 2 ) % m ) NEW_LINE DEDENT DEDENT