#include<bits/stdc++.h>
using namespace std;
const long long N=1e5+10;
long long a[N],tree[N*4],tag[N*4],tag2[N*4],n,q,m;
long long ls(long long p){return (p<<1);}
long long rs(long long p){return (p<<1|1);}
void update(long long p)
{
tree[p]=tree[ls(p)]+tree[rs(p)]%m;
}
void build(long long p,long long pl,long long pr)
{
tag[p]=0;
tag2[p]=1;
if(pl==pr)
{
tree[p]=a[pl];
return;
}
long long mid=(pl+pr)>>1;
build(ls(p),pl,mid);
build(rs(p),mid+1,pr);
update(p);
}
void tag_down(long long p,long long pl,long long pr)
{
long long mid=(pr+pl)>>1;
tree[ls(p)]=(tree[ls(p)]*tag2[ls(p)]+(mid-pl+1)*tag[p])%m;
tree[rs(p)]=(tree[rs(p)]*tag2[rs(p)]+(pr-mid)*tag[p])%m;
tag2[ls(p)]=(tag2[ls(p)]*tag2[p])%m;
tag2[rs(p)]=(tag2[rs(p)]*tag2[p])%m;
tag[ls(p)]=(tag[ls(p)]*tag2[p]+tag[p])%m;
tag[rs(p)]=(tag[rs(p)]*tag2[p]+tag[p])%m;
tag2[p]=1;
tag[p]=0;
}
void change(long long l,long long r,long long p,long long pl,long long pr,long long d)
{
if(l<=pl&&pr<=r)
{
tree[p]+=(pr-pl+1)*d%m;
tag[p]=(tag[p]+d)%m;
return;
}
tag_down(p,pl,pr);
long long mid=(pl+pr)>>1;
if(l<=mid)
{
change(l,r,ls(p),pl,mid,d);
}
if(r>mid)
{
change(l,r,rs(p),mid+1,pr,d);
}
update(p);
}
void change2(long long l,long long r,long long p,long long pl,long long pr,long long d)
{
if(l<=pl&&pr<=r)
{
tree[p]=(tree[p]*d)%m;
tag2[p]=(tag2[p]*d)%m;
tag[p]=(tag[p]*d)%m;
return;
}
tag_down(p,pl,pr);
long long mid=(pl+pr)>>1;
if(l<=mid)
{
change2(l,r,ls(p),pl,mid,d);
}
if(r>mid)
{
change2(l,r,rs(p),mid+1,pr,d);
}
update(p);
}
long long sum(long long l,long long r,long long p,long long pl,long long pr)
{
if(l<=pl&&pr<=r)
{
return tree[p]%m;
}
tag_down(p,pl,pr);
long long res=0,mid=(pl+pr)>>1;
if(l<=mid)
{
res+=sum(l,r,ls(p),pl,mid)%m;
}
if(r>mid)
{
res+=sum(l,r,rs(p),mid+1,pr)%m;
}
return res%m;
}
int main()
{
cin>>n>>q>>m;
for(long long i=1;i<=n;i++)
{
cin>>a[i];
a[i]%=m;
}
build(1,1,n);
while(q--)
{
long long g,l,r,d;
cin>>g>>l>>r;
if(g==1)
{
cin>>d;
change2(l,r,1,1,n,d);
}
if(g==2)
{
cin>>d;
change(l,r,1,1,n,d);
}
if(g==3)
{
cout<<sum(l,r,1,1,n)%m<<"\n";
}
}
return 0;
}