#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Int = long long; template ostream &operator<<(ostream &os, const vector &as); template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template ostream &operator<<(ostream &os, const vector &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } #define COLOR(s) ("\x1b[" s "m") //////////////////////////////////////////////////////////////////////////////// struct ModLong { static unsigned long long M; static long double INV_M; static void setM(unsigned long long m) { M = m; INV_M = 1.0L / M; } unsigned long long x; ModLong() : x(0ULL) {} ModLong(unsigned x_) : x(x_ % M) {} ModLong(unsigned long long x_) : x(x_ % M) {} ModLong(int x_) : x(((x_ %= static_cast(M)) < 0) ? (x_ + static_cast(M)) : x_) {} ModLong(long long x_) : x(((x_ %= static_cast(M)) < 0) ? (x_ + static_cast(M)) : x_) {} ModLong &operator+=(const ModLong &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; } ModLong &operator-=(const ModLong &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; } ModLong &operator*=(const ModLong &a) { // https://github.com/kth-competitive-programming/kactl/blob/main/doc/modmul-proof.pdf // This works at least for M < 2^62, assuming the usual 80-bit long double. const long long y = x * a.x - M * static_cast(INV_M * x * a.x); x = (y < 0LL) ? (y + M) : (y >= static_cast(M)) ? (y - M) : y; return *this; } ModLong &operator/=(const ModLong &a) { return (*this *= a.inv()); } ModLong pow(long long e) const { if (e < 0) return inv().pow(-e); ModLong a = *this, b = 1ULL; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b; } ModLong inv() const { unsigned long long a = M, b = x; long long y = 0, z = 1; for (; b; ) { const unsigned long long q = a / b; const unsigned long long c = a - q * b; a = b; b = c; const long long w = y - static_cast(q) * z; y = z; z = w; } assert(a == 1ULL); return ModLong(y); } ModLong operator+() const { return *this; } ModLong operator-() const { ModLong a; a.x = x ? (M - x) : 0ULL; return a; } ModLong operator+(const ModLong &a) const { return (ModLong(*this) += a); } ModLong operator-(const ModLong &a) const { return (ModLong(*this) -= a); } ModLong operator*(const ModLong &a) const { return (ModLong(*this) *= a); } ModLong operator/(const ModLong &a) const { return (ModLong(*this) /= a); } template friend ModLong operator+(T a, const ModLong &b) { return (ModLong(a) += b); } template friend ModLong operator-(T a, const ModLong &b) { return (ModLong(a) -= b); } template friend ModLong operator*(T a, const ModLong &b) { return (ModLong(a) *= b); } template friend ModLong operator/(T a, const ModLong &b) { return (ModLong(a) /= b); } explicit operator bool() const { return x; } bool operator==(const ModLong &a) const { return (x == a.x); } bool operator!=(const ModLong &a) const { return (x != a.x); } friend std::ostream &operator<<(std::ostream &os, const ModLong &a) { return os << a.x; } }; unsigned long long ModLong::M; long double ModLong::INV_M; // !!!Use ModLong::setM!!! //////////////////////////////////////////////////////////////////////////////// /* state -(M-1), ..., M -(M-1) <= u <= 0 ~~> u+1, ..., u+M 1 <= u <= M ~~> u-M, ..., u-1 identify states by mod (M+1) ~~> typical counting: seqs in [1, M]^N s.t. (M+1) | sum */ constexpr Int MO = 998244353; int main() { Int N, M; for (; ~scanf("%lld%lld", &N, &M); ) { ModLong::setM(MO * (M+1)); ModLong numer = ModLong(M).pow(N) + M * (N&1?-1:+1); assert(numer.x % (M+1) == 0); const Int ans = numer.x / (M+1); printf("%lld\n", ans); } return 0; }