#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int N = 1e6 + 6;
typedef long long ll;
ll a[N];
int n, m;
ll ksm(ll d, ll z, ll mod) {
ll ans = 1;
while (z) {
if (z & 1) {
ans *= d;
ans %= mod;
}
ans = ans * ans % mod;
z /= 2;
}
return ans;
}
int main() {
freopen("kdl.in", "r", stdin);
freopen("kdl.out", "w", stdout);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
ll op, l, r, x, y;
while (m--) {
cin >> op >> l >> r >> x;
if (op == 1) {
for (int i = l; i <= r; i++) {
a[i] += x;
}
}
if (op == 2) {
for (int i = l; i <= r; i++) {
a[i] = x;
}
}
if (op == 3) {
vector<long long> v(r - l + 1);
for (int i = l; i <= r; i++) {
v.push_back(a[i]);
}
sort(v.begin(), v.end());
cout << v[x - 1] << endl;
}
if (op == 4) {
cin >> y;
ll ans = 0;
for (int i = l; i <= r; i++) {
ans += ksm(a[i], x, y);
ans %= y;
}
cout << ans << endl;
}
}
return 0;
}