#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=100000;
int n,q,m;
int a[maxn],w[maxn*4],lzy_mul[maxn*4],lzy_add[maxn*4];
int pushup(int u){
return (w[u*2]+w[u*2+1])%m;
}
void build(int u,int L,int R){
if(L==R){
w[u]=a[L];
return;
}
int M=(L+R)/2;
build(u*2,L,M);
build(u*2+1,M+1,R);
pushup(u);
}
void maketag(int u,int L,int R,int x,int type){
if(type==1){
(lzy_add[u]*=x)%=m;
(lzy_mul[u]*=x)%=m;
(w[u]*=x)%=m;
}
else{
(lzy_add[u]*=x)%=m;
(w[u]+=(R-L+1)*x)%=m;
}
}
void pushdown(int u,int L,int R){
int M=(L+R)/2;
maketag(u*2,L,M,lzy_mul[u],1);
maketag(u*2,L,M,lzy_add[u],2);
maketag(u*2+1,M+1,R,lzy_mul[u],1);
maketag(u*2+1,M+1,R,lzy_add[u],2);
lzy_mul[u]=1;
lzy_add[u]=0;
}
bool InRange(int L,int R,int l,int r){
return (l<=L)&&(R<=r);
}
bool OutofRange(int L,int R,int l,int r){
return (l>R)||(r<L);
}
int query(int u,int L,int R,int l,int r){
if(InRange(L,R,l,r)){
return w[u];
}
else if(!OutofRange(L,R,l,r)){
int M=(L+R)/2;
pushdown(u,L,R);
return (query(u*2,L,M,l,r)+query(u*2+1,M+1,R,l,r))%m;
}
else return 0;
}
void update(int u,int L,int R,int l,int r,int x,int type){
if(InRange(L,R,l,r)){
maketag(u,L,R,x,type);
}
else if(!OutofRange(L,R,l,r)){
int M=(L+R)/2;
pushdown(u,L,R);
update(u*2,L,M,l,r,x,type);
update(u*2+1,M+1,R,l,r,x,type);
pushup(u);
}
}
main(){
cin>>n>>q>>m;
for(int i=1;i<=n;i++)cin>>a[i];
build(1,1,n);
for(int i=1;i<=q;i++){
int type,x,y,k;
cin>>type;
if(type==1||type==2){
cin>>x>>y>>k;
update(1,x,y,1,n,k,type);
}
else{
cin>>x>>y;
cout<<query(1,x,y,1,n)<<'\n';
}
}
return 0;
}