#include <bits/stdc++.h>
using namespace std;
long long gcd(long long n1, long long n2) {
  if (n2 == 0) return n1;
  if (n1 % n2 == 0) return n2;
  return gcd(n2, n1 % n2);
}
long long powmod(long long base, long long exponent) {
  if (exponent < 0) exponent += 1000000007LL - 1;
  long long ans = 1;
  while (exponent) {
    if (exponent & 1) ans = (ans * base) % 1000000007LL;
    base = (base * base) % 1000000007LL;
    exponent /= 2;
  }
  return ans;
}
long long fact[1000005], ifact[1000005];
long long choose(long long n, long long r) {
  if (r > n || n < 0 || r < 0) return 0;
  long long ans = fact[n] * ifact[r];
  ans %= 1000000007LL;
  ans *= ifact[n - r];
  ans %= 1000000007LL;
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, m;
  cin >> n >> m;
  long long ans = 0;
  fact[0] = ifact[0] = 1;
  for (int i = 1; i < 1000005; i++) {
    fact[i] = (i * 1LL * fact[i - 1]) % 1000000007LL;
    ifact[i] = powmod(fact[i], -1);
  }
  for (int x = 2; x <= n - 1; x++) {
    long long now = powmod(n, n - x - 1) * x;
    now %= 1000000007LL;
    now *= choose(n - 2, x - 2);
    now %= 1000000007LL;
    now *= choose(m - 1, x - 1 - 1);
    now %= 1000000007LL;
    now *= powmod(m, n - x);
    now %= 1000000007LL;
    now *= fact[x - 2];
    now %= 1000000007LL;
    ans = (ans + now) % 1000000007LL;
  }
  ans += choose(m - 1, n - 2) * fact[n - 2];
  ans %= 1000000007LL;
  cout << ans << '\n';
}
