#include <bits/stdc++.h>
using namespace std;
struct __s {
  __s() {
    if (1) {
      ios_base::Init i;
      cin.sync_with_stdio(0);
      cin.tie(0);
    }
  }
  ~__s() {
    if (!1)
      fprintf(stderr, "Execution time: %.3lf s.\n",
              (double)clock() / CLOCKS_PER_SEC);
    long long n;
    cin >> n;
  }
} __S;
int mod = 1e9 + 7;
int p[1111111];
int binpow(int x, int n) {
  if (n == 0) return 1;
  if (n == 1) return x;
  if (n & 1) {
    int y = binpow(x, n - 1);
    return (y * 1LL * x) % mod;
  } else {
    int y = binpow(x, n >> 1);
    return (y * 1LL * y) % mod;
  }
}
int inverse(int x) { return binpow(x, mod - 2); }
int main(void) {
  int n, k;
  cin >> n >> k;
  int m = k + 2;
  for (int i = 1; i <= m; i++) {
    p[i] = p[i - 1] + binpow(i, k);
    p[i] %= mod;
  }
  if (n <= m) {
    cout << p[n] << '\n';
    return 0;
  }
  long long a = 1;
  long long b = 1;
  for (int j = 2; j <= m; j++) {
    a *= (n - j);
    a %= mod;
    b *= (1 - j + mod);
    b %= mod;
  }
  long long c = a * inverse(b);
  c %= mod;
  int l = (1 - m + mod);
  int r = 1;
  int ans = 0;
  for (int i = 1; i <= m; i++) {
    long long res = p[i];
    res *= c;
    res %= mod;
    ans += res;
    ans %= mod;
    c *= (n - i);
    c %= mod;
    c *= l;
    c %= mod;
    c *= inverse(((n - i - 1) * 1LL * r) % mod);
    c %= mod;
    r++;
    l++;
  }
  cout << ans << '\n';
  return 0;
}
