#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 */ template class modint { using u64 = std::uint_fast64_t; public: u64 a; constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) { } constexpr u64 &value() noexcept { return a; } constexpr const u64 &value() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } constexpr modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } constexpr modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } constexpr modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } constexpr modint &operator/=(const modint rhs) noexcept { return *this *= ~rhs; } constexpr modint operator^(u64 exp) const noexcept { modint v = 1, x = *this; while (exp) { if (exp & 1) { v *= x; } x *= x; exp >>= 1; } return v; } constexpr modint operator~() const noexcept { return *this ^ (Modulus - 2); } }; using mint = modint<998244353>; 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(const Mat& A, const 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 solveNaive(int N, int K) { vec dp = make_v(K, K, K, mint(0)); dp[0][0][0] = 1; for (int t = 0; t < N; t++) { vec ndp = make_v(K, K, K, mint(0)); for (int a = 0; a < K; a++) { for (int b = 0; b < K; b++) { for (int c = 0; c < K; c++) { mint cur = dp[a][b][c]; ndp[(a + 1) % K][b][c] += cur; ndp[a][(a + b) % K][c] += cur; ndp[a][b][(b + c) % K] += cur; } } } dp = ndp; } mint res = 0; for (int a = 0; a < K; a++) for (int b = 0; b < K; b++) res += dp[a][b][0]; return res.value(); } 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.value(); } 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; }