#include using namespace std; using ll = long long; // #define int ll using PII = pair; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() template T &chmin(T &a, const T &b) { return a = min(a, b); } template T &chmax(T &a, const T &b) { return a = max(a, b); } template bool IN(T a, T b, T x) { return a<=x&&x T ceil(T a, T b) { return a/b + !!(a%b); } template vector make_v(size_t a) { return vector(a); } template auto make_v(size_t a,Ts... ts) { return vector(ts...))>(a,make_v(ts...)); } template typename enable_if::value==0>::type fill_v(T &t, const V &v) { t=v; } template typename enable_if::value!=0>::type fill_v(T &t, const V &v ) { for(auto &e:t) fill_v(e,v); } template ostream &operator <<(ostream& out,const pair& a) { out<<'('< ostream &operator <<(ostream& out,const vector& a){ out<<'['; for(const T &i: a) out< ostream &operator <<(ostream& out, const set& a) { out<<'{'; for(const T &i: a) out< ostream &operator <<(ostream& out, const map& a) { out<<'{'; for(auto &i: a) out< struct modint { ll x; modint(): x(0) {} modint(ll y) : x(y>=0 ? y%MOD : y%MOD+MOD) {} static constexpr ll mod() { return MOD; } // e乗 modint pow(ll e) { ll a = 1, p = x; while(e > 0) { if(e%2 == 0) {p = (p*p) % MOD; e /= 2;} else {a = (a*p) % MOD; e--;} } return modint(a); } modint inv() const { ll a=x, b=MOD, u=1, y=1, v=0, z=0; while(a) { ll q = b/a; swap(z -= q*u, u); swap(y -= q*v, v); swap(b -= q*a, a); } return z; } // Comparators bool operator <(modint b) { return x < b.x; } bool operator >(modint b) { return x > b.x; } bool operator<=(modint b) { return x <= b.x; } bool operator>=(modint b) { return x >= b.x; } bool operator!=(modint b) { return x != b.x; } bool operator==(modint b) { return x == b.x; } // Basic Operations modint operator+(modint r) const { return modint(*this) += r; } modint operator-(modint r) const { return modint(*this) -= r; } modint operator*(modint r) const { return modint(*this) *= r; } modint operator/(modint r) const { return modint(*this) /= r; } modint &operator+=(modint r) { if((x += r.x) >= MOD) x -= MOD; return *this; } modint &operator-=(modint r) { if((x -= r.x) < 0) x += MOD; return *this; } modint &operator*=(modint r) { #if !defined(_WIN32) || defined(_WIN64) x = x * r.x % MOD; return *this; #endif unsigned long long y = x * r.x; unsigned xh = (unsigned) (y >> 32), xl = (unsigned) y, d, m; asm( "divl %4; \n\t" : "=a" (d), "=d" (m) : "d" (xh), "a" (xl), "r" (MOD) ); x = m; return *this; } modint &operator/=(modint r) { return *this *= r.inv(); } // increment, decrement modint operator++() { x++; return *this; } modint operator++(signed) { modint t = *this; x++; return t; } modint operator--() { x--; return *this; } modint operator--(signed) { modint t = *this; x--; return t; } }; using mint = modint<1000000007>; template mint operator*(T l, mint r) { return mint(l) *= r; } template mint operator+(T l, mint r) { return mint(l) += r; } template mint operator-(T l, mint r) { return mint(l) -= r; } template mint operator/(T l, mint r) { return mint(l) /= r; } template bool operator==(T l, mint r) { return mint(l) == r; } template bool operator!=(T l, mint r) { return mint(l) != r; } // Input/Output ostream &operator<<(ostream& os, mint a) { return os << a.x; } istream &operator>>(istream& is, mint &a) { return is >> a.x; } string to_frac(mint v) { static map mp; if(mp.empty()) { mp[0] = mp[mint::mod()] = {0, 1}; FOR(i, 2, 1001) FOR(j, 1, i) if(__gcd(i, j) == 1) { mp[(mint(i) / j).x] = {i, j}; } } auto itr = mp.lower_bound(v.x); if(itr != mp.begin() && v.x - prev(itr)->first < itr->first - v.x) --itr; string ret = to_string(itr->second.first + itr->second.second * ((int)v.x - itr->first)); if(itr->second.second > 1) { ret += '/'; ret += to_string(itr->second.second); } return ret; } // x座標が相異なるn+1点(x_i,y_i)を通るn次以下の多項式f(T)の値を返す // x_i = a + i*d 0<=i<=n (等差数列) // 0割りを起こさないようにTが小さいときに注意 // O(nlog(MOD)) mint lagrange_interpolation_arithmetic(mint a, mint d, vector y, mint T) { const ll n = y.size() - 1; mint ret = 0, ft = 1; REP(i, n+1) ft *= T-(a+d*i); // f_0(x_0) mint f = 1; FOR(i, 1, n+1) f *= -1*i*d; ret += y[0] / f * ft / (T-a); // f_i(x_i) → f_{i+1}(x_{i+1}) REP(i, n) { f *= d*(i+1) / (d*(i-n)); ret += y[i+1] / f * ft / (T-a-d*(i+1)); } return ret; } signed main(void) { cin.tie(0); ios::sync_with_stdio(false); ll n, k; cin >> n >> k; vector y(k+2); y[0] = 0; FOR(i, 1, k+2) y[i] = y[i-1] + mint(i).pow(k); if(n <= k+1) { cout << y[n] << endl; } else { cout << lagrange_interpolation_arithmetic(0, 1, y, n) << endl; } return 0; }