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