def fast_power(a, b, n):
    """
    Calculate the a^n % b

    :param a: a 32-bit integer
    :type a: int
    :param b: a 32-bit integer
    :type b: int
    :param n: a 32-bit integer
    :type n: int
    :return: result
    :rtype: int
    """
    # basic cases
    if n == 0:
        return 1 % b
    elif n == 1:
        return a % b
    elif n < 0:
        return -1

    # (a * c) % b = ((a % b) * (c % b)) % b
    product = fast_power(a, b, n // 2)
    product = (product * product) % b
    if n % 2 == 1:
        product = (product * a) % b

    return product