1 #include<bits/stdc++.h>
2 using namespace std;
3 #define LL long long
4 #define lc (x<<1)
5 #define rc (x<<1|1)
6 #define mid (l+r)/2
7 int const N=50000+10;
8 int c[40][10001],cc[40][10001],p,n,m,C,a[N],s[N<<2],sum[N<<2],phi[40],num;
9 int ph(int x){
10 int ret=x;
11 for(int i=2;i*i<=x;i++)
12 if(x%i==0){
13 ret=ret/i*(i-1);
14 while (x%i==0) x/=i;
15 }
16 if(x>1) ret=ret/x*(x-1);
17 return ret;
18 }
19 void build(int x,int l,int r){
20 if(l==r){
21 sum[x]=a[l]%phi[0]; return;
22 }
23 build(lc,l,mid);
24 build(rc,mid+1,r);
25 sum[x]=(sum[lc]+sum[rc])%phi[0];
26 }
27
28 int calc(unsigned long long x,int p){
29 int check=0;
30 int tx=x,tp=p;
31 // if(x>=phi[p+1]) x=x%phi[p+1]+phi[p+1];
32 while (p>-1){
33 if(check){
34 x=x%phi[p+1]+phi[p+1];
35 int num1=x/10000,num2=x%10000;
36 x=1LL*c[p][num2]*cc[p][num1]%phi[p];
37 }else {
38 unsigned long long tmp=1;
39 if(C!=1)
40 for(int i=1;i<=x;i++){
41 tmp=tmp*C;
42 if(tmp>=phi[p]) {
43 check=1;
44 int num1=x/10000,num2=x%10000;
45 tmp=1LL*c[p][num2]*cc[p][num1]%phi[p];
46 break;
47 }
48 }
49 x=tmp;
50 }
51 p--;
52 }
53 //if(tp>=1) cout<<tx<<" **** "<<tp<<" "<<x<<endl;
54 return x;
55 }
56 void insert(int x,int l,int r,int ll,int rr){
57 if(s[x]>num) return;
58 if(l==r){
59 s[x]++;
60 sum[x]=calc(a[l],s[x]-1);
61 return;
62 }
63 if(ll<=mid) insert(lc,l,mid,ll,rr);
64 if(rr>mid) insert(rc,mid+1,r,ll,rr);
65 s[x]=min(s[lc],s[rc]);
66 sum[x]=(sum[lc]+sum[rc])%phi[0];
67 }
68 int query(int x,int l,int r,int ll,int rr){
69 if(ll<=l && r<=rr) return sum[x];
70 int ret=0;
71 if(ll<=mid) ret+=query(lc,l,mid,ll,rr);
72 if(rr>mid) ret+=query(rc,mid+1,r,ll,rr);
73 return ret%phi[0];
74 }
75 int main(){
76 //freopen("9.in","r",stdin);
77 scanf("%d%d%d%d",&n,&m,&p,&C);
78 phi[0]=p;
79 while (1){
80 c[num][0]=1;
81 unsigned long long tc=C;
82 for(int i=1;i<=10000;i++) c[num][i]=1LL*c[num][i-1]*C%p;
83 cc[num][0]=1;
84 for(int i=1;i<=10000;i++) cc[num][i]=1LL*cc[num][i-1]*c[num][10000]%p;
85 if(p==1) break;
86 phi[++num]=p=ph(p);
87 }
88 phi[++num]=1;
89 //cout<<num<<" "<<phi[0]<<" "<<phi[1]<<endl;
90 for(int i=1;i<=n;i++) scanf("%d",&a[i]);
91 build(1,1,n);
92 while (m--){
93 int opt,l,r;
94 scanf("%d%d%d",&opt,&l,&r);
95 if(opt==0)insert(1,1,n,l,r);
96 else printf("%d\n",query(1,1,n,l,r));
97 //if(opt==0) break;
98
99 }
100 return 0;
101 }