#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : -x;
}
const int mod = 1000000000 + 7;
int n;
int m, w;
int power(int a, int b) {
  int c = 1;
  while (b) {
    if (b & 1) c = ((long long)c * a) % mod;
    b /= 2;
    a = ((long long)a * a) % mod;
  }
  return c;
}
int f[1000001], rf[1000001];
int main() {
  scanf("%d%d", &n, &w);
  f[0] = rf[0] = 1;
  for (int i = 1; i <= n; i++) {
    f[i] = ((long long)f[i - 1] * i) % mod;
    rf[i] = power(f[i], mod - 2);
  }
  int ans = 0, cur = 1;
  for (int i = 1; i <= n; i++) {
    cur = ((long long)cur * (n - i + 1)) % mod;
    if (i + 2 <= w) {
      int all = ((long long)cur * n) % mod;
      all = ((long long)all * all) % mod;
      int tmp = f[n];
      if (n - i - 1 >= 0)
        tmp = ((long long)tmp * rf[n - i - 1]) % mod;
      else
        tmp = 0;
      tmp = ((long long)tmp * tmp) % mod;
      tmp = (all - tmp + mod) % mod;
      tmp = ((long long)tmp * power(n, w - i - 2)) % mod;
      ans = ((long long)ans + tmp) % mod;
    } else if (i + 1 == w) {
      int all = ((long long)cur * cur) % mod;
      all = ((long long)all * n) % mod;
      int tmp = ((long long)f[n - 1]) % mod;
      if (n - i - 1 >= 0)
        tmp = ((long long)tmp * rf[n - i - 1]) % mod;
      else
        tmp = 0;
      tmp = ((long long)tmp * tmp) % mod;
      tmp = ((long long)tmp * n) % mod;
      ans = ((long long)ans + all - tmp + mod) % mod;
    } else {
      int k = i - w;
      int all = ((long long)f[n] * rf[n - i]) % mod;
      all = ((long long)all * f[n - k]) % mod;
      all = ((long long)all * rf[n - i]) % mod;
      int tmp = ((long long)f[n] * rf[n - i]) % mod;
      tmp = ((long long)tmp * (n - i)) % mod;
      tmp = ((long long)tmp * f[n - (k + 2)]) % mod;
      if (n - i - 1 >= 0)
        tmp = ((long long)tmp * rf[n - i - 1]) % mod;
      else
        tmp = 0;
      ans = ((long long)ans + all - tmp + mod) % mod;
    }
  }
  printf("%d\n", ans);
  return 0;
}
