#include <bits/stdc++.h>
using namespace std;
namespace IO {
template <typename T>
inline void read(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  x *= f;
  return;
}
template <typename T>
inline void write(T x = 0) {
  T wr[51];
  wr[0] = 0;
  if (x < 0) putchar('-'), x = -x;
  if (!x) putchar(48);
  while (x) wr[++wr[0]] = x % 10, x /= 10;
  while (wr[0]) putchar(48 + wr[wr[0]--]);
  return;
}
}  // namespace IO
using IO::read;
using IO::write;
const int N = 3e6 + 11, mod = 1e9 + 7;
int fac[N], ifac[N];
int n, m, a, b;
inline int fp(int a, int b) {
  if (b < 0) b += mod - 1;
  int res = 1;
  for (; b; b >>= 1, a = 1ll * a * a % mod)
    if (b & 1) res = 1ll * res * a % mod;
  return res;
}
inline void init(int n) {
  fac[0] = 1;
  for (register int i = 1; i <= n; ++i) fac[i] = 1ll * i * fac[i - 1] % mod;
  ifac[n] = fp(fac[n], mod - 2);
  for (register int i = n; i >= 1; --i) ifac[i - 1] = 1ll * i * ifac[i] % mod;
}
inline int C(int n, int m) {
  if (n < m) return 0;
  return 1ll * fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}
int main() {
  init(2e6);
  read(n), read(m);
  int ans = 0;
  for (register int i = 0; i <= n - 2; ++i)
    ans = (ans + 1ll * C(n - 2, i) * fac[i] % mod * C(m - 1, i) % mod *
                     fp(n, n - i - 3) % mod * (i + 2) % mod * fp(m, n - 2 - i) %
                     mod) %
          mod;
  printf("%d\n", ans);
  return 0;
}
