#include using i64 = long long; i64 P = 998244353; // assume -P <= x < 2P i64 norm(i64 x) { if (x < 0) { x += P; } if (x >= P) { x -= P; } return x; } template T power(T a, i64 b, T base=1) { T res = std::move(base); for (; b; b /= 2, a *= a) { if (b % 2) { res *= a; } } return res; } struct Z { i64 x; Z(i64 x=0) : x(norm(x % P)) {} int val() const { return x; } Z operator-() const { return Z(norm(P - x)); } Z inv() const { assert(x != 0); return power(*this, P - 2); } Z &operator*=(const Z &rhs) { x = i64(x) * rhs.x % P; return *this; } Z &operator+=(const Z &rhs) { x = norm(x + rhs.x); return *this; } Z &operator-=(const Z &rhs) { x = norm(x - rhs.x); return *this; } Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); } friend Z operator*(const Z &lhs, const Z &rhs) { Z res = lhs; res *= rhs; return res; } friend Z operator+(const Z &lhs, const Z &rhs) { Z res = lhs; res += rhs; return res; } friend Z operator-(const Z &lhs, const Z &rhs) { Z res = lhs; res -= rhs; return res; } friend Z operator/(const Z &lhs, const Z &rhs) { Z res = lhs; res /= rhs; return res; } friend std::istream &operator>>(std::istream &is, Z &a) { i64 v; is >> v; a = Z(v); return is; } friend std::ostream &operator<<(std::ostream &os, const Z &a) { return os << a.val(); } }; template struct Mat { std::vector> val; int n, m; Mat(int n, int m, int x=0) : n(n), m(m) { assert(x == 0 || (n == m && x == 1)); val.resize(n, std::vector(m)); if (x == 1) { for (int i = 0; i < n; i++) { val[i][i] = 1; } } } Mat &operator*=(const T &rhs) { for (auto &row : val) { for (auto &x : row) { x *= rhs; } } return *this; } friend Mat operator*(const Mat &lhs, const T &rhs) { Mat res = lhs; res *= rhs; return res; } friend Mat operator*(const T &lhs, const Mat &rhs) { Mat res = rhs; res *= lhs; return res; } Mat &operator+=(const Mat &rhs) { assert(n == rhs.n && m == rhs.m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { val[i][j] += rhs.val[i][j]; } } return *this; } friend Mat operator+(const Mat &lhs, const Mat &rhs) { assert(lhs.n == rhs.n && lhs.m == rhs.m); Mat res = lhs; res += rhs; return res; } friend Mat operator*(const Mat &lhs, const Mat &rhs) { assert(lhs.m == rhs.n); Mat res(lhs.n, rhs.m); for (int k = 0; k < lhs.m; k++) { for (int i = 0; i < lhs.n; i++) { for (int j = 0; j < rhs.m; j++) { res.val[i][j] += lhs.val[i][k] * rhs.val[k][j]; } } } return res; } Mat &operator*=(const Mat &rhs) { assert(m == rhs.n); auto res = (*this) * rhs; *this = std::move(res); return *this; } }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int N, M; std::cin >> N >> M; P = M; auto a = std::vector>({{1, 1}, {1, 0}}); Mat A(2, 2); A.val = std::move(a); A = power(A, N, Mat(2, 2, 1)); Mat B(2, 1); B.val[1][0] = 1; A *= B; std::cout << A.val[1][0] << "\n"; return 0; }