MOD = 10**9+7 P,K = list(map(int,input().split())) now = [1,0] for _ in range(K): next = [0,0] next[0] += now[0] + now[0] * P + now[1] + now[1] next[0] %= MOD next[1] += now[0] * (P - 1) + now[1] * (P - 1) + now[1] * (P - 1) next[1] %= MOD now,next = next,now print(now[0])