There are well-known formulas: , , . Also mathematicians found similar formulas for higher degrees.
Find the value of the sum modulo 109 + 7 (so you should find the remainder after dividing the answer by the value 109 + 7).
Input
The only line contains two integers n, k (1 ≤ n ≤ 109, 0 ≤ k ≤ 106).
Output
Print the only integer a — the remainder after dividing the value of the sum by the value 109 + 7.
Examples
Input
4 1
Output
10
Input
4 2
Output
30
Input
4 3
Output
100
Input
4 0
Output
4
设f(x)为1到x的k次方和,(高中数学讲过k次方和可以由k-1次方和推出来)所以f(x)最高次为k+1,所以需要求出来k+2个数字然后拉格朗日插值即可
#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define maxn 1000005
using namespace std;
ll f[maxn],mul[maxn];
long long power(long long a,long long b)
{
long long ans=1;
while(b)
{
if(b&1)
{
ans=(ans*a)%mod;
b--;
}
b/=2;
a=a*a%mod;
}
return ans;
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
f[0]=0;
for(int i=1;i<=k+2;i++)
f[i]=(f[i-1]+power(i,k))%mod;
if(n<=k+2)
{
printf("%lld",f[n]);
return 0;
}
mul[0]=1;
for(int i=1;i<=k+2;i++)
mul[i]=mul[i-1]*i%mod;
ll t=1;
for(int i=1;i<=k+2;i++)
t=(n-i)*t%mod;
ll ans=0;
for(int i=1;i<=k+2;i++)
{
ll tmp=power(mul[i-1]*mul[k+2-i]%mod*(n-i)%mod,mod-2);
if((k+2-i)&1)
tmp=-tmp;
ans=(ans+f[i]*t%mod*tmp%mod+mod)%mod;
}
printf("%lld\n",ans);
}