#include <bits/stdc++.h>
const int mod = 1e9 + 7;
inline int pw(int base, int p) {
  static int res;
  for (res = 1; p; p >>= 1, base = static_cast<long long>(base) * base % mod)
    if (p & 1) res = static_cast<long long>(res) * base % mod;
  return res;
}
inline int inv(int x) { return pw(x, mod - 2); }
inline void reduce(int &x) { x += x >> 31 & mod; }
int n, k, ans;
int fac[1000010], s[1000010], prod = 1;
int main() {
  scanf("%d%d", &n, &k);
  if (k == 0) {
    std::printf("%d\n", n);
    return 0;
  }
  for (int i = 0; i <= k + 1; i++) {
    prod = static_cast<long long>(n - i) * prod % mod;
    s[i] = pw(i, k);
  }
  fac[0] = 1;
  for (int i = 1; i <= k + 1; i++) {
    fac[i] = static_cast<long long>(fac[i - 1]) * i % mod;
    reduce(s[i] += s[i - 1] - mod);
    if (n == i) {
      std::printf("%d\n", s[i]);
      return 0;
    }
  }
  for (int i = 1; i <= k + 1; i++) {
    reduce(ans += s[i] * static_cast<long long>(prod) % mod * inv(n - i) % mod *
                      inv(fac[i]) % mod * inv(fac[k - i + 1]) *
                      (k - i + 1 & 1 ? -1 : 1) % mod -
                  mod);
    reduce(ans);
  }
  printf("%d\n", ans);
  return 0;
}
