P,K=map(int,input().split()) mod=10**9+7 dp0,dp1=1,0 for k in range(K): dp0,dp1=(dp0+dp1+dp0*P+dp1)%mod,((dp0+dp1)*(P-1)+dp1*(P-1))%mod ans=dp0 print(ans)