#ifndef HIDDEN_IN_VISUAL_STUDIO // 折りたたみ用 // 警告の抑制 #define _CRT_SECURE_NO_WARNINGS // ライブラリの読み込み #include using namespace std; // 型名の短縮 using ll = long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9) using pii = pair; using pll = pair; using pil = pair; using pli = pair; using vi = vector; using vvi = vector; using vvvi = vector; using vl = vector; using vvl = vector; using vvvl = vector; using vb = vector; using vvb = vector; using vvvb = vector; using vc = vector; using vvc = vector; using vvvc = vector; using vd = vector; using vvd = vector; using vvvd = vector; template using priority_queue_rev = priority_queue, greater>; using Graph = vvi; // 定数の定義 const double PI = acos(-1); const vi dx4 = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左) const vi dy4 = { 0, 1, 0, -1 }; const vi dx8 = { 1, 1, 0, -1, -1, -1, 0, 1 }; // 8 近傍 const vi dy8 = { 0, 1, 1, 1, 0, -1, -1, -1 }; const int INF = 1001001001; const ll INFL = 4004004004004004004LL; const double EPS = 1e-12; // 許容誤差に応じて調整 // 入出力高速化 struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } fastIOtmp; // 汎用マクロの定義 #define all(a) (a).begin(), (a).end() #define sz(x) ((int)(x).size()) #define distance (int)distance #define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");} #define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順 #define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順 #define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順 #define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能) #define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能) #define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順) #define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順) #define repit(it, a) for(auto it = (a).begin(); it != (a).end(); ++it) // イテレータを回す(昇順) #define repitr(it, a) for(auto it = (a).rbegin(); it != (a).rend(); ++it) // イテレータを回す(降順) #define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod #define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去 #define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了 // 汎用関数の定義 template inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; } template inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す) template inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す) // 演算子オーバーロード template inline istream& operator>> (istream& is, pair& p) { is >> p.first >> p.second; return is; } template inline istream& operator>> (istream& is, vector& v) { repea(x, v) is >> x; return is; } template inline vector& operator--(vector& v) { repea(x, v) --x; return v; } template inline vector& operator++(vector& v) { repea(x, v) ++x; return v; } // 手元環境(Visual Studio) #ifdef _MSC_VER #define popcount (int)__popcnt // 全ビット中の 1 の個数 #define popcountll (int)__popcnt64 inline int lsb(unsigned int n) { unsigned long i; _BitScanForward(&i, n); return i; } // 最下位ビットの位置(0-indexed) inline int lsbll(unsigned long long n) { unsigned long i; _BitScanForward64(&i, n); return i; } inline int msb(unsigned int n) { unsigned long i; _BitScanReverse(&i, n); return i; } // 最上位ビットの位置(0-indexed) inline int msbll(unsigned long long n) { unsigned long i; _BitScanReverse64(&i, n); return i; } template T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } // 提出用(gcc) #else #define popcount (int)__builtin_popcount #define popcountll (int)__builtin_popcountll #define lsb __builtin_ctz #define lsbll __builtin_ctzll #define msb(n) (31 - __builtin_clz(n)) #define msbll(n) (63 - __builtin_clzll(n)) #define gcd __gcd #endif // デバッグ用 #ifdef _MSC_VER #include "debug.hpp" #else #define dump(...) #define dumpel(v) #define input_from_file(f) #define output_to_file(f) #endif #endif // 折りたたみ用 //-----------------AtCoder 専用----------------- #include using namespace atcoder; //using mint = modint1000000007; using mint = modint998244353; //using mint = modint; // mint::set_mod(m); istream& operator>> (istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; } ostream& operator<< (ostream& os, const mint& x) { os << x.val(); return os; } using vm = vector; using vvm = vector; using vvvm = vector; //---------------------------------------------- mint naive_kubaru(int n, int m) { vvvm dp(n + 1, vvm(m + 1, vm(n + 1))); repi(j, 1, m) dp[1][j][1] = 1; repi(i, 1, n) { repi(j, 1, m) { repi(k, 1, n) { if (j <= k) { dp[i][j][k] = 0; continue; } if (i == n) continue; if (k < n) { dp[i + 1][j][k + 1] += dp[i][j][k]; } repi(j2, 1, m) { if (j2 == j) continue; dp[i + 1][j2][1] += dp[i][j][k]; } } } } dumpel(dp); mint res = mint(m).pow(n); repi(j, 1, m) { repi(k, 1, n) { res -= dp[n][j][k]; } } return res; } mint naive_morau(int n, int m) { vvvm dp(n + 1, vvm(m + 1, vm(n + 1))); repi(j, 1, m) dp[1][j][1] = 1; repi(i, 1, n) { repi(j, 1, m) { repi(k, 1, n) { if (j <= k) { dp[i][j][k] = 0; continue; } if (i == 1) continue; if (k > 1) { dp[i][j][k] = dp[i - 1][j][k - 1]; } else { repi(j2, 1, m) { if (j2 == j) continue; repi(k2, 1, n) { dp[i][j][k] += dp[i - 1][j2][k2]; } } } } } } dumpel(dp); mint res = mint(m).pow(n); repi(j, 1, m) { repi(k, 1, n) { res -= dp[n][j][k]; } } return res; } mint acc_morau(int n, int m) { vvvm dp(n + 1, vvm(m + 1, vm(n + 1))); repi(j, 1, m) dp[1][j][1] = 1; dp[1][1][1] = 0; vvm sum_j(n + 1, vm(m + 1)); vm sum(n + 1); repi(j, 1, m) sum_j[1][j] = 1; sum_j[1][1] = 0; sum[1] = m - 1; repi(i, 2, n) { repi(j, 1, m) { repi(k, 1, n) { if (j <= k) { dp[i][j][k] = 0; continue; } if (k > 1) { dp[i][j][k] = dp[i - 1][j][k - 1]; } else { dp[i][j][k] += sum[i - 1] - sum_j[i - 1][j]; } sum_j[i][j] += dp[i][j][k]; sum[i] += dp[i][j][k]; } } } dumpel(dp); mint res = mint(m).pow(n); repi(j, 1, m) { repi(k, 1, n) { res -= dp[n][j][k]; } } return res; } mint acc_morau2(int n, int m) { // dp[i][j][k] : A[0..i) で直前に j が i - k 個並んでいる vvvm dp(n + 1, vvm(m + 1, vm(n + 1))); repi(j, 1, m) dp[1][j][1 - 1] = 1; dp[1][1][0] = 0; vvm sum_j(n + 1, vm(m + 1)); vm sum(n + 1); repi(j, 1, m) sum_j[1][j] = 1; sum_j[1][1] = 0; sum[1] = m - 1; repi(i, 2, n) { repi(j, 1, m) { repi(k, 1, min(i, n)) { if (j <= k) { dp[i][j][i - k] = 0; continue; } if (k > 1) { dp[i][j][i - k] = dp[i - 1][j][(i - 1) - (k - 1)]; } else { dp[i][j][i - k] += sum[i - 1] - sum_j[i - 1][j]; } sum_j[i][j] += dp[i][j][i - k]; sum[i] += dp[i][j][i - k]; } } } dumpel(dp); mint res = mint(m).pow(n); repi(j, 1, m) { repi(k, 1, n) { res -= dp[n][j][n - k]; } } return res; } mint acc_morau3(int n, int m) { // dp_i[j][k] : A[0..i) で直前に j が i - k 個並んでいる vvm dp(m + 1, vm(n + 1)); repi(j, 1, m) dp[j][1 - 1] = 1; dp[1][0] = 0; vvm sum_j(n + 1, vm(m + 1)); vm sum(n + 1); repi(j, 1, m) sum_j[1][j] = 1; sum_j[1][1] = 0; sum[1] = m - 1; repi(i, 2, n) { repi(j, 1, m) { repi(k, 1, min(i, n)) { if (j <= k) { dp[j][i - k] = 0; continue; } if (k > 1) { ; } else { dp[j][i - k] += sum[i - 1] - sum_j[i - 1][j]; } sum_j[i][j] += dp[j][i - k]; sum[i] += dp[j][i - k]; } } dump(dp); dump(sum); } mint res = mint(m).pow(n); repi(j, 1, m) { repi(k, 1, n) { res -= dp[j][n - k]; } } return res; } mint acc_morau4(int n, int m) { // dp_i[j][k] : A[0..i) で直前に j が i - k 個並んでいる vvm dp(m + 1, vm(n + 1)); repi(j, 1, m) dp[j][1 - 1] = 1; dp[1][0] = 0; vvm sum_j(n + 1, vm(m + 1)); vm sum(n + 1); repi(j, 1, m) sum_j[1][j] = 1; sum_j[1][1] = 0; sum[1] = m - 1; repi(i, 2, n) { sum[i] = sum[i - 1]; repi(j, 1, m) { sum_j[i][j] = sum_j[i - 1][j]; if (i - j >= 0) { sum_j[i][j] -= dp[j][i - j]; sum[i] -= dp[j][i - j]; dp[j][i - j] = 0; } if (j != 1) { sum_j[i][j] -= sum[i - 1] - sum_j[i - 1][j]; sum[i] -= sum[i - 1] - sum_j[i - 1][j]; dp[j][i - 1] += sum[i - 1] - sum_j[i - 1][j]; } sum_j[i][j] += dp[j][i - 1]; sum[i] += dp[j][i - 1]; } dump(dp); dump(sum); } mint res = mint(m).pow(n); repi(j, 1, m) { repi(k, 1, n) { res -= dp[j][n - k]; } } return res; } int main() { // input_from_file("input.txt"); // output_to_file("output.txt"); int n, m; cin >> n >> m; // cout << naive_kubaru(n, m) << endl; // cout << naive_morau(n, m) << endl; // cout << acc_morau(n, m) << endl; // cout << acc_morau2(n, m) << endl; cout << acc_morau3(n, m) << endl; // cout << acc_morau4(n, m) << endl; }