#include <bits/stdc++.h>
using namespace std;
inline int M(int x) { return (x >= 1000000007) ? (x - 1000000007) : x; }
inline int M1(int x) { return (x < 0) ? (x + 1000000007) : x; }
inline int ksm(int p, int k) {
  int res = 1;
  while (k) {
    if (k & 1) res = 1ll * res * p % 1000000007;
    k >>= 1;
    p = 1ll * p * p % 1000000007;
  }
  return res;
}
int K, m, ans = 0, ans1, ans2, fac[1000012], inv[1000012];
int main() {
  scanf("%d%d", &K, &m);
  int n, i, sec, oth;
  fac[0] = 1;
  for (i = 1; i <= K; i++) fac[i] = 1ll * fac[i - 1] * (K - i + 1) % 1000000007;
  inv[K] = ksm(fac[K], 1000000007 - 2);
  for (i = K - 1; i >= 0; i--) inv[i] = 1ll * inv[i + 1] * (K - i) % 1000000007;
  for (n = m; n <= m + K; n++) {
    sec = max(n - (m << 1), 0);
    oth = max(n - ((n - m) << 1), 0);
    ans1 = (n > (m + K)) ? 0
                         : (1ll * ksm(K, oth) * fac[n - m] % 1000000007 *
                            fac[n - m] % 1000000007 * inv[sec] % 1000000007);
    --m;
    sec = max(n - (m << 1), 0);
    oth = max(n - ((n - m) << 1), 0);
    ans2 = (n > (m + K)) ? 0
                         : (1ll * ksm(K, oth) * fac[n - m] % 1000000007 *
                            fac[n - m] % 1000000007 * inv[sec] % 1000000007);
    ++m;
    ans = M(ans + M1(ans1 - ans2));
  }
  printf("%d", ans);
  return 0;
}
