#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; #define fst first #define snd second /* clang-format off */ template struct _vec { using type = vector::type>; }; template struct _vec { using type = T; }; template using vec = typename _vec::type; template vector make_v(size_t size, const T &init) { return vector(size, init); } template auto make_v(size_t size, Ts... rest) { return vector(size, make_v(rest...)); } template inline void chmin(T &a, const T &b) { if (b < a) a = b; } template inline void chmax(T &a, const T &b) { if (b > a) a = b; } /* clang-format on */ //@formatter:off //constexpr int MOD = 998244353; struct mint { int x; mint() : x(0) {} mint(int a) { x = a % MOD; if (x < 0) x += MOD; } mint &operator+=(mint that) { x = (x + that.x) % MOD; return *this; } mint &operator-=(mint that) { x = (x + MOD - that.x) % MOD; return *this; } mint &operator*=(mint that) { x = (int) x * that.x % MOD; return *this; } mint &operator/=(mint that) { return *this *= that.inverse(); } mint operator-() { return mint(-this->x); } friend ostream &operator<<(ostream &out, mint m) { return out << m.x; } mint inverse() { int a = x, b = MOD, u = 1, v = 0; while (b) { int t = a / b; a -= t * b; u -= t * v; swap(a, b); swap(u, v); } return mint(u); }mint operator+(mint that) { return mint(*this) += that; } mint operator-(mint that) { return mint(*this) -= that; } mint operator*(mint that) { return mint(*this) *= that; } mint operator/(mint that) { return mint(*this) /= that; } bool operator ==(mint that) const { return x == that.x; } bool operator !=(mint that) const { return x != that.x; } bool operator <(mint that) const { return x < that.x; } bool operator <=(mint that) const { return x <= that.x; } bool operator >(mint that) const { return x > that.x; } bool operator >=(mint that) const { return x >= that.x; }}; istream &operator>>(istream &i, mint &a) { i >> a.x; return i;} typedef vector vm; vector fac, finv; int mint_len = 1400000; //既に変更されていたら変えない mint com(int a, int b) { if (a < 0) return 0; if (b < 0 || b > a) return 0; return fac[a] * finv[a - b] * finv[b];} mint hom(int a, int b) { return com(a + b - 1, b);} template mint mpow(const T a, const U b) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res;} template inline mint mpow(const T a, const mint b) { int x = a, res = 1; int p = b.x; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res;} using PM = pair; using vm = vector; #define vvm(...) o_vvt(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(mint,__VA_ARGS__) #define smod setmod //setmodを呼ぶ @formatter:on typedef vector Vec; typedef vector Mat; Mat eye(int n) { Mat I(n, Vec(n)); for (int i = 0; i < n; ++i) I[i][i] = 1; return I; } Mat mul(Mat A, const Mat &B) { for (int i = 0; i < A.size(); ++i) { Vec x(A[0].size()); for (int k = 0; k < B.size(); ++k) for (int j = 0; j < B[0].size(); ++j) x[j] += A[i][k] * B[k][j]; A[i].swap(x); } return A; } Mat pow(Mat A, ll k) { Mat X = eye(A.size()); for (; k > 0; k /= 2) { if (k & 1) X = mul(X, A); A = mul(A, A); } return X; } Vec mul(Mat &A, Vec &b) { Vec y(A.size()); for (int i = 0; i < A.size(); ++i) for (int j = 0; j < A[0].size(); ++j) y[i] += A[i][j] * b[j]; return y; } ll solve(int N, int K) { int dim = K * K * K; auto encode = [&](int a, int b, int c) { return a * K * K + b * K + c; }; Mat A = make_v(dim, dim, mint(0)); for (int a = 0; a < K; a++) { for (int b = 0; b < K; b++) { for (int c = 0; c < K; c++) { int cur = encode(a, b, c); A[encode((a + 1) % K, b, c)][cur] += 1; A[encode(a, (a + b) % K, c)][cur] += 1; A[encode(a, b, (b + c) % K)][cur] += 1; } } } A = pow(A, N); Vec init(dim); init[0] = 1; auto goal = mul(A, init); mint res = 0; for (int a = 0; a < K; a++) { for (int b = 0; b < K; b++) { res += goal[encode(a, b, 0)]; } } return res.x; } int main() { #ifdef DEBUG ifstream ifs("in.txt"); cin.rdbuf(ifs.rdbuf()); #endif int N, K; while (cin >> N >> K) { cout << solve(N, K) << endl; } return 0; }