#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using dbl = double;
const int P = 1e9 + 7;
int sum(int a, int b) {
  a += b;
  return a >= P ? a - P : a;
}
int sub(int a, int b) { return sum(a, P - b); }
int mul(int a, int b) { return (ll)a * b % P; }
int bpow(int x, int pw) {
  int res = 1;
  for (; pw > 0; pw /= 2, x = mul(x, x))
    if (pw % 2 == 1) res = mul(res, x);
  return res;
}
int inv(int x) { return bpow(x, P - 2); }
const int N = 1e6 + 10;
int fact[N], rfact[N];
int mpw[N], npw[N];
int cnk(int n, int k) {
  if (k < 0 || k > n) return 0;
  return mul(fact[n], mul(rfact[k], rfact[n - k]));
}
void solve() {
  int n, m;
  cin >> n >> m;
  npw[0] = mpw[0] = 1;
  for (int i = 1; i < N; ++i) {
    mpw[i] = mul(mpw[i - 1], m);
    npw[i] = mul(npw[i - 1], n);
  }
  fact[0] = 1;
  for (int i = 1; i < N; ++i) {
    fact[i] = mul(fact[i - 1], i);
  }
  rfact[N - 1] = inv(fact[N - 1]);
  for (int i = N - 2; i >= 0; --i) rfact[i] = mul(rfact[i + 1], i + 1);
  int ans = 0;
  for (int l = 1; l < n; ++l) {
    auto btw = mul(cnk(n - 2, l - 1), fact[l - 1]);
    auto ed = mul(mpw[n - 1 - l], cnk(m - 1, l - 1));
    auto out = 1;
    if (l != n - 1) out = mul(npw[n - l - 2], l + 1);
    ans = sum(ans, mul(btw, mul(ed, out)));
  }
  cout << ans << endl;
}
int main() {
  int test_count = 1;
  for (int i = 0; i < test_count; ++i) {
    solve();
  }
  (void)0;
}
