// #define _GLIBCXX_DEBUG #include using namespace std; using ll = long long; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; int mod; class mint { long long x; public: mint(long long x = 0): x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint &a) { if ((x += a.x) >= mod) x -= mod; return (*this); } mint& operator-=(const mint &a) { if ((x += mod-a.x) >= mod) x -= mod; return (*this); } mint& operator*=(const mint &a) { (x *= a.x) %= mod; return (*this); } mint operator+(const mint &a) const { mint res(*this); return res += a; } mint operator-(const mint &a) const { mint res(*this); return res -= a; } mint operator*(const mint &a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= (*this); return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint& operator/=(const mint &a) { return (*this) *= a.inv(); } mint operator/(const mint &a) { mint res(*this); return res /= a; } friend ostream& operator<<(ostream& os, const mint &m) { os << m.x; return os; } }; template class Matrix { private: vector> A; public: Matrix(){} Matrix(size_t n): A(n, vector(n, T{})){ assert(n > 0); } Matrix(size_t h, size_t w): A(h, vector(w, T{})){ assert(h > 0 && w > 0); } size_t getHeight() const { return A.size(); } size_t getWidth() const { assert(!A.empty()); return A[0].size(); } // when you read the item inline const vector &operator[](size_t k) const { return A.at(k); } // when you assign the item inline vector &operator[](size_t k){ return A.at(k); } // Matrix I = Matrix::I(n); static Matrix I(size_t n){ Matrix mat_I(n); for(size_t i=0; i> C(h, vector(w, 0)); for(size_t i=0; i= 0); Matrix B = Matrix::I(h); while(k > 0){ if(k & 1) B *= (*this); (*this) *= (*this); k >>= 1; } A.swap(B.A); return (*this); } Matrix operator+(const Matrix &B) const { return (Matrix(*this) += B); } Matrix operator-(const Matrix &B) const { return (Matrix(*this) -= B); } Matrix operator*(const Matrix &B) const { return (Matrix(*this) *= B); } Matrix operator^(const long long k) const { return (Matrix(*this) ^= k); } friend ostream &operator<<(ostream &os, const Matrix &p){ size_t h = p.getHeight(); size_t w = p.getWidth(); for(size_t i=0; i> n >> m; mod = m; Matrix F(2, 1); Matrix M(2, 2); F[0][0] = 0; F[1][0] = 1; M[0][0] = 1; M[0][1] = 1; M[1][0] = 1; M[1][1] = 0; M ^= (n-1); Matrix ans = M * F; cout << ans[0][0] << endl; }