#include<bits/stdc++.h>
#define lol long long
using namespace std;
lol n,q,M,ans;
lol e[1000005];
struct nodde
{
lol lazyc,lazyj,zhi,dql,dqr;
}t[1000005];
void build(lol l,lol r,lol tz)
{
t[tz].dql=l;
t[tz].dqr=r;
t[tz].lazyc=1;
if(l==r)
{
t[tz].zhi=e[l];
return ;
}
lol mid=(r+l)>>1;
build(l,mid,tz<<1);
build(mid+1,r,tz<<1|1);
t[tz].zhi=t[tz<<1].zhi+t[tz<<1|1].zhi;
return ;
}
void qumo(lol tz)
{
t[tz].zhi%=M;
t[tz].lazyc%=M;
t[tz].lazyj%=M;
if(t[tz].lazyc==0)
{
t[tz].lazyc=1;
}
return ;
}
void pushdown(lol lt,lol rt,lol tz)
{
t[tz].zhi=t[tz].zhi*t[tz].lazyc+t[tz].lazyj*(rt-lt+1);
t[tz<<1].lazyj*=t[tz].lazyc;t[tz<<1|1].lazyj*=t[tz].lazyc;
t[tz<<1].lazyj+=t[tz].lazyj;t[tz<<1|1].lazyj+=t[tz].lazyj;
t[tz<<1|1].lazyc*=t[tz].lazyc;t[tz<<1].lazyc*=t[tz].lazyc;
t[tz].lazyc=1;
t[tz].lazyj=0;
qumo(tz);
qumo(tz<<1);
qumo(tz<<1|1);
return ;
}
void update(lol tz)
{
if(t[tz].lazyc!=1||t[tz].lazyj!=0)
{
pushdown(t[tz].dql,t[tz].dqr,tz);
}
t[tz].zhi=(t[tz<<1].zhi*t[tz<<1].lazyc+t[tz<<1].lazyj*(t[tz<<1].dqr-t[tz<<1].dql+1))+(t[tz<<1|1].zhi*t[tz<<1|1].lazyc+t[tz<<1|1].lazyj*(t[tz<<1|1].dqr-t[tz<<1|1].dql+1));
if(tz==1)
{
return ;
}
update(tz>>1);
return ;
}
void cheng(lol lt,lol rt,lol lc,lol rc,lol tz,lol zh)
{
if(t[tz].lazyc!=1||t[tz].lazyj!=0)
{
pushdown(lt,rt,tz);
}
if(lt>rc||rt<lc)
{
return ;
}
else if(lt>=lc&&rt<=rc)
{
t[tz].lazyj*=zh;
t[tz].lazyc*=zh;
qumo(tz);
update(tz>>1);
}
else
{
lol mid=(lt+rt)/2;
cheng(lt,mid,lc,rc,tz<<1,zh);
cheng(mid+1,rt,lc,rc,tz<<1|1,zh);
}
return ;
}
void jia(lol lt,lol rt,lol lc,lol rc,lol tz,lol zh)
{
if(t[tz].lazyc!=1||t[tz].lazyj!=0)
{
pushdown(lt,rt,tz);
}
if(lt>rc||rt<lc)
{
return ;
}
else if(lt>=lc&&rt<=rc)
{
t[tz].lazyj+=zh;
qumo(tz);
update(tz>>1);
}
else
{
lol mid=(lt+rt)/2;
jia(lt,mid,lc,rc,tz<<1,zh);
jia(mid+1,rt,lc,rc,tz<<1|1,zh);
}
return ;
}
void shu(lol lt,lol rt,lol lc,lol rc,lol tz)
{
if(t[tz].lazyc!=1||t[tz].lazyj!=0)
{
pushdown(lt,rt,tz);
}
if(lt>rc||rt<lc)
{
return ;
}
else if(lt>=lc&&rt<=rc)
{
ans+=t[tz].zhi;
ans%=M;
}
else
{
lol mid=(lt+rt)/2;
shu(lt,mid,lc,rc,tz<<1);
shu(mid+1,rt,lc,rc,tz<<1|1);
}
return ;
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>q>>M;
for(int i=1;i<=n;i++)
{
cin>>e[i];
}
build(1,n,1);
int op;
for(int i=1;i<=q;i++)
{
cin>>op;
lol l,r;
cin>>l>>r;
lol xa;
if(op==1)
{
cin>>xa;
cheng(1,n,l,r,1,xa);
}
if(op==2)
{
cin>>xa;
jia(1,n,l,r,1,xa);
}
if(op==3)
{
ans=0;
shu(1,n,l,r,1);
cout<<ans<<'\n';
}
}
return 0;
}