#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e6 + 9, maxV = 536, MOD = 1e9 + 7, AND = (1 << 20) - 1;
int n, m;
long long fastPow(long long a, long long b) {
  if (b < 0) return fastPow(fastPow(a, abs(b)), MOD - 2);
  long long ret = 1;
  while (b) {
    if (b & 1) ret = ret * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return ret;
}
long long fac[maxN][2];
long long ncr(int n, int k) {
  if (k > n) return 0;
  return fac[n][0] * fac[k][1] % MOD * fac[n - k][1] % MOD;
}
long long npr(int n, int k) {
  if (k > n) return 0;
  return fac[n][0] * fac[n - k][1] % MOD;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  fac[0][0] = fac[0][1] = 1;
  for (long long i = 1; i <= max(m, n); i++) {
    fac[i][0] = fac[i - 1][0] * i % MOD;
    fac[i][1] = fastPow(fac[i][0], MOD - 2);
  }
  long long ans = 0;
  for (long long k = 2; k <= n; k++) {
    long long treeamnt =
        npr(n - 2, k - 2) * k % MOD * fastPow(n, n - k - 1) % MOD;
    long long colamnt = ncr(m - 1, k - 2) * fastPow(m, n - k) % MOD;
    ans = (ans + treeamnt * colamnt) % MOD;
  }
  cout << ans << '\n';
}
