#line 1 "..\\Main.cpp" #include #include #include #include #include #include #line 2 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\linear-modulo\\matrix-modulo.hpp" #line 4 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\linear-modulo\\matrix-modulo.hpp" #include #include namespace nachia{ template struct MatrixModulo{ private: int h; int w; std::vector elems; public: MatrixModulo(int new_h=0, int new_w=0){ h = new_h; w = new_w; elems.assign(h * w, 0); } MatrixModulo(const MatrixModulo &) = default; int numRow() const { return h; } int numColumn() const { return w; } int height() const { return numRow(); } int width() const { return numColumn(); } typename std::vector::iterator operator[](int y){ return elems.begin() + (y * w); } typename std::vector::const_iterator operator[](int y) const { return elems.begin() + (y * w); } static MatrixModulo Identity(int idx){ auto res = MatrixModulo(idx, idx); for(int i = 0; i < idx; i++) res[i][i] = 1; return res; } void swapColumns(int x1, int x2){ assert(0 <= x1 && x1 < numColumn()); assert(0 <= x2 && x2 < numColumn()); for(int y=0; y=i; k--) g[j][k] -= g[j][i] * g[i][k]; } return ans; } int rank() const { MatrixModulo g = *this; int y = 0; for (int d=0; d=d; j--) g[i][j] -= g[i][d] * g[y][j]; y++; } return y; } MatrixModulo pow(unsigned long long i){ auto a = *this; auto res = Identity(height()); while(i){ if(i % 2 == 1) res = res * a; a = a * a; i /= 2; } return res; } }; } // namespace nachia #line 9 "..\\Main.cpp" using Modint = atcoder::static_modint<998244353>; using namespace std; using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i<(int)(n); i++) const i64 INF = 1001001001001001001; pair solve(i64 N, i64 M, i64 K, i64 Q = 1){ if(N == 0) return { 1, 0 }; i64 g = std::gcd(M, K); if(g == 1){ auto mat = nachia::MatrixModulo(2,2); mat[0][0] = Modint(Q-1); mat[0][1] = Modint(Q); mat[1][0] = Modint(M-1)*(Q); mat[1][1] = Modint(M-2)*(Q) + Modint(Q-1); mat = mat.pow(N); // cout << "solve(" << N << ", " << M << ", " << K << ", " << Q << ") = "; // cout << "(" << mat[0][0].val() << ", " << (mat[0][1] * (M-1)).val() << ")" << endl; return make_pair(mat[0][0], mat[0][1] * (M-1)); } auto [c0, c1] = solve(N-1, M/g, K, Q*g); Modint a0 = (c1 * 1) * Q + c0 * (Q-1); Modint a1 = (c0 * (M-1) + c1 * (M - 2)) * Q + c1 * (Q-1); // cout << "solve(" << N << ", " << M << ", " << K << ", " << Q << ") = "; // cout << "(" << a0.val() << ", " << a1.val() << ")" << endl; return { a0, a1 }; } void testcase(){ i64 N, M, K; cin >> N >> M >> K; cout << solve(N,M,K).first.val() << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); testcase(); return 0; }