#include <bits/stdc++.h>
using namespace std;
long long k, w, ans;
long long f[1001000];
long long nimei[1001000];
long long tmp1, tmp2, tmp;
long long Pow(long long x, long long n) {
  long long tmp = 1;
  while (n) {
    if (n & 1) tmp = (tmp * x) % 1000000007LL;
    n >>= 1;
    x = (x * x) % 1000000007LL;
  }
  return tmp;
}
long long fuck(long long x, long long m) {
  if (m < 0) return 0;
  long long tmp, fuck;
  fuck = f[x];
  if (nimei[m]) return fuck * nimei[m] % 1000000007LL;
  tmp = Pow(f[m], 1000000007LL - 2LL);
  nimei[m] = tmp;
  return fuck * tmp % 1000000007LL;
}
int main() {
  f[0] = 1;
  for (long long i = 1; i <= 1000000; i++) {
    f[i] = f[i - 1] * i;
    f[i] %= 1000000007LL;
  }
  while (~scanf("%I64d%I64d", &k, &w)) {
    ans = 0;
    memset(nimei, 0, sizeof(nimei));
    for (long long i = 1; i <= k; i++) {
      if (i == w) {
        tmp1 = fuck(k, k - i);
        tmp1 = tmp1 * tmp1 % 1000000007LL;
        tmp2 = fuck(k, k - i - 1);
        tmp2 *= fuck(k - 2, k - 1 - i);
        tmp2 %= 1000000007LL;
        ans += tmp1 - tmp2 + 1000000007LL;
        ans %= 1000000007LL;
      }
      if (i == (w - 1)) {
        tmp1 = fuck(k, k - i);
        tmp1 = (tmp1 * tmp1 % 1000000007LL) * k % 1000000007LL;
        tmp2 = fuck(k - 1, k - i - 1);
        tmp2 = (tmp2 * tmp2 % 1000000007LL) * k % 1000000007LL;
        ans += tmp1 - tmp2 + 1000000007LL;
        ans %= 1000000007LL;
      }
      if (i < (w - 1)) {
        tmp = Pow(k, w - i);
        tmp1 = fuck(k, k - i);
        tmp1 = tmp1 * tmp1 % 1000000007LL;
        tmp2 = fuck(k - 1, k - i - 1);
        tmp2 = tmp2 * tmp2 % 1000000007LL;
        tmp2 %= 1000000007LL;
        ans += (tmp1 - tmp2 + 1000000007LL) % 1000000007LL * tmp;
        ans %= 1000000007LL;
      }
      if (i > w) {
        tmp1 = fuck(k, k - i);
        tmp1 *= fuck(k - i + w, k - i);
        tmp1 %= 1000000007LL;
        tmp2 = fuck(k, k - i + w - 2);
        tmp = fuck(k - i + w - 2, k - i - 1);
        tmp = tmp * tmp % 1000000007LL;
        tmp2 = tmp2 * tmp % 1000000007LL;
        ans += tmp1 - tmp2 + 1000000007LL;
        ans %= 1000000007LL;
      }
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
