#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  x = 0;
  bool f = 0;
  char ch = getchar();
  for (; !(ch >= '0' && ch <= '9'); ch = getchar()) f |= ch == '-';
  for (; (ch >= '0' && ch <= '9'); ch = getchar())
    x = (x << 3) + (x << 1) + (ch ^ 48);
  x = f ? -x : x;
}
inline int qpow(int x, int y = (int)(1e9 + 7) - 2) {
  int s = 1;
  while (y) {
    if (y & 1) s = 1ll * s * x % (int)(1e9 + 7);
    x = 1ll * x * x % (int)(1e9 + 7);
    y >>= 1;
  }
  return s;
}
int N, k, ans, n[int(1e6 + 5)], m[int(1e6 + 5)], sum[int(1e6 + 5)],
    fn[int(1e6 + 5)], fm[int(1e6 + 5)];
int main() {
  read(N);
  read(k);
  if (N <= k + 1) {
    for (int i = 1; i <= N; ++i)
      sum[i] = (sum[i - 1] + qpow(i, k)) % (int)(1e9 + 7);
    printf("%d\n", sum[N]);
    return 0;
  }
  m[0] = 1;
  n[0] = N;
  for (int i = 1; i <= k + 1; ++i)
    n[i] = 1ll * n[i - 1] * (N - i) % (int)(1e9 + 7),
    m[i] = 1ll * m[i - 1] * i % (int)(1e9 + 7),
    sum[i] = (sum[i - 1] + qpow(i, k)) % (int)(1e9 + 7);
  fn[k + 1] = qpow(n[k + 1]);
  fm[k + 1] = qpow(m[k + 1]);
  for (int i = k; i >= 0; --i)
    fm[i] = 1ll * fm[i + 1] * (i + 1) % (int)(1e9 + 7),
    fn[i] = 1ll * fn[i + 1] * (N - (i + 1)) % (int)(1e9 + 7);
  for (int i = 0; i <= k + 1; ++i)
    ans = (ans +
           1ll * n[k + 1] * fn[i] % (int)(1e9 + 7) * n[i - 1] % (int)(1e9 + 7) *
               sum[i] % (int)(1e9 + 7) * fm[i] % (int)(1e9 + 7) *
               fm[k + 1 - i] * ((k + 1 - i) & 1 ? -1 : 1) % (int)(1e9 + 7) +
           (int)(1e9 + 7)) %
          (int)(1e9 + 7);
  printf("%d\n", ans);
  return 0;
}
