#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, md = 1e9 + 7;
int n, m, ans, fac[N], iv[N], pn[N], pm[N], k;
inline int pow(int a, int b) {
  int res = 1;
  for (; b; b >>= 1, a = (unsigned long long)a * a % md)
    if (b & 1) res = (unsigned long long)res * a % md;
  return res;
}
inline int C(int n, int m) {
  return (n >= m) ? (unsigned long long)fac[n] * iv[m] % md * iv[n - m] % md
                  : 0;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m, k = max(n, m);
  for (int i = *fac = *pn = *pm = 1; i <= k; ++i)
    fac[i] = (unsigned long long)fac[i - 1] * i % md,
    pn[i] = (unsigned long long)pn[i - 1] * n % md,
    pm[i] = (unsigned long long)pm[i - 1] * m % md;
  iv[k] = pow(fac[k], md - 2);
  for (int i = k - 1; ~i; --i) iv[i] = (i + 1uLL) * iv[i + 1] % md;
  ans = (unsigned long long)C(m - 1, n - 2) % md * fac[n - 2] % md;
  for (int i = 0; i < n - 2; ++i)
    ans = (ans + (i + 2uLL) * C(n - 2, i) % md * C(m - 1, i) % md * fac[i] %
                     md * pn[n - i - 3] % md * pm[n - i - 2]) %
          md;
  cout << ans << '\n';
  return 0;
}
