mod = 10 ** 9 + 7 p, K = map(int, input().split()) zero = 1 other = 0 # 1, 2, ... P-1それぞれの状態数(ぜんぶいっしょ) for _ in range(K): n0 = 0 n1 = 0 # mult 0 n0 += zero + (p - 1) * other # mult 1,2,..p-1 n0 += zero * (p - 1) n1 += other * (p - 1) # add n0 += zero + (p - 1) * other n1 += zero + (p - 1) * other zero = n0 % mod other = n1 % mod print(zero)