def main0(p,k): # 自明DP mod=10**9+7 dp=[0]*p dp[0]=1 for _ in range(k): ndp=[0]*p for j in range(p): # dp[j]からの遷移 if dp[j]==0:continue for i in range(p): # (1) ndp[(j+i)%p]+=dp[j] ndp[(j+i)%p]%=mod # (2) ndp[(j*i)%p]+=dp[j] ndp[(j*i)%p]%=mod dp=ndp return dp[0] def main1(p,k): mod=10**9+7 # dp[i]:N=iとなる操作列の個数 # 初期値dp[0]=0 dp0=1 # dp[0]の値 dp1=0 # sum(dp)-dp[0] for _ in range(k): ndp0=dp1+dp0 # (1) ndp0+=dp0*p%mod+dp1 # (2) ndp1=(dp0+dp1)*(p-1)%mod # (1) ndp1+=dp1*(p-1) # (2) dp0=ndp0%mod dp1=ndp1%mod return dp0 if __name__=='__main__': p,k=map(int,input().split()) #ret0=main0(p,k) #print(ret0) ret1=main1(p,k) print(ret1)