from typing import Optional


def hyper_exponentiation(a: int, k: int, m: Optional[int] = None):
    """
    Compute a↑↑k mod m

    Where a↑↑k is defined recursively as
        1) a↑↑1 = a
        2) a↑↑(k+1) = a^(a↑↑k)
    """
    previous = 0
    current = 1
    while k >= 1:
        current = pow(a, current, m)
        if current == previous:  # if repeating then will repeat forever
            break
        previous = current
        k -= 1
    return current