#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 1;
const int mod = 1e9 + 7;
void add(int &a, int b) {
  a += b;
  if (a >= mod) a -= mod;
}
void sub(int &a, int b) {
  a -= b;
  if (a < 0) a += mod;
}
int mul(int a, int b) { return 1ll * a * b % mod; }
int Pow(int a, int b) {
  if (b < 0) return Pow(a, mod + b - 1);
  int ans = 1;
  while (b) {
    if (b & 1) ans = mul(ans, a);
    a = mul(a, a);
    b >>= 1;
  }
  return ans;
}
int inv(int a, int p) { return a == 1 ? 1 : p - 1ll * p * inv(p % a, a) / a; }
int Fac[N];
int Inv[N];
int Ckn(int n, int k) {
  if (n < k) return 0;
  if (k < 0) return 0;
  return mul(Fac[n], mul(Inv[k], Inv[n - k]));
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  Fac[0] = Inv[0] = 1;
  for (int i = 1; i < N; ++i) Fac[i] = mul(Fac[i - 1], i);
  Inv[N - 1] = inv(Fac[N - 1], mod);
  for (int i = N - 2; i; --i) Inv[i] = mul(Inv[i + 1], i + 1);
  int n;
  cin >> n;
  int m;
  cin >> m;
  int ans = 0;
  for (int i = 1; i < n; ++i) {
    int cur = Ckn(m - 1, i - 1);
    cur = mul(cur, Fac[n - 2]);
    cur = mul(cur, Inv[n - i - 1]);
    cur = mul(cur, i + 1);
    cur = mul(cur, Pow(n, n - i - 2));
    cur = mul(cur, Pow(m, n - i - 1));
    add(ans, cur);
  }
  cout << ans << endl;
}
