#include <bits/stdc++.h>
using namespace std;
const int N=5e5+10;
int n,m;
long long a[N];
long long qpow(long long x,long long y,int mod)
{
long long ans=1,cnt=x%mod;
while (y)
{
if (y%2==1) ans=ans*cnt%mod;
cnt=cnt*cnt%mod;
y/=2;
}
return ans;
}
int main() {
freopen("sjjgt.in","r",stdin);
freopen("sjjgt.out","w",stdout);
ios::sync_with_stdio(0),cin.tie(0);
cin>>n>>m;
for (int i=1;i<=n;i++) cin>>a[i];
while (m--)
{
int op,l,r,x;
cin>>op>>l>>r>>x;
if (op==1)
{
for (int i=l;i<=r;i++) a[i]+=x;
}
else
{
long long now=a[r];
for (int i=r-1;i>=l;i--) now=qpow(a[i],now,x);
cout<<now<<endl;
}
}
}