#line 1 "library/Template/template.hpp" #include using namespace std; #define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define ALL(v) (v).begin(),(v).end() #define UNIQUE(v) sort(ALL(v)),(v).erase(unique(ALL(v)),(v).end()) #define MIN(v) *min_element(ALL(v)) #define MAX(v) *max_element(ALL(v)) #define LB(v,x) lower_bound(ALL(v),(x))-(v).begin() #define UB(v,x) upper_bound(ALL(v),(x))-(v).begin() using ll=long long int; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; templateinline bool chmax(T& a,T b){if(ainline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} templateT ceil(T x,U y){assert(y!=0); if(y<0)x=-x,y=-y; return (x>0?(x+y-1)/y:x/y);} templateT floor(T x,U y){assert(y!=0); if(y<0)x=-x,y=-y; return (x>0?x/y:(x-y+1)/y);} templateint popcnt(T x){return __builtin_popcountll(x);} templateint topbit(T x){return (x==0?-1:63-__builtin_clzll(x));} templateint lowbit(T x){return (x==0?-1:__builtin_ctzll(x));} // g++-11 -std=c++1z -o a.out hoge.cpp -I . #line 2 "library/Utility/fastio.hpp" #include class FastIO{ static constexpr int L=1<<16; char rdbuf[L]; int rdLeft=0,rdRight=0; inline void reload(){ int len=rdRight-rdLeft; memmove(rdbuf,rdbuf+rdLeft,len); rdLeft=0,rdRight=len; rdRight+=fread(rdbuf+len,1,L-len,stdin); } inline bool skip(){ for(;;){ while(rdLeft!=rdRight and rdbuf[rdLeft]<=' ')rdLeft++; if(rdLeft==rdRight){ reload(); if(rdLeft==rdRight)return false; } else break; } return true; } template::value,int> =0>inline bool _read(T& x){ if(!skip())return false; if(rdLeft+20>=rdRight)reload(); bool neg=false; if(rdbuf[rdLeft]=='-'){ neg=true; rdLeft++; } x=0; while(rdbuf[rdLeft]>='0' and rdLeft=rdRight)reload(); bool neg=false; if(rdbuf[rdLeft]=='-'){ neg=true; rdLeft++; } x=0; while(rdbuf[rdLeft]>='0' and rdLeft=rdRight)reload(); x=0; while(rdbuf[rdLeft]>='0' and rdLeft::value,int> =0>inline bool _read(T& x){ if(!skip())return false; if(rdLeft+20>=rdRight)reload(); bool neg=false; if(rdbuf[rdLeft]=='-'){ neg=true; rdLeft++; } x=0; while(rdbuf[rdLeft]>='0' and rdbuf[rdLeft]<='9' and rdLeft='0' and rdbuf[rdLeft]<='9' and rdLeft=rdRight)reload(); x=rdbuf[rdLeft++]; return true; } inline bool _read(string& x){ if(!skip())return false; for(;;){ int pos=rdLeft; while(pos' ')pos++; x.append(rdbuf+rdLeft,pos-rdLeft); if(rdLeft==pos)break; rdLeft=pos; if(rdLeft==rdRight)reload(); else break; } return true; } templateinline bool _read(vector& v){ for(auto& x:v){ if(!_read(x))return false; } return true; } char wtbuf[L],tmp[50]; int wtRight=0; inline void flush(){ fwrite(wtbuf,1,wtRight,stdout); wtRight=0; } inline void _write(const char& x){ if(wtRight>L-32)flush(); wtbuf[wtRight++]=x; } inline void _write(const string& x){ for(auto& c:x)_write(c); } template::value,int> =0>inline void _write(T x){ if(wtRight>L-32)flush(); if(x==0){ _write('0'); return; } else if(x<0){ _write('-'); if (__builtin_expect(x == std::numeric_limits::min(), 0)) { switch (sizeof(x)) { case 2: _write("32768"); return; case 4: _write("2147483648"); return; case 8: _write("9223372036854775808"); return; } } x=-x; } int pos=0; while(x!=0){ tmp[pos++]=char((x%10)|48); x/=10; } rep(i,0,pos)wtbuf[wtRight+i]=tmp[pos-1-i]; wtRight+=pos; } inline void _write(__int128_t x){ if(wtRight>L-40)flush(); if(x==0){ _write('0'); return; } else if(x<0){ _write('-'); x=-x; } int pos=0; while(x!=0){ tmp[pos++]=char((x%10)|48); x/=10; } rep(i,0,pos)wtbuf[wtRight+i]=tmp[pos-1-i]; wtRight+=pos; } inline void _write(__uint128_t x){ if(wtRight>L-40)flush(); if(x==0){ _write('0'); return; } int pos=0; while(x!=0){ tmp[pos++]=char((x%10)|48); x/=10; } rep(i,0,pos)wtbuf[wtRight+i]=tmp[pos-1-i]; wtRight+=pos; } templateinline void _write(const vector& v){ rep(i,0,v.size()){ if(i)_write(' '); _write(v[i]); } } public: FastIO(){} ~FastIO(){flush();} inline void read(){} template inline void read(Head& head,Tail&... tail){ assert(_read(head)); read(tail...); } templateinline void write(){if(ln)_write('\n');} template inline void write(const Head& head,const Tail&... tail){ if(space)_write(' '); _write(head); write(tail...); } }; /** * @brief Fast IO */ #line 3 "sol.cpp" #line 2 "library/Math/fastdiv.hpp" struct FastDiv{ using u64=uint64_t; using u128=__uint128_t; constexpr FastDiv():m(),s(),x(){} constexpr FastDiv(int _m) :m(_m),s(__lg(m-1)),x(((u128(1)<<(s+64))+m-1)/m){} constexpr int get(){return m;} constexpr friend u64 operator/(u64 n,const FastDiv& d){ return (u128(n)*d.x>>d.s)>>64; } constexpr friend int operator%(u64 n,const FastDiv& d){ return n-n/d*d.m; } constexpr pair divmod(u64 n)const{ u64 q=n/(*this); return {q,n-q*m}; } int m,s; u64 x; }; /** * @brief Fast Division */ #line 3 "library/Math/dynamic.hpp" struct Fp{ using u64=uint64_t; int v; static int get_mod(){return _getmod();} static void set_mod(int _m){bar=FastDiv(_m);} Fp inv() const{ int tmp,a=v,b=get_mod(),x=1,y=0; while(b){ tmp=a/b,a-=tmp*b; swap(a,b); x-=tmp*y; swap(x,y); } if(x<0){x+=get_mod();} return x; } Fp():v(0){} Fp(ll x){ v=x%get_mod(); if(v<0)v+=get_mod(); } Fp operator-()const{return Fp()-*this;} Fp pow(ll t){ assert(t>=0); Fp res=1,b=*this; while(t){ if(t&1)res*=b; b*=b; t>>=1; } return res; } Fp& operator+=(const Fp& x){ v+=x.v; if(v>=get_mod())v-=get_mod(); return *this; } Fp& operator-=(const Fp& x){ v+=get_mod()-x.v; if(v>=get_mod())v-=get_mod(); return *this; } Fp& operator*=(const Fp& x){ v=(u64(v)*x.v)%bar; return *this; } Fp& operator/=(const Fp& x){ (*this)*=x.inv(); return *this; } Fp operator+(const Fp& x)const{return Fp(*this)+=x;} Fp operator-(const Fp& x)const{return Fp(*this)-=x;} Fp operator*(const Fp& x)const{return Fp(*this)*=x;} Fp operator/(const Fp& x)const{return Fp(*this)/=x;} bool operator==(const Fp& x)const{return v==x.v;} bool operator!=(const Fp& x)const{return v!=x.v;} private: static FastDiv bar; static int _getmod(){return bar.get();} }; FastDiv Fp::bar(998244353); /** * @brief Dynamic Modint */ #line 2 "library/Math/matrix.hpp" templatestruct Matrix{ int h,w; vector> val; T det; Matrix(){} Matrix(int n):h(n),w(n),val(vector>(n,vector(n))){} Matrix(int n,int m):h(n),w(m),val(vector>(n,vector(m))){} vector& operator[](const int i){return val[i];} Matrix& operator+=(const Matrix& m){ assert(h==m.h and w==m.w); rep(i,0,h)rep(j,0,w)val[i][j]+=m.val[i][j]; return *this; } Matrix& operator-=(const Matrix& m){ assert(h==m.h and w==m.w); rep(i,0,h)rep(j,0,w)val[i][j]-=m.val[i][j]; return *this; } Matrix& operator*=(const Matrix& m){ assert(w==m.h); Matrix res(h,m.w); rep(i,0,h)rep(j,0,m.w)rep(k,0,w)res.val[i][j]+=val[i][k]*m.val[k][j]; *this=res; return *this; } Matrix operator+(const Matrix& m)const{return Matrix(*this)+=m;} Matrix operator-(const Matrix& m)const{return Matrix(*this)-=m;} Matrix operator*(const Matrix& m)const{return Matrix(*this)*=m;} Matrix pow(ll k){ Matrix res(h,h),c=*this; rep(i,0,h)res.val[i][i]=1; while(k){if(k&1)res*=c; c*=c; k>>=1;} return res; } vector gauss(int c=-1){ if(val.empty())return {}; if(c==-1)c=w; int cur=0; vector res; det=1; rep(i,0,c){ if(cur==h)break; rep(j,cur,h)if(val[j][i]!=0){ swap(val[cur],val[j]); if(cur!=j)det*=-1; break; } det*=val[cur][i]; if(val[cur][i]==0)continue; rep(j,0,h)if(j!=cur){ T z=val[j][i]/val[cur][i]; rep(k,i,w)val[j][k]-=val[cur][k]*z; } res.push_back(i); cur++; } return res; } Matrix inv(){ assert(h==w); Matrix base(h,h*2),res(h,h); rep(i,0,h)rep(j,0,h)base[i][j]=val[i][j]; rep(i,0,h)base[i][h+i]=1; base.gauss(h); rep(i,0,h)rep(j,0,h)res[i][j]=base[i][h+j]/base[i][i]; return res; } bool operator==(const Matrix& m){ assert(h==m.h and w==m.w); rep(i,0,h)rep(j,0,w)if(val[i][j]!=m.val[i][j])return false; return true; } bool operator!=(const Matrix& m){ assert(h==m.h and w==m.w); rep(i,0,h)rep(j,0,w)if(val[i][j]==m.val[i][j])return false; return true; } friend istream& operator>>(istream& is,Matrix& m){ rep(i,0,m.h)rep(j,0,m.w)is>>m[i][j]; return is; } friend ostream& operator<<(ostream& os,Matrix& m){ rep(i,0,m.h){ rep(j,0,m.w)os<T det(Matrix& a){ return a[0][0]*a[1][1]-a[0][1]*a[1][0]; } FastIO io; void solve(int _rot){ // io.write("Case #"+to_string(_rot)+": "); ll n,mod; io.read(n,mod); Fp::set_mod(mod); Matrix<__int128_t> a(2,2); rep(i,0,2)rep(j,0,2)io.read(a[i][j]); // value ll nn=n; Matrix A(2,2),res(2,2),add(2,2),pw(2,2); rep(i,0,2)rep(j,0,2)A[i][j]=pw[i][j]=a[i][j]; add[0][0]=add[1][1]=1; while(nn){ if(nn&1){ res*=pw; res+=add; } add+=add*pw; pw*=pw; nn>>=1; } res*=A; Fp ret=res[0][0]*res[1][1]-res[0][1]*res[1][0]; cerr< ae=a; ae[0][0]--; ae[1][1]--; if(det(ae)!=0){ if(det(a)<0)sign^=1; if(det(ae)<0)sign^=1; ll A=a[0][0],B=a[0][1],C=a[1][0],D=a[1][1]; if((A-D)*(A-D)+B*C*4>=0){ Matrix<__int128_t> base(2,2); rep(i,0,2)rep(j,0,2)base[i][j]=a[i][j]; if(n%2==0)base*=a; base[0][0]--; base[1][1]--; if(det(base)<0)sign^=1; } } else{ assert(0); } if(sign)ret=-ret; io.write(ret.v); } int main(){ int t; io.read(t); rep(rot,0,t)solve(rot+1); return 0; }