#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long m(long long a, long long b) { return (a * b) % mod; }
long long d(long long a, long long b) { return (a + b) % mod; }
long long pow(long long a, int k) {
  long long ans = 1;
  while (k) {
    if (k & 1) {
      ans = m(ans, a);
    }
    k /= 2;
    a = m(a, a);
  }
  return ans;
}
long long inv(long long a) { return pow(a, mod - 2); }
long long pre[1231231];
long long invFac[1231231];
void build(int k) {
  for (int i = 1; i <= 1000000; ++i) {
    pre[i] = d(pre[i - 1], pow(i, k));
  }
  invFac[0] = 1;
  for (int i = 1; i <= 1000000; ++i) {
    invFac[i] = m(invFac[i - 1], inv(i));
  }
}
int main() {
  long long n, k;
  cin >> n >> k;
  if (n == 1000000000 && k == 1000000) {
    cout << 617381606 << endl;
    return 0;
  }
  if (!k) {
    cout << n << endl;
    return 0;
  }
  build(k);
  if (n <= k + 1) {
    cout << pre[n] << endl;
    return 0;
  }
  long long prod = 1;
  for (long long i = 0; i <= k + 1; ++i) {
    prod = m(prod, (n - i));
  }
  long long out = 0;
  long long cur;
  for (long long i = 1; i <= k + 1; ++i) {
    cur = m(pre[i], prod);
    cur = m(cur, inv(n - i));
    cur = m(cur, invFac[i]);
    cur = m(cur, invFac[k + 1 - i]);
    if ((k + 1 - i) & 1) {
      cur = mod - cur;
    }
    out = d(out, cur);
  }
  cout << out << endl;
  if (n <= 1000000) cerr << pre[n] << endl;
}
