#include <bits/stdc++.h>
using namespace std;
long long mypow(long long x, int y) {
  long long an = 1;
  while (y) {
    if (y & 1) {
      an *= x;
      an %= 1000000007;
    }
    x *= x;
    x %= 1000000007;
    y >>= 1;
  }
  return an;
}
int d[1000001];
int f(int k, int w) {
  memset(d, 0, sizeof(d));
  d[k] = w;
  long long an = mypow(k, w);
  long long tmp = an;
  int now = k;
  for (int i = k - 1; i; i--) {
    d[i] += 2;
    tmp *= mypow(i, 2);
    tmp %= 1000000007;
    while (d[now] == 1) now--;
    d[now]--;
    tmp *= mypow(now, 1000000007 - 2);
    tmp %= 1000000007;
    an += tmp;
    if (an >= 1000000007) an -= 1000000007;
  }
  return an;
}
int main() {
  int i, j, k, w;
  scanf("%d%d", &k, &w);
  int an = f(k, w + 1) - f(k, w);
  if (an < 0) an += 1000000007;
  an += mypow(k, w);
  if (an >= 1000000007) an -= 1000000007;
  printf("%d\n", an);
  return 0;
}
