#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
inline int add(int x, int y) {
  x += y;
  if (x >= mod) x -= mod;
  return x;
}
inline int mul(int x, int y) { return ((long long)x) * y % mod; }
const int N = 1e6 + 5;
int fac[N + 5], ifac[N + 5], p[N + 5];
int Pow(int a, int b) {
  int x = 1;
  for (; b > 0; b >>= 1) {
    if (b & 1) x = mul(x, a);
    a = mul(a, a);
  }
  return x;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n, m;
  cin >> n >> m;
  fac[0] = ifac[0] = p[0] = 1;
  for (int i = 1; i < N; ++i) {
    fac[i] = mul(fac[i - 1], i);
    ifac[i] = Pow(fac[i], mod - 2);
    p[i] = mul(p[i - 1], m);
  }
  auto comb = [&](int x, int y) {
    return mul(mul(fac[x], ifac[x - y]), ifac[y]);
  };
  auto f = [&](int x, int y) {
    if (x == y) return 1;
    return mul(y, Pow(x, x - y - 1));
  };
  int ans = 0;
  for (int k = 1; k < n; ++k)
    if (m >= k) {
      int g = mul(mul(comb(m - 1, m - k), comb(n - 2, k - 1)),
                  mul(mul(fac[k - 1], p[n - k - 1]), f(n, k + 1)));
      ans = add(ans, g);
    }
  cout << ans << '\n';
  return 0;
}
