#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long power(long long x, long long k) {
  long long res = 1;
  while (k > 0) {
    if (k & 1) res = res * x % MOD;
    x = x * x % MOD;
    k >>= 1;
  }
  return res;
}
long long inverse(long long x) { return power(x, MOD - 2); }
const long long MN = 1000111;
long long y[MN], gt[MN], inv_gt[MN], f[MN], l[MN], r[MN];
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  gt[0] = 1;
  for (long long i = (1), _b = (MN - 1); i <= _b; ++i)
    gt[i] = gt[i - 1] * i % MOD;
  for (long long i = (0), _b = (MN); i <= _b; ++i) inv_gt[i] = inverse(gt[i]);
  long long x, k;
  while (cin >> x >> k) {
    long long n = k + 2;
    for (long long i = (1), _b = (n); i <= _b; ++i) y[i] = power(i, k);
    for (long long i = (1), _b = (n); i <= _b; ++i)
      y[i] = (y[i - 1] + y[i]) % MOD;
    long long res = 0;
    for (long long i = (1), _b = (n); i <= _b; ++i) {
      f[i] = (x - i + MOD) % MOD;
    }
    l[0] = 1;
    for (long long i = (1), _b = (n); i <= _b; ++i)
      l[i] = l[i - 1] * f[i] % MOD;
    r[n + 1] = 1;
    for (long long i = (n), _b = (1); i >= _b; --i)
      r[i] = r[i + 1] * f[i] % MOD;
    for (long long i = (1), _b = (n); i <= _b; ++i) {
      long long cur = 1;
      cur = cur * l[i - 1] % MOD;
      cur = cur * inv_gt[i - 1] % MOD;
      cur = cur * r[i + 1] % MOD;
      cur = cur * inv_gt[n - i] % MOD;
      if ((n - i) % 2) cur = cur * (MOD - 1) % MOD;
      res = (res + cur * y[i]) % MOD;
    }
    cout << res << endl;
  }
}
