import java.io.*;
import java.util.*;
public class Main {
public static void main
(String[] args
) { InputReader in
= new InputReader
(System.
in); OutputWriter out
= new OutputWriter
(System.
out); TaskC solver = new TaskC(in, out);
solver.solve();
out.close();
}
static class TaskC {
static int n;
static int m;
static int[] inv;
static int[] fact;
static int[] invfact;
static int[] powN;
static int[] powM;
InputReader in;
OutputWriter out;
TaskC(InputReader in, OutputWriter out) {
this.in = in;
this.out = out;
}
private void solve() {
n = in.readInt();
m = in.readInt();
precalc();
int ans = 0;
int choosePath = 1;
for(int k = 1; k < n; ++k) {
if (k > m) break;
int cur = MathUtil.mul(choosePath, cayley(n, k + 1));
cur = MathUtil.mul(cur, binom(m - 1, k - 1));
cur = MathUtil.mul(cur, powM[n - k - 1]);
choosePath = MathUtil.mul(choosePath, n - k - 1);
ans = MathUtil.sum(ans, cur);
}
out.print(ans);
}
private int cayley(int n, int k) {
if (n - k - 1 < 0) {
return MathUtil.mul(k, MathUtil.binPow(n, MathUtil.mod - 2));
}
return MathUtil.mul(k, powN[n - k - 1]);
}
private int binom(int n, int k) {
return MathUtil.mul(fact[n], MathUtil.mul(invfact[k], invfact[n - k]));
}
private void precalc() {
powN = new int[n + 1];
powM = new int[n + 1];
powN[0] = 1;
powM[0] = 1;
for(int i = 1; i <= n; ++i) {
powN[i] = MathUtil.mul(powN[i - 1], n);
powM[i] = MathUtil.mul(powM[i - 1], m);
}
inv = new int[m + 1];
fact = new int[m + 1];
invfact = new int[m + 1];
inv[0] = inv[1] = 1;
fact[0] = fact[1] = 1;
invfact[0] = invfact[1] = 1;
for(int i = 2; i <= m; ++i) {
inv[i] = MathUtil.mul(inv[MathUtil.mod % i], MathUtil.mod - MathUtil.mod / i);
fact[i] = MathUtil.mul(fact[i - 1], i);
invfact[i] = MathUtil.mul(invfact[i - 1], inv[i]);
}
}
}
static class MathUtil {
private static final int mod = 1_000_000_007;
static int binPow(int a, int n) {
int result = 1;
while (n > 0) {
if (n % 2 == 1) {
result = mul(result, a);
}
n /= 2;
a = mul(a, a);
}
return result;
}
static int mul(int a, int b) {
return (int)((long)a * b % mod);
}
static int sum(int a, int b) {
int result = a + b;
if (result >= mod) result -= mod;
return result;
}
}
static class OutputWriter {
}
public OutputWriter
(Writer writer
) { }
public void print
(Object...
objects) { for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
writer.println();
}
public void close() {
writer.close();
}
}
static class InputReader {
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public double readDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res
* Math.
pow(10, readInt
()); }
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res
* Math.
pow(10, readInt
()); }
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}