#include <bits/stdc++.h>
const int MAX_N = 1e6 + 10;
const long long MOD = 1e9 + 7;
long long sqr(long long x) { return x * x % MOD; }
long long qpow(long long a, long long b) {
  return b ? sqr(qpow(a, b / 2)) * (b & 1 ? a : 1) % MOD : 1;
}
long long inv(long long a) { return qpow(a, MOD - 2); }
int N, M, V;
long long fac[MAX_N], ifac[MAX_N], ans;
long long comb(long long a, long long b) {
  return fac[a] * ifac[b] % MOD * ifac[a - b] % MOD;
}
int main() {
  std::scanf("%d%d", &N, &M);
  V = std::max(N, M);
  fac[0] = 1;
  for (int i = 1; i <= V; i++) fac[i] = fac[i - 1] * i % MOD;
  ifac[V] = inv(fac[V]);
  for (int i = V; i; i--) ifac[i - 1] = ifac[i] * i % MOD;
  for (int i = 0; i <= std::min(N - 2, M - 1); i++) {
    int n = N - 1 - i, k = i + 2;
    long long cur = comb(N - 2, i) * comb(M - 1, i) % MOD * fac[i] % MOD;
    if (n > 1)
      (cur *= k * qpow(n - 1 + k, n - 2) % MOD * qpow(M, n - 1) % MOD) %= MOD;
    (ans += cur) %= MOD;
  }
  std::printf("%lld\n", ans);
}
