#include using namespace std; typedef signed long long ll; #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define FORR2(x,y,arr) for(auto& [x,y]:arr) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) template bool chmax(T &a, const T &b) { if(a bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;} //------------------------------------------------------- ll OA[101010]; int N,Q,C; ll mo; int S[20]; int num[101010][20]; vector cand; ll phi; ll modpow(ll a, ll n = mo-2) { ll r=1;a%=mo; while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1; return r; } template class BIT { public: V bit[1< bt[20]; template class BITm { public: V bit[1< btm; void solve() { int i,j,k,l,r,x,y; string s; cin>>N>>mo>>Q; ll M=mo; phi=mo; for(i=2;i*i<=M;i++) if(M%i==0) { phi=phi/i*(i-1); cand.push_back(i); while(M%i==0) { S[C]++; M/=i; } C++; } if(M>1) { cand.push_back(M); S[C]=1; C++; } FOR(i,N) { cin>>OA[i]; ll a=OA[i]; if(a) { FOR(j,C) { while(a%cand[j]==0) a/=cand[j], num[i][j]++; bt[j].add(i,num[i][j]); } btm.add(i,a%mo); } else { FOR(j,C) { num[i][j]=S[j]; bt[j].add(i,num[i][j]); } btm.add(i,1); } } while(Q--) { int J,M,A,B; cin>>J>>M>>A>>B; if(OA[J]) { int ok=0; if(M==mo) { ok=1; FOR(i,C) if(num[J][i]