#include <bits/stdc++.h>
using namespace std;
const int modulo = 1000000007;
int P[1000001];
int getPower(int a, int p) {
  if (p == 0) return 1;
  int res = getPower(a, p / 2);
  res = (int)((long long)res * res % modulo);
  if (p % 2 == 1) res = (int)((long long)res * a % modulo);
  return res;
}
int solve(int k, int w) {
  int res = (int)((long long)(getPower(k, w + 1) - 1) *
                  getPower(k - 1, modulo - 2) % modulo);
  for (int i = 1; i <= k; i++)
    if (i <= w) {
      res +=
          (int)((long long)P[i] * P[i] % modulo * getPower(k, w - i) % modulo);
      res %= modulo;
    } else {
      res += (int)((long long)P[i] * P[i] % modulo *
                   getPower(P[i - w], modulo - 2) % modulo);
      res %= modulo;
    }
  return res;
}
int main() {
  int k, w;
  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) % modulo);
  int res = (solve(k, w) - solve(k, w - 1)) % modulo;
  res = (res + modulo) % modulo;
  printf("%d\n", res);
  return 0;
}
