#include <bits/stdc++.h>
using namespace std;
template <typename Tp>
inline void read(Tp &x) {
  static char c;
  static bool neg;
  x = 0, c = getchar(), neg = false;
  for (; !isdigit(c); c = getchar()) {
    if (c == '-') {
      neg = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (neg) {
    x = -x;
  }
}
const int MOD = 1e9 + 7;
const int PHI = MOD - 1;
inline int power(int base, int exp) {
  if (exp < 0) {
    exp += PHI;
  }
  int res = 1;
  while (exp > 0) {
    if (exp & 1) {
      res = (long long)res * base % MOD;
    }
    base = (long long)base * base % MOD;
    exp >>= 1;
  }
  return res;
}
const int N = 1e6 + 5;
int n, m;
int fac[N], inv[N];
inline void init(int MX) {
  fac[0] = fac[1] = inv[0] = inv[1] = 1;
  for (int i = 2; i <= MX; ++i) {
    fac[i] = (long long)fac[i - 1] * i % MOD;
    inv[i] = (long long)(MOD - MOD / i) * inv[MOD % i] % MOD;
  }
  for (int i = 2; i <= MX; ++i) {
    inv[i] = (long long)inv[i - 1] * inv[i] % MOD;
  }
}
inline int C(int n, int m) {
  if (n < m) {
    return 0;
  } else {
    return (long long)fac[n] * inv[m] % MOD * inv[n - m] % MOD;
  }
}
inline int calc(int k) {
  return (long long)C(n - 2, k - 1) * fac[k - 1] % MOD * C(m - 1, k - 1) % MOD *
         power(m, n - k - 1) % MOD * power(n, n - k - 2) % MOD * (k + 1) % MOD;
}
int main() {
  read(n), read(m);
  init(std::max(n, m));
  int ans = 0;
  for (int i = 1; i < n; ++i) {
    ans = (ans + calc(i)) % MOD;
  }
  printf("%d\n", ans);
}
