#include <bits/stdc++.h>
long long fac[1000005];
long long inv[1000005];
long long w, k;
long long qp(long long b, long long e) {
  long long ans, t;
  ans = 1, t = b;
  while (e) {
    if (e & 1) {
      ans *= t;
      ans %= 1000000007;
    }
    t *= t;
    t %= 1000000007;
    e >>= 1;
  }
  return ans;
}
long long P(long long m, long long n) {
  if (m > n) return 0;
  if (m < 0 || n < 0) return 0;
  return fac[n] * inv[n - m] % 1000000007;
}
long long work() {
  long long l, d, t1, t2, t3, ans = 0;
  for (l = 1; l <= k; ++l) {
    if (w - l >= 2) {
      t1 = qp(k, (l + w) - l * 2 - 2) % 1000000007;
      t2 = (P(l, k) * P(l, k)) % 1000000007;
      t2 = (t2 * k) % 1000000007 * k % 1000000007;
      t3 = (P(l + 1, k) * P(l + 1, k)) % 1000000007;
      ans += (t2 - t3 + 1000000007) % 1000000007 * t1 % 1000000007;
      ans %= 1000000007;
    }
    if (w - l == 1) {
      t1 = (P(l, k) * P(l, k)) % 1000000007;
      t1 = t1 * k % 1000000007;
      t2 = (P(l + 1, k) * P(l, k - 1)) % 1000000007;
      ans += (t1 - t2 + 1000000007) % 1000000007;
      ans %= 1000000007;
    }
    if (w <= l) {
      d = l - w;
      t1 = (P(d, k) * P(w, k - d)) % 1000000007 * P(w, k - d) % 1000000007;
      t2 = (P(d + 2, k) * P(w - 1, k - d - 2)) % 1000000007 *
           P(w - 1, k - d - 2) % 1000000007;
      ans += (t1 - t2 + 1000000007) % 1000000007;
      ans %= 1000000007;
    }
  }
  return (ans + 1000000007) % 1000000007;
}
int main() {
  int i;
  scanf("%I64d%I64d", &k, &w);
  fac[0] = inv[0] = 1;
  for (i = 1; i <= k; ++i) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= 1000000007;
    inv[i] = qp(fac[i], 1000000007 - 2) % 1000000007;
  }
  printf("%I64d\n", work());
  return 0;
}
