#include <bits/stdc++.h>
long long a, b, c, d, e, f, g, h, i, j, k;
long long H[1000005], H1[1000005];
long long l, w;
long long an;
long long Mod(long long x) {
  if (x < 0) x += (1 + (-x) / 1000000007) * 1000000007;
  return x % 1000000007;
}
long long Mi(long long x, long long y) {
  long long ii;
  if (y < 0) return 0;
  if (y == 0) return 1;
  if (y == 1) return x;
  ii = Mi(x, y >> 1);
  ii = Mod(ii * ii);
  if (y & 1) ii = Mod(ii * x);
  return ii;
}
long long P(long long x, long long y) { return Mod(H[x] * H1[x - y]); }
int main() {
  scanf("%I64d%I64d", &a, &b);
  H[0] = 1;
  H1[0] = 1;
  for (i = 1; i <= a; i++) {
    H[i] = H[i - 1] * i;
    H[i] %= 1000000007;
    H1[i] = Mi(H[i], 1000000007 - 2);
  }
  for (l = 0; l <= a; l++) {
    if (b - l >= 2) {
      e = Mod(P(a, l) * P(a, l));
      e = Mod(e * a);
      e = Mod(e * a);
      e -= Mod(P(a, l + 1) * P(a, l + 1));
      d = Mi(a, b - l - 2);
      e = Mod(d * e);
      an += e;
      an %= 1000000007;
    }
    if (b - l == 1) {
      e = Mod(P(a, l) * P(a, l));
      e = Mod(e * a);
      e -= Mod(P(a, l + 1) * P(a - 1, l));
      an += e;
      an %= 1000000007;
    }
    if (l >= b) {
      c = l - b;
      e = Mod(P(a, c) * P(a - c, b));
      e = Mod(e * P(a - c, b));
      d = Mod(P(a, c + 2) * P(a - c - 2, b - 1));
      d = Mod(d * P(a - c - 2, b - 1));
      e = Mod(e - d);
      an += e;
      an %= 1000000007;
    }
  }
  printf("%I64d\n", an);
  return 0;
}
