#include "bits/stdc++.h" using namespace std; #ifdef _DEBUG #include "dump.hpp" #else #define dump(...) #endif //#define int long long #define rep(i,a,b) for(int i=(a);i<(b);i++) #define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--) #define all(c) begin(c),end(c) const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = 1'000'000'007; template bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } template struct ModInt { static const int kMod = MOD; unsigned x; ModInt() :x(0) {} ModInt(signed x_) { x_ %= MOD; if (x_ < 0)x_ += MOD; x = x_; } ModInt(signed long long x_) { x_ %= MOD; if (x_ < 0)x_ += MOD; x = x_; } int get()const { return (int)x; } ModInt &operator+=(ModInt m) { if ((x += m.x) >= MOD)x -= MOD; return *this; } ModInt &operator-=(ModInt m) { if ((x += MOD - m.x) >= MOD)x -= MOD; return *this; } ModInt &operator*=(ModInt m) { x = (unsigned long long)x*m.x%MOD; return *this; } ModInt &operator/=(ModInt m) { return *this *= m.inverse(); } ModInt operator+(ModInt m)const { return ModInt(*this) += m; } ModInt operator-(ModInt m)const { return ModInt(*this) -= m; } ModInt operator*(ModInt m)const { return ModInt(*this) *= m; } ModInt operator/(ModInt m)const { return ModInt(*this) /= m; } ModInt operator-()const { return ModInt(MOD - (signed)x); } bool operator==(ModInt m)const { return x == m.x; } bool operator!=(ModInt m)const { return x != m.x; } ModInt inverse()const { signed a = x, b = MOD, u = 1, v = 0; while (b) { signed t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } if (u < 0)u += MOD; return ModInt(u); } }; template ostream &operator<<(ostream &os, const ModInt &m) { return os << m.x; } template istream &operator>>(istream &is, ModInt &m) { signed long long s; is >> s; m = ModInt(s); return is; }; template ModInt pow(ModInt a, unsigned long long k) { ModInt r = 1; while (k) { if (k & 1)r *= a; a *= a; k >>= 1; } return r; } using mint = ModInt; // n < 10^7 // 前計算 O(n) // 計算 O(1) // Verified: https://yukicoder.me/submissions/330366 vector fact, factinv, inv; void precompute(int n) { int m = fact.size(); if (n < m)return; n = min(n, mint::kMod - 1); // N >= kMod => N! = 0 (mod kMod) fact.resize(n + 1); factinv.resize(n + 1); inv.resize(n + 1); if (m == 0) { fact[0] = 1; m = 1; } for (int i = m; i <= n; i++) fact[i] = fact[i - 1] * i; factinv[n] = fact[n].inverse(); for (int i = n; i >= m; i--) factinv[i - 1] = factinv[i] * i; // ((i-1)!)^(-1) = (i!)^(-1) * i for (int i = m; i <= n; i++) inv[i] = factinv[i] * fact[i - 1]; } mint C(int n, int k) { if (k < 0 || n < k)return 0; // Lucas's theorem if (n >= mint::kMod) return C(n % mint::kMod, k % mint::kMod) * C(n / mint::kMod, k / mint::kMod); precompute(n); return k > n ? 0 : fact[n] * factinv[n - k] * factinv[k]; } mint P(int n, int k) { if (k < 0 || n < k)return 0; precompute(n); return k > n ? 0 : fact[n] * factinv[n - k]; } mint H(int n, int k) { if (n == 0 && k == 0)return 1; // H(0,0) = 1 != C(-1,0) = 0 return C(n + k - 1, k); } // ベルヌーイ数 B^- // O(n^2) vector bernoulliNumbers(int n) { vector B(n + 1); B[0] = 1; precompute(n + 1); for (int i = 1; i <= n; i++) { for (int k = 0; k <= i - 1; k++) B[i] += C(i + 1, k) * B[k]; B[i] *= -inv[i + 1]; } return B; } // 冪乗和 // 1^k + 2^k + ... + n^k // O(k^2) mint sumOfPowers(long long n, int k) { vector B = bernoulliNumbers(k); mint sum = 0; mint p = 1; mint s = k & 1 ? -1 : 1; for (int i = 1; i < k + 2; i++) { p *= n; sum += s * C(k + 1, i) * p * B[k + 1 - i]; s *= -1; } sum /= k + 1; return sum; } signed main() { cin.tie(0); ios::sync_with_stdio(false); long long n, k; cin >> n >> k; cout << sumOfPowers(n, k) << endl; return 0; }