#include <bits/stdc++.h>
#define N 5015
#define mod 998244353

using namespace std;

typedef long long ll;

int n, m;
ll sum[N], power[N][N];

ll ksm(int a, int b) {
	return power[a][b];
}

void Init() {
	for (int i = 0; i <= m+2; i++) {
		power[i][0] = 1;
		for (int j = 1; j <= n+2; j++) {
			power[i][j] = (power[i][j - 1] * i) % mod;
		}
	}
}

int main() {
	scanf("%d%d", &n, &m);
	Init();
	for (int j = 0; j <= n + 5; j++) {
		for (int i = 0; i < m; i++) {
			sum[j] = (sum[j] + ksm(i, j)) % mod;
		}
	}
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		ll res = 0;
		res = (res + (m * (ans + ksm(m - 1, i - 1)) % mod) % mod) % mod;
		for (int j = i - 1; j >= 1; j--) {
			res = (res % mod + ((ksm(m - 1, i - j - 1) * ksm(m, j - 1)) % mod * m)) % mod;
			res = ((res - (ksm(m, j - 1) * sum[i - j - 1]) % mod) % mod + mod) % mod;
		}
		ans = res;
	}
	cout << ans % mod << '\n';
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define forin(in ,n) for(ll i=0; i<n; i++) cin>>in[i]
#define forout(out) for(ll i=0; i<(ll)out.size(); i++) cout<<out[i]<<endl
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep_up(i, a, n) for (ll i = a; i < n; ++i)
#define rep_down(i, a, n) for (ll i = a; i >= n; --i)
#define P pair<ll, ll>

#define all(v) v.begin(), v.end()
#define fi first
#define se second
#define vvvll vector<vector<vector<ll>>>
#define vvll vector<vector<ll>>
#define vll vector<ll>
#define pqll priority_queue<ll>
#define pqllg priority_queue<ll, vector<ll>, greater<ll>>

constexpr ll INF = (1ll << 60);
// constexpr ll mod = 1000000007;
constexpr ll mod = 998244353;
constexpr double pi = 3.14159265358979323846;
template <typename T>
inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
void pt(T val) {
    cout << val << "\n";
}
template <typename T>
void pt_vll(vector<T> &v) {
    ll vs = v.size();
    rep(i, vs) {
        cout << v[i];

        if (i == vs - 1)
            cout << "\n";
        else
            cout << " ";
    }
}
ll mypow(ll a, ll n) {
    ll ret = 1;
    if(n==0) return 1;
    if(a==0) return 0;
    rep(i, n) {
        if (ret > (ll)(1e18 + 10) / a) return -1;
        ret *= a;
    }
    return ret;
}
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
ll dig(ll n) {//桁数
    ll res = 0;
    while(n > 0) {
        res ++;
        n /= 10;
    }
    return res;
}
vector<bool> seen;
void dfs(vvll &G, ll v){
    seen[v]=true;
    for(auto next:G[v]){
        if(seen[next]) continue;
        dfs(G,next);
    }
}

int main() {
    ll n,m,x,ans=1;
    cin>>n>>m;
    vll A(n);
    vll X(m+1,1);
    rep(i,n){
        if(i==0){
            A[0]=m;
            continue;
        }
        ll sum=0;
        rep(j,m){
            A[i]+=mod+A[i-1]+modpow(m,i,mod)-X[j];
            A[i]%=mod;
            X[j]=(m-j-1)*X[j]+modpow(m,i,mod);
            X[j]%=mod;
        }
    }
    cout<<A[n-1]%mod<<endl;
}