#include <bits/stdc++.h>
const int MOD = 1e9 + 7;
const int maxn = 1000100;
int f[maxn], g[maxn], a[maxn], b[maxn];
int main() {
  int n, m, c, ans = 0;
  scanf("%d%d", &n, &m);
  int up = n - 2 < m - 1 ? n - 2 : m - 1;
  for (int i = (0); i <= (maxn - 1); ++i) {
    f[i] = i == 0 ? 1 : (long long)f[i - 1] * i % MOD;
    g[i] = i <= 1 ? 1 : (long long)g[MOD % i] * (MOD - MOD / i) % MOD;
    a[i] = i == 0 ? 1 : (long long)a[i - 1] * m % MOD;
    b[i] = i == 0 ? 1 : (long long)b[i - 1] * n % MOD;
  }
  for (int i = (1); i <= (maxn - 1); ++i)
    g[i] = (long long)g[i - 1] * g[i] % MOD;
  for (int i = (0); i <= (up); ++i) {
    c = (long long)f[n - 2] * g[n - 2 - i] % MOD;
    c = (long long)c * f[m - 1] % MOD;
    c = (long long)c * g[i] % MOD;
    c = (long long)c * g[m - 1 - i] % MOD;
    c = (long long)c * a[n - 2 - i] % MOD;
    c = (long long)c * (i + 2) % MOD;
    c = (long long)c * b[n - 2 - i] % MOD;
    c = (long long)c * f[n - 1] % MOD;
    c = (long long)c * g[n] % MOD;
    ans = (ans + c) % MOD;
  }
  printf("%d\n", ans);
  return 0;
}
