(a: int, n: int, r: int = None)
return ans
Recursive version of binary exponentiation
    
    Calculate a ^ n
    if r is specified, return the result modulo r
    
    Time Complexity :  O(log(n))
    Space Complexity : O(log(n))
Recursive version of binary exponentiation
    
    Calculate a ^ n
    if r is specified, return the result modulo r
    
    Time Complexity :  O(log(n))
    Space Complexity : O(log(n))
Recursive version of binary exponentiation Calculate a ^ n if r is specified return the result modulo r Time Complexity : O ( log ( n )) Space Complexity : O ( log ( n ))
def power_recur(a: int, n: int, r: int = None):
    """
    Recursive version of binary exponentiation
    
    Calculate a ^ n
    if r is specified, return the result modulo r
    
    Time Complexity :  O(log(n))
    Space Complexity : O(log(n))
    """
    if n == 0:
        ans = 1
    elif n == 1:
        ans = a
    else:
        ans = power_recur(a, n // 2, r)
        ans = ans * ans
        if n % 2:
            ans = ans * a
    if r:
        ans %= r
    return ans
def power_recur ( a : int , n : int , r : int = None ) : if n == 0 : ans = 1 elif n == 1 : ans = a else : ans = power_recur ( a , n // 2 , r ) ans = ans * ans if n % 2 : ans = ans * a if r : ans %= r return ans