def fast_exponentiation(base, exp, n):
    """
        Iteratively finds the result of the expression (base**exp) mod n
    """

    bin_exp = bin(exp)[2:]
    output = 1
    for i in bin_exp:
        output = (output ** 2) % n
        if i == "1":
            output = (output*base) % n
    return output