P, K = map(int, input().split()) mod = 10**9+7 # dp:K回目の処理後にN=0 or N != 0 dp = [[0]*2 for _ in range(K+1)] dp[0][0] = 1 for i in range(1, K+1): dp[i][1] = dp[i-1][1]*(2*P-2) + dp[i-1][0]*(P-1) dp[i][0] = dp[i-1][1]*2 + dp[i-1][0]*(P+1) dp[i][0] %= mod dp[i][1] %= mod print(dp[-1][0])