#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int fc[1000001], ans, k, w, dv[1000001], nv[1000001];
inline int pw(int base, int index) {
  long long ret = 1, tmp = base;
  for (; index; index >>= 1, tmp *= tmp, tmp %= mod)
    if (index & 1) ret *= tmp, ret %= mod;
  return ret;
}
inline int P(int n, int k) {
  long long ret = fc[n];
  ret = ret * dv[n - k] % mod;
  return ret;
}
int main() {
  cin >> k >> w;
  fc[0] = nv[0] = nv[1] = dv[0] = 1;
  for (int i = 1; i < 1000001; i++) fc[i] = (long long)fc[i - 1] * i % mod;
  for (int i = 2; i < 1000001; i++)
    nv[i] = (long long)(mod - mod / i) * nv[mod % i] % mod;
  for (int i = 1; i < 1000001; i++) dv[i] = (long long)dv[i - 1] * nv[i] % mod;
  for (int i = 1; i <= k; ++i) {
    if (i <= w - 1)
      ans = ((long long)ans + (long long)P(k, i) * i % mod * pw(k, w - i - 1) %
                                  mod * P(k, i) % mod) %
            mod;
    else
      ans = ((long long)ans +
             (long long)P(k, i) * w % mod * P(k - i + w - 1, w - 1) % mod) %
            mod;
    if (i <= w - 2)
      ans = ((long long)ans + (long long)P(k, i) * i % mod * pw(k, w - i - 2) %
                                  mod * P(k, i + 1) % mod) %
            mod;
    else
      ans = ((long long)ans + (long long)P(k, i) * (w - 1) % mod *
                                  P(k - i + w - 2, w - 1) % mod) %
            mod;
  }
  cout << ans << endl;
  return 0;
}
