#include <bits/stdc++.h>
const int N = 1000005, p = 1e9 + 7;
int k, w, ans;
long long r[N], f[N], g[N], S;
long long pow(long long x, long long y) {
  long long res = 1;
  x %= p;
  for (; y; y /= 2, x = x * x % p)
    if (y & 1) res = res * x % p;
  return res;
}
long long A(int x, int y) { return f[x] * g[x - y] % p; }
int main() {
  scanf("%d%d", &k, &w), r[1] = f[0] = g[0] = 1, S = pow(k, w - 2);
  for (int i = 2; i <= k; i++) r[i] = -r[p % i] * (p / i) % p;
  for (int i = 1; i <= k; i++)
    f[i] = f[i - 1] * i % p, g[i] = g[i - 1] * r[i] % p;
  for (int i = 1; i <= k; i++) {
    if (i + 1 <= w)
      ans = (A(k, i) * A(k, i) % p * S % p * i + ans) % p;
    else
      ans = (A(k, i) * A(k - i + w - 1, w - 1) % p * w + ans) % p;
    if (i == k) break;
    S = S * r[k] % p;
    if (i + 2 <= w)
      ans = (A(k, i) * A(k, i + 1) % p * S % p * i + ans) % p;
    else
      ans = (A(k, i + 1) * A(k - i + w - 2, w - 2) % p * (w - 1) + ans) % p;
  }
  printf("%d\n", (ans + p) % p);
  return 0;
}
