#include <bits/stdc++.h>
using namespace std;
const int modl = (int)1e9 + 7;
int n, m;
int inv[(int)1e6 + 1], fac[(int)1e6 + 1];
int mpow(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1) res = ((long long)res) * x % modl;
    x = ((long long)x) * x % modl;
    y >>= 1;
  }
  return res;
}
int main() {
  int i, j, k, l;
  int cnt, tmp, tmp2;
  cin >> n >> m;
  inv[0] = inv[1] = 1;
  for (i = 2; i <= m - 1; i++)
    inv[i] = ((long long)inv[i - 1]) * mpow(i, modl - 2) % modl;
  cnt = 0;
  k = min(m + 1, n);
  fac[2] = 1;
  for (i = 3; i <= k; i++)
    fac[i] = ((long long)fac[i - 1]) * (n - i + 1) % modl;
  int mlnl = 1;
  int ttmf = 1;
  for (i = 2; i <= m - 1; i++) ttmf = ((long long)ttmf) * i % modl;
  for (i = 0; i < n - k; i++) {
    mlnl = ((long long)mlnl) * m % modl;
    if (i) mlnl = ((long long)mlnl) * n % modl;
  }
  for (i = k; i >= 2; i--) {
    tmp = ((long long)ttmf) * inv[i - 2] % modl;
    tmp = ((long long)tmp) * inv[m - i + 1] % modl;
    tmp = ((long long)tmp) * fac[i] % modl;
    if (n - i) {
      tmp = ((long long)tmp) * mlnl % modl;
      tmp = ((long long)tmp) * i % modl;
      mlnl = ((long long)mlnl) * n % modl;
    }
    mlnl = ((long long)mlnl) * m % modl;
    cnt = (cnt + tmp) % modl;
  }
  cout << cnt << "\n";
  return 0;
}
