#include <iostream>
using namespace std;

long long mod = 998244353;
long long dp[5009][3];
long long d2[5009][3];
long long N, M;

long long solve(int border) {
	for (int i = 0; i <= N; i++) {
		dp[i][0] = 0; d2[i][0] = 0;
		dp[i][1] = 0; d2[i][1] = 0;
		dp[i][2] = 0; d2[i][2] = 0;
	}

	dp[0][0] = 1;
	d2[0][0] = 0;
	for (int i = 1; i <= N; i++) {
		long long r[3] = { border, M - border - 1, 1 };
		for (int j = 0; j < 3; j++) {
			for (int k = 0; k < 3; k++) {
				if (j == 2 && k == 1) continue;
				if (j == 2 && k == 2) {
					dp[i][k] += 1LL * (r[1] + r[2]) * dp[i - 1][j];
					d2[i][k] += 1LL * (r[1] + r[2]) * d2[i - 1][j];
				}
				else {
					dp[i][k] += 1LL * r[k] * dp[i - 1][j];
					d2[i][k] += 1LL * r[k] * d2[i - 1][j];
				}
				if (j == 2 && k == 0) {
					d2[i][k] += 1LL * r[k] * dp[i - 1][j];
				}
			}
		}
		for (int j = 0; j < 3; j++) dp[i][j] %= mod;
		for (int j = 0; j < 3; j++) d2[i][j] %= mod;
	}
	return (d2[N][0] + d2[N][1] + d2[N][2] + dp[N][2]) % mod;
}

int main() {
	cin >> N >> M;

	long long Answer = 0;
	for (int i = 0; i <= M - 1; i++) {
		long long r = solve(i);
		Answer += r;
		Answer %= mod;
	}
	cout << Answer << endl;
	return 0;
}
#include<iostream>
#include<vector>
using namespace std;
typedef long long LL;
const int N=1e5+10,mod=998244353;
LL Pow[N];
LL f[N];
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	Pow[0]=1;
	for(int i=1;i<=n;i++){
		Pow[i]=Pow[i-1]*m%mod;
	}
	for(int i=1;i<=m;i++)
	f[i]=1;
	LL ans=m,pre=0;
	for(int i=2;i<=n;i++){
		pre=ans;
		ans=0;
		if(i>2)
		for(int j=1;j<=m;j++){
			f[j]=(f[j]*(m-j)%mod+Pow[i-2])%mod;
		}
		for(int j=1;j<=m;j++){
			ans=(ans+pre+Pow[i-1]-f[j]+mod)%mod;
		}
	}
	cout<<ans;
	return 0;
}






