#include <bits/stdc++.h>
using namespace std;
inline int read() {
  register int data = 0, w = 1;
  char ch = 0;
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') w = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    data = (data << 1) + (data << 3) + (ch ^ 48), ch = getchar();
  return data * w;
}
int n, m, fac[1000005], ifac[1000005], inv[1000005];
const int mod = 1e9 + 7;
int KSM(int x, int k) {
  int tmp = 1, y = x;
  for (int i = k; i; i >>= 1, y = 1ll * y * y % mod)
    if (i & 1) tmp = 1ll * tmp * y % mod;
  return tmp;
}
int C(int n, int m) {
  return n < m ? 0 : 1ll * fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}
int F(int n, int k) {
  return 1ll * C(n - 1, k - 1) * KSM(n, n - k) % mod * fac[k] % mod;
}
const int M = 1e6;
int t[1000005];
int main() {
  n = read(), m = read();
  fac[0] = 1;
  for (int i = 1; i <= M; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
  ifac[M] = KSM(fac[M], mod - 2);
  for (int i = M - 1; i >= 0; i--) ifac[i] = 1ll * ifac[i + 1] * (i + 1) % mod;
  inv[0] = inv[1] = 1;
  for (int i = 2; i <= M; i++)
    inv[i] = 1ll * inv[mod % i] * (mod - mod / i) % mod;
  t[0] = 1;
  for (int i = 1; i <= M; i++) t[i] = 1ll * t[i - 1] * m % mod;
  int ans = 0;
  for (int i = 0; i <= min(m, n - 2); i++) {
    int x = 1ll * C(m - 1, i) * F(n, i + 2) % mod * t[n - 2 - i] % mod *
            inv[n] % mod * inv[n - 1] % mod;
    ans = (ans + x) % mod;
  }
  cout << ans << '\n';
  return 0;
}
