#include <bits/stdc++.h>
using namespace std;
const int p = 1e9 + 7;
inline int power(int x, int y) {
  int s = 1;
  for (; y; x = (long long)x * x % p, y >>= 1)
    if (y & 1) s = (long long)s * x % p;
  return s;
}
int fac[1000005], invf[1000005];
inline int C(int n, int m) {
  if (n < m) return 0;
  return (long long)fac[n] * invf[m] % p * invf[n - m] % p;
}
int main() {
  int n, w;
  scanf("%d%d", &n, &w);
  int m = max(n, w);
  fac[0] = 1;
  for (int i = 1; i <= m; i++) fac[i] = (long long)fac[i - 1] * i % p;
  invf[m] = power(fac[m], p - 2);
  for (int i = m - 1; ~i; i--) invf[i] = (long long)invf[i + 1] * (i + 1) % p;
  int ans = 0;
  for (int i = 0; i <= n - 2 && i <= w - 1; i++) {
    ans = (ans +
           (long long)C(n - 2, i) * fac[i] % p * C(w - 1, i) % p *
               (i < n - 2 ? (long long)power(n, n - i - 3) * (i + 2) % p : 1) %
               p * power(w, n - i - 2) % p) %
          p;
  }
  printf("%d", ans);
  return 0;
}
