P, K = map(int,input().split()) MOD = 10**9+7 z = 1 nz = 0 for k in range(K): z, nz = z*(P+1) + 2*nz, (z+2*nz)*(P-1) z %= MOD nz %= MOD print(z)