#include <bits/stdc++.h>
using namespace std;
inline void setmin(int &x, int y) {
  if (y < x) x = y;
}
inline void setmax(int &x, int y) {
  if (y > x) x = y;
}
inline void setmin(long long &x, long long y) {
  if (y < x) x = y;
}
inline void setmax(long long &x, long long y) {
  if (y > x) x = y;
}
const int N = 100000;
const int inf = (int)1e9 + 1;
const long long big = (long long)1e18 + 1;
const int P = 239;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;
long long bin_pow(long long a, long long p) {
  if (a == 0) return 0;
  long long res = 1;
  while (p) {
    if (p & 1) res = res * a % MOD;
    a = a * a % MOD;
    p >>= 1;
  }
  return res;
}
long long rev(long long x) { return bin_pow(x, MOD - 2); }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.precision(20);
  cout << fixed;
  int n, k;
  cin >> n >> k;
  long long x[k + 2], y[k + 2];
  for (int i = 0; i < k + 2; i++) {
    x[i] = i;
    y[i] = bin_pow(x[i], k);
    if (i > 0) y[i] = (y[i] + y[i - 1]) % MOD;
  }
  if (n <= k + 1) {
    cout << y[n] << "\n";
    return 0;
  }
  long long A = 1, B = 1;
  for (int i = 0; i < k + 2; i++) {
    A = A * (n - x[i]) % MOD;
    if (i > 0) B = B * (x[0] - x[i] + MOD) % MOD;
  }
  long long ans = 0;
  for (int i = 0; i < k + 2; i++) {
    ans = (ans + y[i] * (A * rev(n - x[i]) % MOD) % MOD * rev(B) % MOD) % MOD;
    if (i < k + 1) {
      B = B * (i + 1) % MOD;
      B = B * rev(-(k + 1 - i) + MOD) % MOD;
    }
  }
  cout << ans << "\n";
  return 0;
}
