#include #include namespace my{ using namespace atcoder; using ml=modint; auto&operator<<(std::ostream&o,const ml&x){return o<>(std::istream&i,ml&x){long long t;i>>t;x=t;return i;} void main(); void solve(); } int main(){my::main();} namespace my{ #define eb emplace_back #define all(a) (a).begin(),(a).end() #define done(...) rr pp(__VA_ARGS__) #define RD(T,...) T __VA_ARGS__;li(__VA_ARGS__) #define LL(...) RD(ll,__VA_ARGS__) #define JO(a,b) a##b #define jo(a,b) JO(a,b) #define FE(v,e,...) for(au&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):v) #define FO(n) for(ll jo(i,__LINE__)=n;jo(i,__LINE__)-->0;) #define FOR(i,...) for(au[i,i##O,i##E]=range(0,__VA_ARGS__);i=i##O;i-=i##E) #define I template #define J class #define au auto #define rr return using namespace std; Iconstexpr bool same=is_same_v; using is=istream;using os=ostream;using dd=long double;using str=string; using ll=long long;using ull=unsigned long long;using lll=__int128_t;using ulll=__uint128_t; os&operator<<(os&o,const ulll&x){rr(x<10?o:o<0?x:-x);} constexpr dd ee=1e-12; constexpr ll oo=3e18; constexpr ll dx[]{-1,0,1,0,-1,1,1,-1},dy[]{0,-1,0,1,-1,-1,1,1}; constexpr char sp=32,nl=10; au Yes(bool y){rr y?"Yes":"No";} au No(){rr Yes(0);} au range(bool s,ll a,ll b=oo,ll c=1){if(b==oo)b=a,(s?b:a)=0;rr tuple{a-s,b,c};} ll rand(ll l=oo,ll r=oo){static ull a=495;a^=a<<7,a^=a>>9;rr r!=oo?a%(r-l)+l:l!=oo?a%l:a;} bool eve(ll x){rr~x&1;} IT sq(T a){rr a*a;} IT zz(T x){rr x<0?-x:x;} Ill len(const T&a){rr a.size();} Iau min(const A&...a){rr min(initializer_list>{a...});} Istruct cp{ A a={};B b={}; cp(){} cp(A a,B b):a(a),b(b){} cp(pairp):a(p.first),b(p.second){} bool operator==(const cp&c)const{rr a==c.a&&b==c.b;} auto operator<=>(const cp&c)const{rr a!=c.a?a<=>c.a:b<=>c.b;} friend is&operator>>(is&i,cp&c){rr i>>c.a>>c.b;} friend os&operator<<(os&o,const cp&c){rr o<; Istruct tr{ A a={};B b={};C c={}; tr(){} tr(A a,B b,C c):a(a),b(b),c(c){} bool operator==(const tr&t)const{rr a==t.a&&b==t.b&&c==t.c;} auto operator<=>(const tr&t)const{rr a!=t.a?a<=>t.a:b!=t.b?b<=>t.b:c<=>t.c;} friend is&operator>>(is&i,tr&t){rr i>>t.a>>t.b>>t.c;} friend os&operator<<(os&o,const tr&t){rr o<; IT&sort(T&a){sort(all(a));rr a;} Idecltype(au)first(T&a){rr*begin(a);} Idecltype(au)last(T&a){rr*rbegin(a);} Iau pop_front(T&a){assert(len(a));au r=first(a);a.pop_front();rr r;} Iau pop_back(T&a){assert(len(a));au r=last(a);a.pop_back();rr r;} Iusing array=std::array; Ios&operator<<(os&o,const array&a){fo(i,n)o<using map=std::map; Ios&operator<<(os&o,const map&m){fe(m,e)o<using umap=unordered_map; Ios&operator<<(os&o,const umap&m){fe(m,e)o<using bset=bitset; Ios&operator<<(os&o,const bset&b){fo(i,n)o<os&operator<<(os&o,const tuple&t){apply([&](const au&...a){ll i=sizeof...(a);(((o<0,sp))),...);},t);rr o;} Istruct priority_queue:std::priority_queue,F>{ priority_queue(const initializer_list&a={}){fe(a,e)this->emplace(e);} priority_queue(const vector&a){fe(a,e)this->emplace(e);} T front(){rr this->top();} T pop_front(){T r=this->top();this->pop();rr r;} friend os&operator<<(os&o,priority_queue q){while(len(q))o<0,sp);rr o;} }; Iusing max_priority_queue=priority_queue>; Iusing min_priority_queue=priority_queue>; Istruct ve; Iconstexpr bool isv=0; Iconstexpr bool isv> =1; Iconstexpr bool isv> =1; Iau rawv(V){if constexpr(isv)rr rawv(V(1)[0]);else rr V();} Istruct ve:vector{ using vector::vector; using T=decltype(rawv(V())); Ive(const vector&v={}){static_assert(isv ==isv);fe(v,e)this->eb(e);} ve&operator+=(const ve&u){au&v=*this;fo(i,len(v))v[i]+=u[i];rr v;}ve operator+(const ve&u)const{rr ve(*this)+=u;} ve&operator-=(const ve&u){au&v=*this;fo(i,len(v))v[i]-=u[i];rr v;}ve operator-(const ve&u)const{rr ve(*this)-=u;} ve&operator^=(const ve&u){fe(u,e)this->eb(e);rr*this;}ve operator^(const ve&u)const{rr ve(*this)^=u;} ve&operator+=(const T&x){au&v=*this;fe(v,e)e+=x;rr v;}ve operator+(const T&x)const{rr ve(*this)+=x;} ve&operator-=(const T&x){au&v=*this;fe(v,e)e-=x;rr v;}ve operator-(const T&x)const{rr ve(*this)-=x;} ve&operator*=(const T&x){au&v=*this;fe(v,e)e*=x;rr v;}ve operator*(const T&x)const{rr ve(*this)*=x;} ve&operator++(){rr*this+=1;} ve&operator--(){rr*this-=1;} ve operator-()const{rr ve(*this)*=-1;} Iau&operator+=(const bset&a){fo(i,n)(*this)[i]+=a[i];rr*this;} Iau&operator-=(const bset&a){fo(i,n)(*this)[i]-=a[i];rr*this;} au lower_bound(const V&x)const{rr std::lower_bound(all(*this),x);} au upper_bound(const V&x)const{rr std::upper_bound(all(*this),x);} Iau scan(F f)const{cpr;fe(*this,e)if constexpr(!isv)r.b?f(r.a,e),r:r={e,1};else if(au s=e.scan(f);s.b)r.b?f(r.a,s.a),r:r=s;rr r;} T min()const{rr scan([](T&a,const T&b){a>b?a=b:0;;}).a;} ve zeta()const{ve v=*this;if constexpr(isv)fe(v,e)e=e.zeta();fo(i,len(v)-1)v[i+1]+=v[i];rr v;} void af(const V&x={}){this->emplace(this->begin(),x);} friend is&operator>>(is&i,ve&v){fe(v,e)i>>e;rr i;} friend os&operator<<(os&o,const ve&v){fe(v,e)o<?nl:sp);rr o;} }; Iau vec(const ll(&s)[n],T x={}){if constexpr(n==i+1)rr ve(s[i],x);else{au X=vec(s,x);rr ve(s[i],X);}} Ivoid set_size(const ll(&l)[n],A&...a){((a=vec(l,rawv(a))),...);} Iusing v2=ve>;using u1=ve;using u2=v2; using graph=v2; void io(){cin.tie(0)->sync_with_stdio(0);cout<void li(A&...a){(cin>>...>>a);} Ivoid pp(const A&...a){ll i=sizeof...(a);((cout<0,c)),...);cout<au rle(const ve&a){ve>r;fe(a,e)len(r)&&e==r.back().a?++r.back().b:r.eb(e,1).b;rr r;} Iau rce(vea){rr rle(sort(a));} ll inv(ll x,ll m){ll a=(x%m+m)%m,b=m,u=1,v=0;while(b)u-=a/b*v,swap(u,v),a-=a/b*b,swap(a,b);rr(u%m+m)%m;} Istruct rec:F{rec(F&&f):F(std::forward(f)){}Idecltype(au)operator()(A&&...a)const{rr F::operator()(*this,std::forward(a)...);}}; veprime_enumerate(ll n){ vesieve(n/3+1,1); for(ll p=5,d=4,i=1,rn=floor_sqrt(n);p<=rn;p+=d=6-d,i++){ if(!sieve[i])continue; for(ll q=sq(p)/3,r=d*p/3+(d*p%3==2),s=p*2;qr{2,3}; for(ll p=5,d=4,i=1;p<=n;p+=d=6-d,i++)if(sieve[i])r.eb(p); while(len(r)&&r.back()>n)r.pop_back(); return r; } struct montgomery64{ using ml=montgomery64; using i64=ll; using u64=ull; using u128=__uint128_t; static inline u64 m=998244353; static inline u64 miv; static inline u64 n2; static void set_mod(u64 m){ assert(m<(1ULL<<63)); assert(m&1); ml::m=m; n2=-u128(m)%m; miv=m; fo(5)miv*=2-m*miv; assert(m*miv==1); } static u64 mod(){ return m; } u64 a; montgomery64(const i64&a=0):a(reduce((u128)(a%(i64)m+m)*n2)){} static u64 reduce(const u128&a){ u128 r=(a+u128(u64(a)*-miv)*m)>>64; return r>=m?r-m:r; } ml&operator+=(const ml&b){if((a+=b.a)>=m)a-=m;return*this;} ml&operator-=(const ml&b){if(i64(a-=b.a)<0)a+=m;return*this;} ml&operator*=(const ml&b){a=reduce(u128(a)*b.a);return*this;} ml&operator/=(const ml&b){*this*=b.inv();return*this;} ml operator+(const ml&b)const{return ml(*this)+=b;} ml operator-(const ml&b)const{return ml(*this)-=b;} ml operator*(const ml&b)const{return ml(*this)*=b;} ml operator/(const ml&b)const{return ml(*this)/=b;} bool operator==(const ml&b)const{return a==b.a;} bool operator!=(const ml&b)const{return a!=b.a;} ml operator-()const{return ml()-ml(*this);} ml pow(u128 n)const{ ml r(1),x(*this); while(n){ if(n&1)r*=x; x*=x; n>>=1; } return r; } ml inv()const{ u64 a=this->a,b=m,u=1,v=0; while(b)u-=a/b*v,swap(u,v),a-=a/b*b,swap(a,b); return u; } u64 val()const{ return reduce(a); } friend is&operator>>(is&i,ml&b){ ll t;i>>t;b=t; return i; } friend os&operator<<(os&o,const ml&b){ return o<bool miller_rabin(ll n,veas){ ll t=0,d=n-1; while(eve(d))d>>=1; if(mont::mod()!=n)mont::set_mod(n); mont one=1,minus_one=n-1; fe(as,a){ if(a%n==0)continue; ll t=d; mont y=mont(a).pow(t); while(t!=n-1&&y!=one&&y!=minus_one)y*=y,t<<=1; if(y!=minus_one&&eve(t))return 0; } return 1; } bool is_prime(ll n){ if(eve(n))return n==2; if(n<=1)return 0; if(n<4759123141LL)return miller_rabin(n,{2,7,61}); return miller_rabin(n,{2,325,9375,28178,450775,9780504,1795265022}); } templatell pollard_rho(ll n){ if(eve(n))return 2; if(is_prime(n))return n; if(mont::mod()!=n)mont::set_mod(n); mont R,one=1; auto f=[&](mont x){return x*x+R;}; while(1){ mont x,y,ys,q=one; R=rand(2,n),y=rand(2,n); ll g=1; constexpr ll m=128; for(ll r=1;g==1;r<<=1){ x=y; fo(r)y=f(y); for(ll k=0;g==1&&kinner_factorize(ll n){ if(n<=1)return{}; ll d=pollard_rho(n); if(d==n)return{d}; return inner_factorize(d)^inner_factorize(n/d); } vefactorize(ll n){ return rce(inner_factorize(n)); } veprime_table(ll n){ ver(n+1); fe(prime_enumerate(n),p)r[p]=1; rr r; } void main(){io();ll T=1;li(T);fo(T)solve();} void solve(){ LL(N,M); if(N>=M)done(0); ml::set_mod(M); if(M<=2e5){ ml t=1; fo(i,1,N+1)t*=i; done(t); } if(is_prime(M)){ ml::set_mod(M); ml t=1; fo(i,N+1,M)t*=i; done(-t.inv()); }else{ au fa=factorize(M); if(len(fa)>1){ done(0); }else{ //Mは素数の累乗 if(N>=fa[0].a*fa[0].b){ done(0); }else{ ml t=1; fo(i,1,N+1)t*=i; done(t); } } } }}