#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 1;
int n, q, mod;
ll st[maxn * 4];
ll lazy1[maxn * 4];
ll lazy2[maxn * 4];
int a[maxn];
void push_down(int p, int len)
{
st[2 * p] = (st[2 * p] * lazy2[p] + lazy1[p] * (len - len / 2)) % mod;
st[2 * p + 1] = (st[2 * p + 1] * lazy2[p] + lazy1[p] * len / 2) % mod;
lazy2[2 * p] = (lazy2[2 * p] * lazy2[p]) % mod;
lazy2[2 * p + 1] = (lazy2[2 * p + 1] * lazy2[p]) % mod;
lazy1[p * 2] = (lazy1[2 * p] * lazy2[p] + lazy1[p]) % mod;
lazy1[p * 2 + 1] = (lazy1[2 * p + 1] * lazy2[p] + lazy1[p]) % mod;
lazy1[p] = 0;
lazy2[p] = 1;
}
void build(int l = 1, int r = n, int p = 1)
{
if (l == r)
{
st[p] = a[l] % mod;
return;
}
int mid = l + ((r - l) >> 1);
build(l, mid, p * 2);
build(mid + 1, r, p * 2 + 1);
st[p] = (st[p * 2] + st[p * 2 + 1]) % mod;
for (int i = 1; i <= 4 * n + 1; i++)
{
lazy1[i] = 0;
lazy2[i] = 1;
}
}
void addition(int l, int r, int c, int s = 1, int t = n, int p = 1)
{
if (l <= s && r >= t)
{
st[p] = (st[p] + c * (t - s + 1)) % mod;
lazy1[p] = (lazy1[p] + c) % mod;
return;
}
int mid = s + ((t - s) >> 1);
push_down(c, t - s + 1);
if (l <= mid)
addition(l, r, c, s, mid, p * 2);
if (r > mid)
addition(l, r, c, mid + 1, t, p * 2 + 1);
st[p] = (st[p * 2] + st[p * 2 + 1]) % mod;
}
void multiplication(int l, int r, int c, int s = 1, int t = n, int p = 1)
{
if (l <= s && r >= t)
{
st[p] = st[p] * c % mod;
lazy2[p] = (lazy2[p] * c) % mod;
lazy1[p] = (lazy1[p] * c) % mod;
return;
}
int mid = s + ((t - s) >> 1);
push_down(c, t - s + 1);
if (l <= mid)
multiplication(l, r, c, s, mid, p * 2);
if (r > mid)
multiplication(l, r, c, mid + 1, t, p * 2 + 1);
st[p] = (st[p * 2] + st[p * 2 + 1]) % mod;
}
ll ask(int l, int r, int s = 1, int t = n, int p = 1)
{
if (l <= s && r >= t)
return st[p];
int mid = s + ((t - s) >> 1);
push_down(p, t - s + 1);
ll ans = 0;
if (l <= mid)
ans = (ans + ask(l, r, s, mid, p * 2)) % mod;
if (r > mid)
ans = (ans + ask(l, r, mid + 1, t, 2 * p + 1)) % mod;
return ans;
}
int main()
{
cin >> n >> q >> mod;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
build();
while (q--)
{
int step;
cin >> step;
if (step == 2)
{
int x, y, c;
cin >> x >> y >> c;
addition(x, y, c);
}
else if (step == 1)
{
int x, y, c;
cin >> x >> y >> c;
multiplication(x, y, c);
}
else
{
int x, y;
cin >> x >> y;
cout << ask(x, y) << endl;
}
}
system("pause");
return 0;
}