(a: int, n: int, r: int = None)
return ans
Iterative version of binary exponentiation
    
    Calculate a ^ n
    if r is specified, return the result modulo r
    
    Time Complexity :  O(log(n))
    Space Complexity : O(1)
Iterative version of binary exponentiation
    
    Calculate a ^ n
    if r is specified, return the result modulo r
    
    Time Complexity :  O(log(n))
    Space Complexity : O(1)
Iterative version of binary exponentiation Calculate a ^ n if r is specified return the result modulo r Time Complexity : O ( log ( n )) Space Complexity : O ( 1 )
def power(a: int, n: int, r: int = None):
    """
    Iterative version of binary exponentiation
    
    Calculate a ^ n
    if r is specified, return the result modulo r
    
    Time Complexity :  O(log(n))
    Space Complexity : O(1)
    """
    ans = 1
    while n:
        if n & 1:
            ans = ans * a
        a = a * a
        if r:
            ans %= r
            a %= r
        n >>= 1
    return ans
def power ( a : int , n : int , r : int = None ) : ans = 1 while n : if n & 1 : ans = ans * a a = a * a if r : ans %= r a %= r n >>= 1 return ans