#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define rep(i, a, b) for(int i = a; i <= b; ++i)
#define per(i, a, b) for(int i = b; i >= a; --i)

using ll = long long;
using namespace std;

const int N = 5005;
const int P = 998244353;

int n, m; 
ll ans, dp[N];

ll pw(ll a, ll b) {
	ll c = 1;
	for (; b; b /= 2, a = a * a % P)
		if (b & 1) c = c * a % P;
	return c;
}

int main() {
	scanf("%d%d", &n, &m);
	ans = n * pw(m, n) % P;
	rep(len, 2, n)
		rep(x, 1, m)
			dp[len] = (dp[len] + pw(m - x, len - 2)) % P;
	rep(i, 1, n)
		rep(j, i + 1, n)
			ans = (ans - dp[j - i + 1] * pw(m, n - (j - i + 1)) % P + P) % P;
	printf("%lld\n", ans);
	return 0;
}

#include<bits/stdc++.h>
#define pi 3.141592653589793238
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define MOD 1000000007
#define INF 999999999999999999 
#define pb push_back
#define ff first
#define ss second
 
#define mt make_tuple
#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
 
 
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const int N = 5e3 + 1;
ll n, m; 
ll p[N][N];
ll d[N][N];
ll s[N];
int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    fast;
    ll T = 1, i, j;
    
    //cin >> T;
    while (T--) {
        ll mod = 998244353;
        cin >> n >> m;
        if(m == 1){
            cout << 1 << endl;
            return 0;
        }
        p[0][0] = 1;
        for(i = 1; i <= m; i++){
            p[i][0] = 1;
            for(j = 1; j <= n; j++){
                p[i][j] = (p[i][j - 1] * i) % mod;
            }
        }
        for(i = 2; i <= n; i++){
            for(j = 1; j <= m; j++){
                d[i][j] = p[m - 1][i - 2] - p[m - j][i - 2];
                d[i][j] %= mod;
                d[i][j] += mod;
                d[i][j] %= mod;
            }
        }
        for(i = 1; i <= m; i++){
            for(j = 2; j <= n; j++){
                s[j] += d[j][i];
                s[j] %= mod;
            }
        }
        vector<ll> dp(n + 1);
        dp[1] = m;
        for(i = 2; i <= n; i++){
            //cout << i << "##\n";
            dp[i] += m * dp[i - 1];
            //cout << dp[i] << endl;
            dp[i] %= mod;
            dp[i] += m * (p[m - 1][i - 1]);
            dp[i] %= mod;
            // for(j = 1; j <= m; j++){
            //     dp[i] += (p[m - 1][i - 1]);
            //     ll l = 0;
            //     for(ll k = i - 1; k >= 1; k--){
            //         dp[i] += (p[m][k - 1]) * (p[m - 1][l] - p[m - j][l]);
            //         dp[i] %= mod;
            //         l++;
            //     }
            //     //cout << dp[i] << endl;
            // }
            for(j = i - 1; j >= 1; j--){
                dp[i] += p[m][j - 1] * s[i - j + 1];
                dp[i] %= mod;
            }
        }
        cout << dp[n] << endl;
    }
    return 0;
}