#include <bits/stdc++.h>
using namespace std;
long long fact[1000005], fact_inv[1000005], inv[1000005];
inline long long quick_pow(long long a, int n) {
  long long res = 1;
  while (n) {
    if (n & 1) res = res * a % 1000000007;
    a = a * a % 1000000007;
    n >>= 1;
  }
  return res;
}
int main() {
  int k, w;
  scanf("%d%d", &k, &w);
  fact[0] = 1;
  for (int i = 1; i < 1000005; i++) fact[i] = fact[i - 1] * i % 1000000007;
  fact_inv[1000004] = quick_pow(fact[1000004], 1000000007 - 2);
  for (int i = 1000003; i >= 0; i--)
    fact_inv[i] = fact_inv[i + 1] * (i + 1) % 1000000007;
  inv[0] = inv[1] = 1;
  for (int i = 2; i < 1000005; i++)
    inv[i] =
        (1000000007 - inv[1000000007 % i] * (1000000007 / i) % 1000000007) %
        1000000007;
  long long ans = 0;
  for (int t = 1; t <= k; t++) {
    if (t <= w - 1)
      ans = (ans + t * fact[k] % 1000000007 * fact[k] % 1000000007 *
                       quick_pow(k, w - t - 1) % 1000000007 * fact_inv[k - t] %
                       1000000007 * fact_inv[k - t] % 1000000007) %
            1000000007;
    else
      ans = (ans + fact[k] * w % 1000000007 * fact[k - t + w - 1] % 1000000007 *
                       fact_inv[k - t] % 1000000007 * fact_inv[k - t] %
                       1000000007) %
            1000000007;
  }
  for (int t = 1; t <= k; t++) {
    if (t + 1 <= w - 1)
      ans = (ans + t * fact[k] % 1000000007 * fact[k] % 1000000007 *
                       quick_pow(k, w - t - 2) % 1000000007 * fact_inv[k - t] %
                       1000000007 * fact_inv[k - t - 1] % 1000000007) %
            1000000007;
    else
      ans = (ans + fact[k] * (w - 1) % 1000000007 * fact[k - t + w - 2] %
                       1000000007 * fact_inv[k - t] % 1000000007 *
                       fact_inv[k - t - 1] % 1000000007) %
            1000000007;
  }
  printf("%lld\n", ans);
  return 0;
}
