def fast_pow_iterative(x, n, mod=None):
    """
    This method divides the power problem into sub-problems of size n/2
    and compute the sub-problems iteratively.

    - Time Complexity: O(log n)
    - Space Complexity: O(1)
    - Algorithmic Paradigm: Divide and conquer.

    :param x: given number
    :type x: int
    :param n: given exponential
    :type n: int
    :param mod: large number to avoid overflow
    :type mod: int
    :return: result of pow
    :rtype: int
    """
    if mod is not None:
        res = 1 % mod
        while n > 0:
            if (n & 1) != 0:
                res *= x % mod
            x *= x % mod
            n >>= 1
    else:
        # in python 3, since there is no longer a limit to
        # the value of integers, we don't need the `mod` argument
        res = 1
        while n > 0:
            if (n & 1) != 0:
                res *= x
            x *= x
            n >>= 1
    return res