#include <bits/stdc++.h>
using namespace std;
const long long p = 1000000007;
long long mul[1000010], _mul[1000010], inv[1000010];
int m, K, n;
inline long long C(int x, int y) {
  if (y > x) return 0;
  return mul[x] * _mul[y] % p * _mul[x - y] % p;
}
inline long long A(int x, int y) {
  if (y > x) return 0;
  return mul[x] * _mul[x - y] % p;
}
long long qpow(long long x, long long y) {
  if (y < 0) return 0;
  long long ans = 1;
  for (; y; x = x * x % p, y >>= 1)
    if (y & 1) ans = ans * x % p;
  return ans;
}
int main() {
  scanf("%d%d", &K, &n);
  int i, j, L;
  mul[0] = mul[1] = _mul[0] = _mul[1] = inv[0] = inv[1] = 1;
  for (i = 2; i <= K; i++)
    mul[i] = mul[i - 1] * i % p, inv[i] = 1ll * (p - p / i) * inv[p % i] % p,
    _mul[i] = _mul[i - 1] * inv[i] % p;
  long long ans = 0;
  for (L = 1; L <= K; L++) {
    if (n - L >= 2)
      (ans += qpow(K, n - L - 2) *
              (1ll * K * K % p * A(K, L) % p * A(K, L) % p -
               A(K, L + 1) * A(K, L + 1) % p + p) %
              p) %= p;
    else if (n - L == 1)
      (ans += (1ll * K * A(K, L) % p * A(K, L) % p -
               A(K, L + 1) * A(K - 1, L) % p + p) %
              p) %= p;
    else
      (ans +=
       (1ll * A(K, L - n) * A(K - (L - n), n) % p * A(K - (L - n), n) % p -
        A(K, L - n + 2) * A(K - (L - n + 2), n - 1) % p *
            A(K - (L - n + 2), n - 1) % p) %
       p) %= p;
  }
  printf("%I64d\n", (ans % p + p) % p);
  return 0;
}
