def fast_modular_multiply(g, k, p):
    """Calculates the value g^k mod p
    
    Args:
        g: an integer representing the base
        k: an integer representing the exponent
        p: an integer (prime number)
        
    Returns:
        an integer representing g^k mod p

    """
    u = g
    y = 1
    while k != 0:
        if k % 2 == 1:
            y = y * u % p
        u = u * u % p
        k //= 2
    return y