import sys readline = sys.stdin.readline P, K = map(int, input().split()) mod = 10**9 + 7 dp = [1, 0] for _ in range(K): next_dp = [0, 0] next_dp[0] += dp[0] + dp[0] * P next_dp[1] += dp[0] * (P-1) next_dp[0] += dp[1] * 2 next_dp[1] += dp[1] * (P-1) * 2 dp = next_dp dp[0] %= mod dp[1] %= mod print(dp[0])