#include <bits/stdc++.h>
using namespace std;
inline void Read(long long &x) {
  long long f = 1;
  char c = getchar();
  x = 0;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  x *= f;
}
const long long N = 1000010, mod = 1000000007;
long long n, k, fac[N], pre[N], suf[N], ans;
inline void Init() {
  fac[0] = pre[0] = suf[k + 3] = 1;
  for (register long long i = 1; i <= k + 2; i++) fac[i] = fac[i - 1] * i % mod;
  for (register long long i = 1; i <= k + 2; i++)
    pre[i] = pre[i - 1] * (n - i) % mod;
  for (register long long i = k + 2; i >= 1; i--)
    suf[i] = suf[i + 1] * (n - i) % mod;
}
inline long long Pow(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod, b >>= 1;
  }
  return ans;
}
signed main() {
  Read(n), Read(k);
  Init();
  long long sum = 0;
  for (register long long i = 1; i <= k + 2; i++) {
    long long p = pre[i - 1] * suf[i + 1] % mod;
    long long q =
        fac[i - 1] * fac[k + 2 - i] % mod * ((k - i) & 1 ? -1 : 1) % mod;
    sum = (sum + Pow(i, k)) % mod;
    ans = (ans + sum * p % mod * Pow(q, mod - 2) % mod) % mod;
  }
  cout << (ans + mod) % mod << endl;
  return 0;
}
