#include <bits/stdc++.h>
const int mo = 1000000007;
int k, w, P[1000001];
int quick(int a, int p) {
  if (p == 0) return 1;
  int res = quick(a, p / 2);
  res = (int)((long long)res * res % mo);
  if (p % 2 == 1) res = (int)((long long)res * a % mo);
  return res;
}
int calc(int k, int w) {
  int res = (int)((long long)(quick(k, w + 1) - 1) * quick(k - 1, mo - 2) % mo);
  for (int i = 1; i <= k; i++)
    if (i <= w)
      res = (res + (int)((long long)P[i] * P[i] % mo * quick(k, w - i) % mo)) %
            mo;
    else
      res = (res + (int)((long long)P[i] * P[i] % mo * quick(P[i - w], mo - 2) %
                         mo)) %
            mo;
  return res;
}
int main() {
  scanf("%d%d", &k, &w);
  if (k == 1) {
    printf("1\n");
    return 0;
  }
  P[0] = 1;
  for (int i = 1; i <= k; i++)
    P[i] = (int)((long long)P[i - 1] * (k - i + 1) % mo);
  int res = (calc(k, w) - calc(k, w - 1)) % mo;
  res = (res + mo) % mo;
  printf("%d\n", res);
  return 0;
}
