#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair P; using lll=__int128_t; ll MOD; ll powmod(ll a, ll k, ll mod){ lll ap=a, ans=1; while(k){ if(k&1){ ans*=ap; ans%=mod; } ap=ap*ap; ap%=mod; k>>=1; } return (ll)ans; } vector> matrixmul(int l, int m, int n, vector> a, vector> b){ vector> c(l, vector(n)); for(int i=0; i> matrixpow(int n, vector> a, ll k){ vector> ap=a, ans(n, vector(n)); for(int i=0; i>=1; } return ans; } int main() { ll n; cin>>n>>MOD; if(MOD==2){ cout<<0<> mat(2, vector(2)); mat[0][1]=1, mat[1][0]=MOD-1, mat[1][1]=4%MOD; ll r=powmod(2, n, MOD*MOD-1); auto matp=matrixpow(2, mat, r); cout<<(2*matp[0][0]+4*matp[0][1]+MOD-2)%MOD<