#include <bits/stdc++.h>
using namespace std;
const int maxK = 1000006;
const int Mod = 1000000007;
int jc[maxK], ni[maxK];
int k, w, ans, tmp, cnt;
void add(int &x, int y) {
  x += y;
  if (x >= Mod) x -= Mod;
}
int mul(long long x, int y) {
  x *= y;
  if (x >= Mod) x %= Mod;
  return x;
}
int power(int x, int k) {
  int s = 1;
  while (k) {
    if (k & 1) s = mul(s, x);
    k >>= 1;
    x = mul(x, x);
  }
  return s;
}
int A(int n, int m) {
  if (m > n) return 0;
  return mul(jc[n], ni[n - m]);
}
int main() {
  cin >> k >> w;
  jc[0] = ni[0] = 1;
  for (int i = 1; i <= k; ++i) {
    jc[i] = mul(jc[i - 1], i);
    ni[i] = power(jc[i], Mod - 2);
  }
  for (int i = 1; i <= k; ++i) {
    if (w > i) {
      tmp = mul(i, A(k - 1, i - 1));
      tmp = mul(tmp, tmp);
      add(tmp, mul(mul(A(k - 1, i), A(k - 1, i - 1)), i * 2));
      tmp = mul(tmp, power(k, (w - i)));
    } else {
      tmp = A(k - (i - w), w);
      tmp = mul(tmp, tmp);
      cnt = A(k - (i - w) - 2, w - 1);
      cnt = mul(cnt, cnt);
      cnt = mul(cnt, A(k - (i - w), 2));
      tmp -= cnt;
      if (tmp < 0) tmp += Mod;
      tmp = mul(tmp, A(k, i - w));
    }
    add(ans, tmp);
  }
  printf("%d\n", ans);
  return 0;
}
