#define lc u<<1
#define rc u<<1|1
const int N = 1e5 + 5;
i64 w[N], n, m, p;
struct Tree { //线段树
i64 l, r, sum, add, mul;
} tr[N * 4];
void cal_lazy(i64 fa, i64 ch) {
i64 a = tr[fa].mul, b = tr[fa].add;
i64 len = tr[ch].r - tr[ch].l + 1;
tr[ch].sum = (a * tr[ch].sum % p + len * b % p) % p;
}
void tag_union(i64 fa, i64 ch) {
i64 a = tr[fa].mul, b = tr[fa].add;
tr[ch].mul = (tr[ch].mul * a) % p;
tr[ch].add = (tr[ch].add * a % p + b) % p;
}
void init_lazy(i64 u) {
tr[u].add = 0;
tr[u].mul = 1;
}
void pushdown(i64 u) {
if (tr[u].add != 0 || tr[u].mul != 1) {
cal_lazy(u, lc);
cal_lazy(u, rc);
tag_union(u, lc);
tag_union(u, rc);
init_lazy(u);
}
}
void pushup(i64 u) { //上传
tr[u].sum = (tr[lc].sum + tr[rc].sum) % p;
}
void build(i64 u, i64 l, i64 r) { //建树
tr[u].l = l, tr[u].r = r;
init_lazy(u);
if (l == r) {
tr[u].sum = w[l];
return ;
}
i64 mid = (l + r) >> 1;
build(lc, l, mid);
build(rc, mid + 1, r);
pushup(u);
}
void modify(i64 u, i64 l, i64 r, i64 k, int type) {
if (tr[u].l >= l && tr[u].r <= r) {
if (type == 2) {
i64 len = tr[u].r - tr[u].l + 1;
tr[u].sum = (tr[u].sum + k * len % p) % p;
tr[u].add = (tr[u].add + k) % p;
} else {
tr[u].sum = (tr[u].sum * k) % p;
tr[u].mul = (tr[u].mul * k) % p;
tr[u].add = (tr[u].add * k) % p;
}
return ;
}
pushdown(u);
i64 mid = (tr[u].l + tr[u].r) >> 1;
if (l <= mid)
modify(lc, l, r, k, type);
if (r > mid)
modify(rc, l, r, k, type);
pushup(u);
}
i64 query(i64 u, i64 l, i64 r) { //区查
if (l <= tr[u].l && tr[u].r <= r)
return tr[u].sum;
i64 mid = tr[u].l + tr[u].r >> 1;
pushdown(u);
i64 res = 0;
if (l <= mid) res += query(lc, l, r), res %= p;
if (r > mid) res += query(rc, l, r), res %= p;
return res % p;
}