#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long mul(long long a, long long b) { return a * b % mod; }
long long add(long long a, long long b) {
  long long res = a + b;
  if (res >= mod) {
    res -= mod;
  }
  return res;
}
const long long MAXN = 1e6 + 5;
vector<long long> fact(MAXN);
vector<long long> fact_obr(MAXN);
vector<long long> pow_n(MAXN);
vector<long long> pow_m(MAXN);
long long n;
long long m;
long long fast_pow(long long a, long long b) {
  if (b == 0) {
    return 1;
  } else {
    if (b % 2 == 1) {
      return mul(fast_pow(a, b - 1), a);
    } else {
      long long res = fast_pow(a, b / 2);
      return mul(res, res);
    }
  }
}
void pre_calc() {
  fact[0] = 1;
  fact_obr[0] = fast_pow(fact[0], mod - 2);
  pow_n[0] = 1;
  pow_m[0] = 1;
  for (long long i = 1; i < MAXN; ++i) {
    fact[i] = mul(fact[i - 1], i);
    fact_obr[i] = fast_pow(fact[i], mod - 2);
    pow_n[i] = mul(pow_n[i - 1], n);
    pow_m[i] = mul(pow_m[i - 1], m);
  }
}
long long A(long long n, long long k) { return mul(fact[n], fact_obr[n - k]); }
long long C(long long n, long long k) {
  if (k > n) {
    return 0;
  }
  if (n == 0 && k != 0) {
    return 0;
  }
  return mul(A(n, k), fact_obr[k]);
}
long long f(long long y) {
  if (y == n) {
    return 1;
  }
  return mul(y, pow_n[n - y - 1]);
}
int32_t main() {
  cin >> n >> m;
  pre_calc();
  long long ans = 0;
  for (long long edges = 1; edges < n; ++edges) {
    long long _add = 1;
    _add = mul(_add, A(n - 2, edges - 1));
    _add = mul(_add, f(edges + 1));
    _add = mul(_add, C(m - 1, edges - 1));
    _add = mul(_add, pow_m[n - edges - 1]);
    ans = add(ans, _add);
  }
  cout << ans;
}
