def mod_exp(b: int, a: int, n: int) -> int:
    """Fast Modular Exponentiation:
    Raises 'b' to the 'a' power modulo 'n'.  
    This implementation has a computational complexity of O(log2(a))."""    
    res = 1
    for i in range(a.bit_length()):
        if bool((1<<i) & a): res = (res * b) % n
        b = (b**2) % n
    return res