#include<bits/stdc++.h>
using namespace std;
#define int long long
#define reg register
#define x1 x11
#define x2 x22
#define y1 y11
#define y2 y22
#define z1 z11
#define z2 z22
const int mod=998244353;
const int maxn=6e5+5;
const int INF=2e18;
const double Pi=acos(-1.0);
const double Exp=1e-6;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0' || ch>'9')
	{
		if(ch=='-')
		f=-1;
		ch=getchar();
	}
	while(ch>='0' && ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
int n,m;
int mul(int x,int y,int mod)
{
	int res=1;
	while(y)
	{
		if(y & 1)
		res=res*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return res%mod;
}
int pre[maxn];
int ans=0;
signed main()
{
	pre[0]=1;
	for(int i=1;i<=5000;i++)
	pre[i]=pre[i-1]*i%mod;
	n=read(),m=read();
	for(int len=1;len<=n-1;len++)
	{
		int cnt=n-len-1;
		int wns=mul(m,cnt,mod)%mod;
		wns=wns*(n-len)%mod;
		int res=0;
		for(int i=2;i<=m;i++)
		{
			res=(res+(i-1)*mul(m-i,len-1,mod)%mod)%mod;
		}
		wns=wns*res%mod;
		ans=(ans+wns)%mod;
	}
//	cout<<"ans="<<ans<<endl;
	for(int i=1;i<=n;i++)
	{
		int cnt=n-i;
		int wns=mul(m,cnt,mod)%mod;
		int res=0;
		for(int j=1;j<=m;j++)
		{
			res=(res+mul(m-j,i-1,mod)%mod)%mod;
		}
		wns=wns*res%mod;
		ans=(ans+wns)%mod;
	}
	printf("%lld\n",ans);
	return 0;
}
/*

2 2 4
3 2
5 6

*/
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

using namespace __gnu_pbds;
using namespace std;

using ll = long long;
using ld = long double;

typedef tree<
        int,
        null_type,
        less<int>,
        rb_tree_tag,
        tree_order_statistics_node_update>
        ordered_set;

#define mp make_pair


const int MOD =  998244353;

int mul(int a, int b) {
    return (1LL * a * b) % MOD;
}

int add(int a, int b) {
    int s = (a+b);
    if (s>=MOD) s-=MOD;
    return s;
}

int sub(int a, int b) {
    int s = (a+MOD-b);
    if (s>=MOD) s-=MOD;
    return s;
}

int po(int a, ll deg)
{
    if (deg==0) return 1;
    if (deg%2==1) return mul(a, po(a, deg-1));
    int t = po(a, deg/2);
    return mul(t, t);
}

int inv(int n)
{
    return po(n, MOD-2);
}


mt19937 rnd(time(0));

/*
const int LIM = 1e6 + 5;

vector<int> facs(LIM), invfacs(LIM);

void init()
{
    facs[0] = 1;
    for (int i = 1; i<LIM; i++) facs[i] = mul(facs[i-1], i);
    invfacs[LIM-1] = inv(facs[LIM-1]);
    for (int i = LIM-2; i>=0; i--) invfacs[i] = mul(invfacs[i+1], i+1);
}

int C(int n, int k)
{
    if (n<k) return 0;
    if (n<0 || k<0) return 0;
    return mul(facs[n], mul(invfacs[k], invfacs[n-k]));
}*/


/*struct DSU
{
    vector<int> sz;
    vector<int> parent;
    void make_set(int v) {
        parent[v] = v;
        sz[v] = 1;
    }

    int find_set(int v) {
        if (v == parent[v])
            return v;
        else
        {
            auto par = find_set(parent[v]);
            parent[v] = par;
            return par;
        }
    }

    void union_sets(int a, int b) {
        find_set(a);
        find_set(b);

        a = find_set(a);
        b = find_set(b);

        if (a != b) {
            if (sz[a] < sz[b])
                swap(a, b);
            parent[b] = a;
            sz[a] += sz[b];
        };
    }

    DSU (int n)
    {
        parent.resize(n);
        sz.resize(n);
        for (int i = 0; i<n; i++) make_set(i);
    }
};*/



int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);

    int n, m; cin>>n>>m;

    vector<vector<int>> degs(m+1, vector<int>(n+1));

    for (int i = 1; i<=m; i++)
    {
        degs[i][0] = 1;
        for (int j = 1; j<=n; j++) degs[i][j] = mul(degs[i][j-1], i);
    }

    ll tot = mul(n, degs[m][n]);
    for (int len = 1; len<=n-1; len++)
    {
        int sum = 0;
        for (int val = 1; val<=m; val++)
        {
            if (val==m && len>1) continue;
            //probability = 1/m^2 + (m - val)^(len-1)
            int cnt = degs[m][n - len - 1];
            if (len>=2) cnt = mul(cnt, degs[m - val][len-1]);
            sum = add(sum, cnt);
        }
        tot = sub(tot, mul(sum, n-len));
    }

    cout<<tot;

}

/*
f(n, m) = f(n, m-1) + //Case when there are several ones



 */