#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 3e4 + 50;

int sum1[N << 2], sum0[N << 2], L[N << 2];

int sq;
int l, r, F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all;
map< int , int > H, w;

bool cmp(pair< pii , int > x, pair< pii , int > y) {
	if(x.st.st / sq == y.st.st / sq) return x.st.nd < y.st.nd;
	return x < y;
}

int get(int x) {
	if(x >= 0) return F[x];
	if((-x) & 1) return F[-x]; return mod - F[-x];
}

void merge(int &k) {
	t = L[k];
	l = sol, r = sag;
	sum1[k] = (get(t+1) * sum1[l] + get(t) * sum0[l]) % mod;
	sum1[k] = (sum1[k] + get(t+1) * sum1[r] + get(t) * sum0[r]) % mod;
	sum0[k] = (get(t) * sum1[l] + get(t-1) * sum0[l]) % mod;
	sum0[k] = (sum0[k] + get(t) * sum1[r] + get(t-1) * sum0[r]) % mod;
}

void assign(int k, int bas, int son, int x, int op) {
	if(bas > x || son < x) return ;
	if(bas == son) {
		if(op == 0) {
			sum0[k] = sum1[k] = 0;
			return ;
		}
		sum1[k] = wh[bas] * get(L[k]) % mod;
		sum0[k] = wh[bas] * get(L[k] - 1) % mod;
		return ;
	}
	assign(sol, bas, orta, x, op); assign(sag, orta + 1, son, x, op);
	merge(k);
}

void update(int k, int bas, int son, int x, int y, int t) {
	if(bas > y || son < x) return ;
	if(x <= bas && son <= y) {
		L[k] += t;
		if(bas == son) {
			if(!cnt[bas]) return ;
			sum1[k] = wh[bas] * get(L[k]) % mod;
			sum0[k] = wh[bas] * get(L[k] - 1) % mod;
			return ;
		}
		merge(k);
		return ;
	}
	update(sol, bas, orta, x, y, t); update(sag, orta + 1, son, x, y, t);
	merge(k);
}

int get() { return sum1[1]; }

void add(int x) {
	if(!cnt[x]++) {
		assign(1, 1, s, x, 1);
		update(1, 1, s, x, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		assign(1, 1, s, x, 0);
		update(1, 1, s, x, s, -1);
	}
}

int main() {

	scanf("%d %d", &n, &mod);

	sq = sqrt(n);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		all.pb(mp(mp(x, y), i));
	}

	sort(all.begin(), all.end(), cmp);

	int l = 1, r = 0;

	foreach(it, all) {
		int x = it->st.st;
		int y = it->st.nd;
		while(r < y) add(a[++r]);
		while(l < x) del(a[l++]);
		while(r > y) del(a[r--]);
		while(l > x) add(a[--l]);
		ans[it->nd] = get();
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 3e4 + 50;
const int sq = 300;

int sum1[N << 2], sum0[N << 2], L[N << 2];

int l, r, F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all;
map< int , int > H, w;

bool cmp(pair< pii , int > x, pair< pii , int > y) {
	if(x.st.st / sq == y.st.st / sq) return x.st.nd < y.st.nd;
	return x < y;
}

int get(int x) {
	if(x >= 0) return F[x];
	if((-x) & 1) return F[-x]; return mod - F[-x];
}

void merge(int &k) {
	t = L[k];
	l = sol, r = sag;
	sum1[k] = (get(t+1) * sum1[l] + get(t) * sum0[l]) % mod;
	sum1[k] = (sum1[k] + get(t+1) * sum1[r] + get(t) * sum0[r]) % mod;
	sum0[k] = (get(t) * sum1[l] + get(t-1) * sum0[l]) % mod;
	sum0[k] = (sum0[k] + get(t) * sum1[r] + get(t-1) * sum0[r]) % mod;
}

void assign(int k, int bas, int son, int x, int op) {
	if(bas > x || son < x) return ;
	if(bas == son) {
		if(op == 0) {
			sum0[k] = sum1[k] = 0;
			return ;
		}
		sum1[k] = wh[bas] * get(L[k]) % mod;
		sum0[k] = wh[bas] * get(L[k] - 1) % mod;
		return ;
	}
	assign(sol, bas, orta, x, op); assign(sag, orta + 1, son, x, op);
	merge(k);
}

void update(int k, int bas, int son, int x, int y, int t) {
	if(bas > y || son < x) return ;
	if(x <= bas && son <= y) {
		L[k] += t;
		if(bas == son) {
			if(!cnt[bas]) return ;
			sum1[k] = wh[bas] * get(L[k]) % mod;
			sum0[k] = wh[bas] * get(L[k] - 1) % mod;
			return ;
		}
		merge(k);
		return ;
	}
	update(sol, bas, orta, x, y, t); update(sag, orta + 1, son, x, y, t);
	merge(k);
}

int get() { return sum1[1]; }

void add(int x) {
	if(!cnt[x]++) {
		assign(1, 1, s, x, 1);
		update(1, 1, s, x, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		assign(1, 1, s, x, 0);
		update(1, 1, s, x, s, -1);
	}
}

int main() {

	scanf("%d %d", &n, &mod);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		all.pb(mp(mp(x, y), i));
	}

	sort(all.begin(), all.end(), cmp);

	int l = 1, r = 0;

	foreach(it, all) {
		int x = it->st.st;
		int y = it->st.nd;
		while(r < y) add(a[++r]);
		while(l < x) del(a[l++]);
		while(r > y) del(a[r--]);
		while(l > x) add(a[--l]);
		ans[it->nd] = get();
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 3e4 + 50;
const int sq = 173;

class node {
	public:
	int sum1, sum0, cnt, L;
} ST[N << 2];

int F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all;
map< int , int > H, w;

bool cmp(pair< pii , int > x, pair< pii , int > y) {
	if(x.st.st / sq == y.st.st / sq) return x.st.nd < y.st.nd;
	return x < y;
}

int get(int x) {
	if(x >= 0) return F[x];
	x = -x;
	if(x & 1) return F[x];
	return mod - F[x];
}

node merge(node &x, node l, node r) {
	int t = x.L;
	x.cnt = l.cnt + r.cnt;
	x.sum1 = (get(t+1) * (ll) l.sum1 + get(t) * (ll) l.sum0) % mod;
	x.sum1 = (x.sum1 + get(t+1) * (ll) r.sum1 + get(t) * (ll) r.sum0) % mod;
	x.sum0 = (get(t) * (ll) l.sum1 + get(t-1) * (ll) l.sum0) % mod;
	x.sum0 = (x.sum0 + get(t) * (ll) r.sum1 + get(t-1) * (ll) r.sum0) % mod;
	return x;
}

node assing(int k, int bas, int son, int x, int op) {
	if(bas > x || son < x) return ST[k];
	if(bas == son) {
		if(op == 0) {
			ST[k].sum0 = ST[k].sum1 = ST[k].cnt = 0;
			return ST[k];
		}
		ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
		ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
		ST[k].cnt = 1;
		return ST[k];
	} return merge(ST[k], assing(sol, bas, orta, x, op), assing(sag, orta + 1, son, x, op));
}

node update(int k, int bas, int son, int x, int y, int t) {
	if(bas > y || son < x) return ST[k];
	if(x <= bas && son <= y) {
		ST[k].L += t;
		if(bas == son) {
			if(ST[k].cnt == 0) return ST[k];
			ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
			ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
			return ST[k];
		}
		return merge(ST[k], ST[sol], ST[sag]);
	} return merge(ST[k], update(sol, bas, orta, x, y, t), update(sag, orta + 1, son, x, y, t));
}

int get() { return ST[1].sum1; }

void add(int x) {
	if(!cnt[x]++) {
		assing(1, 1, s, x, 1);
		update(1, 1, s, x, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		assing(1, 1, s, x, 0);
		update(1, 1, s, x, s, -1);
	}
}

int get(int x, int y) {
	vector< int > v;
	FOR(i, x, y) v.pb(wh[a[i]]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	int last = 1, ans = 0;
	FOR(i, 0, v.size() - 1) ans = (ans + F[last++] * (ll) v[i] % mod) % mod;
	return ans;
}

int main() {

	scanf("%d %d", &n, &mod);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		all.pb(mp(mp(x, y), i));
	}

	sort(all.begin(), all.end(), cmp);

	int l = 1, r = 0;

	foreach(it, all) {
		int x = it->st.st;
		int y = it->st.nd;
		while(r < y) add(a[++r]);
		while(l < x) del(a[l++]);
		while(r > y) del(a[r--]);
		while(l > x) add(a[--l]);
		ans[it->nd] = get();
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 3e4 + 50;

int sum1[N << 2], sum0[N << 2], L[N << 2];

int sq;
int l, r, F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all;
map< int , int > H, w;

bool cmp(pair< pii , int > x, pair< pii , int > y) {
	if(x.st.st / sq == y.st.st / sq) return x.st.nd < y.st.nd;
	return x < y;
}

int get(int x) {
	if(x >= 0) return F[x];
	if((-x) & 1) return F[-x]; return mod - F[-x];
}

void merge(int k) {
	t = L[k];
	l = sol, r = sag;
	sum1[k] = (get(t+1) * sum1[l] + get(t) * sum0[l]) % mod;
	sum1[k] = (sum1[k] + get(t+1) * sum1[r] + get(t) * sum0[r]) % mod;
	sum0[k] = (get(t) * sum1[l] + get(t-1) * sum0[l]) % mod;
	sum0[k] = (sum0[k] + get(t) * sum1[r] + get(t-1) * sum0[r]) % mod;
}

void assign(int k, int bas, int son, int x, int op) {
	if(bas == son) {
		L[k] += op;
		if(!cnt[bas]) { sum0[k] = sum1[k] = 0; return ; }
		sum1[k] = wh[bas] * get(L[k]) % mod;
		sum0[k] = wh[bas] * get(L[k] - 1) % mod;
		return ;
	}
	if(x <= orta) {
		assign(sol, bas, orta, x, op);
		L[sag] += op;
		if(orta + 1 == son) {
			if(cnt[son]) {
				sum1[sag] = wh[son] * get(L[sag]) % mod;
				sum0[sag] = wh[son] * get(L[sag] - 1) % mod;
			}
		} else merge(sag);
	}
	else assign(sag, orta + 1, son, x, op);
	merge(k);
}

int get() { return sum1[1]; }

void add(int x) {
	if(!cnt[x]++) assign(1, 1, s, x, +1);
}

void del(int x) {
	if(!--cnt[x]) assign(1, 1, s, x, -1);
}

int main() {

	scanf("%d %d", &n, &mod);

	sq = sqrt(n);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		all.pb(mp(mp(x, y), i));
	}

	sort(all.begin(), all.end(), cmp);

	int l = 1, r = 0;

	foreach(it, all) {
		int x = it->st.st;
		int y = it->st.nd;
		while(r < y) add(a[++r]);
		while(l < x) del(a[l++]);
		while(r > y) del(a[r--]);
		while(l > x) add(a[--l]);
		ans[it->nd] = get();
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 1e5 + 5;
const int sq = 1;

class node {
	public:
	int sum1, sum0, cnt, L;
} ST[N << 2];

int F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all[N];
map< int , int > H, w;

int get(int x) {
	if(x >= 0) return F[x];
	x = -x;
	if(x & 1) return F[x];
	return mod - F[x];
}

node merge(node &x, node l, node r) {
	int t = x.L;
	x.cnt = l.cnt + r.cnt;
	x.sum1 = (get(t+1) * (ll) l.sum1 + get(t) * (ll) l.sum0) % mod;
	x.sum1 = (x.sum1 + get(t+1) * (ll) r.sum1 + get(t) * (ll) r.sum0) % mod;
	x.sum0 = (get(t) * (ll) l.sum1 + get(t-1) * (ll) l.sum0) % mod;
	x.sum0 = (x.sum0 + get(t) * (ll) r.sum1 + get(t-1) * (ll) r.sum0) % mod;
	return x;
}

node assing(int k, int bas, int son, int x, int op) {
	if(bas > x || son < x) return ST[k];
	if(bas == son) {
		if(op == 0) {
			ST[k].sum0 = ST[k].sum1 = ST[k].cnt = 0;
			return ST[k];
		}
		ST[k].sum1 = bas * (ll) get(ST[k].L) % mod;
		ST[k].sum0 = bas * (ll) get(ST[k].L - 1) % mod;
		ST[k].cnt = 1;
		return ST[k];
	} return merge(ST[k], assing(sol, bas, orta, x, op), assing(sag, orta + 1, son, x, op));
}

node update(int k, int bas, int son, int x, int y, int t) {
	if(bas > y || son < x) return ST[k];
	if(x <= bas && son <= y) {
		ST[k].L += t;
		if(bas == son) {
			if(ST[k].cnt == 0) return ST[k];
			ST[k].sum1 = bas * (ll) get(ST[k].L) % mod;
			ST[k].sum0 = bas * (ll) get(ST[k].L - 1) % mod;
			return ST[k];
		}
		return merge(ST[k], ST[sol], ST[sag]);
	} return merge(ST[k], update(sol, bas, orta, x, y, t), update(sag, orta + 1, son, x, y, t));
}

int query(int k, int bas, int son, int x, int y) {
	if(bas > y || son < x) return 0;
	if(x <= bas && son <= y) return ST[k].cnt;
	return query(sol, bas, orta, x, y) + query(sag, orta + 1, son, x, y);
}

int get() { return ST[1].sum1; }

void add(int x) {
	if(!cnt[x]++) {
		assing(1, 1, s, x, 1);
		update(1, 1, s, x, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		assing(1, 1, s, x, 0);
		update(1, 1, s, x, s, -1);
	}
}

int get(int x, int y) {
	vector< int > v;
	FOR(i, x, y) v.pb(a[i]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	int last = 1, ans = 0;
	FOR(i, 0, v.size() - 1) ans = (ans + F[last++] * (ll) wh[v[i]] % mod) % mod;
	return ans;
}

int main() {

	scanf("%d %d", &n, &mod);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	int last = 1;

	FOR(i, 1, n) {
		if(i == n || i % sq == 0) {
			++S;
			start[S] = last, finish[S] = i;
			FOR(j, last, i) belong[j] = S;
			last = i + 1;
		}
	}

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		if(belong[x] == belong[y]) {
			ans[i] = get(x, y);
		}
		else all[belong[x]].pb(mp(mp(y, x), i));
	}

	FOR(i, 1, S) {
		sort(all[i].begin(), all[i].end());
		int last = finish[i];
		foreach(it, all[i]) {
			while(last < it->st.st) add(a[++last]);
			FOR(j, it->st.nd, finish[i]) add(a[j]);
			ans[it->nd] = get();
			FOR(j, it->st.nd, finish[i]) del(a[j]);
		} while(last > start[i]) del(a[last--]);
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 3e4 + 50;
const int sq = 150;

class node {
	public:
	int sum1, sum0, cnt, L;
} ST[N << 2];

int F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all;
map< int , int > H, w;

bool cmp(pair< pii , int > x, pair< pii , int > y) {
	if(x.st.st / sq == y.st.st / sq) return x.st.nd < y.st.nd;
	return x < y;
}

int get(int x) {
	if(x >= 0) return F[x];
	x = -x;
	if(x & 1) return F[x];
	return mod - F[x];
}

node merge(node &x, node l, node r) {
	int t = x.L;
	x.cnt = l.cnt + r.cnt;
	x.sum1 = (get(t+1) * l.sum1 + get(t) * l.sum0) % mod;
	x.sum1 = (x.sum1 + get(t+1) * r.sum1 + get(t) * r.sum0) % mod;
	x.sum0 = (get(t) * l.sum1 + get(t-1) * l.sum0) % mod;
	x.sum0 = (x.sum0 + get(t) * r.sum1 + get(t-1) * r.sum0) % mod;
	return x;
}

node assing(int k, int bas, int son, int x, int op) {
	if(bas > x || son < x) return ST[k];
	if(bas == son) {
		if(op == 0) {
			ST[k].sum0 = ST[k].sum1 = ST[k].cnt = 0;
			return ST[k];
		}
		ST[k].sum1 = wh[bas] * get(ST[k].L) % mod;
		ST[k].sum0 = wh[bas] * get(ST[k].L - 1) % mod;
		ST[k].cnt = 1;
		return ST[k];
	} return merge(ST[k], assing(sol, bas, orta, x, op), assing(sag, orta + 1, son, x, op));
}

node update(int k, int bas, int son, int x, int y, int t) {
	if(bas > y || son < x) return ST[k];
	if(x <= bas && son <= y) {
		ST[k].L += t;
		if(bas == son) {
			if(ST[k].cnt == 0) return ST[k];
			ST[k].sum1 = wh[bas] * get(ST[k].L) % mod;
			ST[k].sum0 = wh[bas] * get(ST[k].L - 1) % mod;
			return ST[k];
		}
		return merge(ST[k], ST[sol], ST[sag]);
	} return merge(ST[k], update(sol, bas, orta, x, y, t), update(sag, orta + 1, son, x, y, t));
}

int get() { return ST[1].sum1; }

void add(int x) {
	if(!cnt[x]++) {
		assing(1, 1, s, x, 1);
		update(1, 1, s, x, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		assing(1, 1, s, x, 0);
		update(1, 1, s, x, s, -1);
	}
}

int get(int x, int y) {
	vector< int > v;
	FOR(i, x, y) v.pb(wh[a[i]]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	int last = 1, ans = 0;
	FOR(i, 0, v.size() - 1) ans = (ans + F[last++] * (ll) v[i] % mod) % mod;
	return ans;
}

int main() {

	scanf("%d %d", &n, &mod);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		all.pb(mp(mp(x, y), i));
	}

	sort(all.begin(), all.end(), cmp);

	int l = 1, r = 0;

	foreach(it, all) {
		int x = it->st.st;
		int y = it->st.nd;
		while(r < y) add(a[++r]);
		while(l < x) del(a[l++]);
		while(r > y) del(a[r--]);
		while(l > x) add(a[--l]);
		ans[it->nd] = get();
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 3e4 + 5;
const int sq = 300;

class node {
	public:
	int sum1, sum0, cnt, L;
} ST[N << 2];

int F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all;
map< int , int > H, w;

bool cmp(pair< pii , int > x, pair< pii , int > y) {
	if(x.st.st / sq == y.st.st / sq) return x.st.nd < y.st.nd;
	return x < y;
}

int get(int x) {
	if(x >= 0) return F[x];
	x = -x;
	if(x & 1) return F[x];
	return mod - F[x];
}

node merge(node &x, node l, node r) {
	int t = x.L;
	x.cnt = l.cnt + r.cnt;
	x.sum1 = (get(t+1) * (ll) l.sum1 + get(t) * (ll) l.sum0) % mod;
	x.sum1 = (x.sum1 + get(t+1) * (ll) r.sum1 + get(t) * (ll) r.sum0) % mod;
	x.sum0 = (get(t) * (ll) l.sum1 + get(t-1) * (ll) l.sum0) % mod;
	x.sum0 = (x.sum0 + get(t) * (ll) r.sum1 + get(t-1) * (ll) r.sum0) % mod;
	return x;
}

node assing(int k, int bas, int son, int x, int op) {
	if(bas > x || son < x) return ST[k];
	if(bas == son) {
		if(op == 0) {
			ST[k].sum0 = ST[k].sum1 = ST[k].cnt = 0;
			return ST[k];
		}
		ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
		ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
		ST[k].cnt = 1;
		return ST[k];
	} return merge(ST[k], assing(sol, bas, orta, x, op), assing(sag, orta + 1, son, x, op));
}

node update(int k, int bas, int son, int x, int y, int t) {
	if(bas > y || son < x) return ST[k];
	if(x <= bas && son <= y) {
		ST[k].L += t;
		if(bas == son) {
			if(ST[k].cnt == 0) return ST[k];
			ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
			ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
			return ST[k];
		}
		return merge(ST[k], ST[sol], ST[sag]);
	} return merge(ST[k], update(sol, bas, orta, x, y, t), update(sag, orta + 1, son, x, y, t));
}

int get() { return ST[1].sum1; }

void add(int x) {
	if(!cnt[x]++) {
		assing(1, 1, s, x, 1);
		update(1, 1, s, x, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		assing(1, 1, s, x, 0);
		update(1, 1, s, x, s, -1);
	}
}

int get(int x, int y) {
	vector< int > v;
	FOR(i, x, y) v.pb(wh[a[i]]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	int last = 1, ans = 0;
	FOR(i, 0, v.size() - 1) ans = (ans + F[last++] * (ll) v[i] % mod) % mod;
	return ans;
}

int main() {

	scanf("%d %d", &n, &mod);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		all.pb(mp(mp(x, y), i));
	}

	int l = 1, r = 0;

	foreach(it, all) {
		int x = it->st.st;
		int y = it->st.nd;
		while(r < y) add(a[++r]);
		while(l < x) del(a[l++]);
		while(r > y) del(a[r--]);
		while(l > x) add(a[--l]);
		ans[it->nd] = get();
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 3e4 + 50;
const int sq = 75;

class node {
	public:
	int sum1, sum0, cnt, L;
} ST[N << 2];

int F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all;
map< int , int > H, w;

bool cmp(pair< pii , int > x, pair< pii , int > y) {
	if(x.st.st / sq == y.st.st / sq) return x.st.nd < y.st.nd;
	return x < y;
}

int get(int x) {
	if(x >= 0) return F[x];
	x = -x;
	if(x & 1) return F[x];
	return mod - F[x];
}

node merge(node &x, node l, node r) {
	int t = x.L;
	x.cnt = l.cnt + r.cnt;
	x.sum1 = (get(t+1) * l.sum1 + get(t) * l.sum0) % mod;
	x.sum1 = (x.sum1 + get(t+1) * r.sum1 + get(t) * r.sum0) % mod;
	x.sum0 = (get(t) * l.sum1 + get(t-1) * l.sum0) % mod;
	x.sum0 = (x.sum0 + get(t) * r.sum1 + get(t-1) * r.sum0) % mod;
	return x;
}

node assing(int k, int bas, int son, int x, int op) {
	if(bas > x || son < x) return ST[k];
	if(bas == son) {
		if(op == 0) {
			ST[k].sum0 = ST[k].sum1 = ST[k].cnt = 0;
			return ST[k];
		}
		ST[k].sum1 = wh[bas] * get(ST[k].L) % mod;
		ST[k].sum0 = wh[bas] * get(ST[k].L - 1) % mod;
		ST[k].cnt = 1;
		return ST[k];
	} return merge(ST[k], assing(sol, bas, orta, x, op), assing(sag, orta + 1, son, x, op));
}

node update(int k, int bas, int son, int x, int y, int t) {
	if(bas > y || son < x) return ST[k];
	if(x <= bas && son <= y) {
		ST[k].L += t;
		if(bas == son) {
			if(ST[k].cnt == 0) return ST[k];
			ST[k].sum1 = wh[bas] * get(ST[k].L) % mod;
			ST[k].sum0 = wh[bas] * get(ST[k].L - 1) % mod;
			return ST[k];
		}
		return merge(ST[k], ST[sol], ST[sag]);
	} return merge(ST[k], update(sol, bas, orta, x, y, t), update(sag, orta + 1, son, x, y, t));
}

int get() { return ST[1].sum1; }

void add(int x) {
	if(!cnt[x]++) {
		assing(1, 1, s, x, 1);
		update(1, 1, s, x, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		assing(1, 1, s, x, 0);
		update(1, 1, s, x, s, -1);
	}
}

int get(int x, int y) {
	vector< int > v;
	FOR(i, x, y) v.pb(wh[a[i]]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	int last = 1, ans = 0;
	FOR(i, 0, v.size() - 1) ans = (ans + F[last++] * (ll) v[i] % mod) % mod;
	return ans;
}

int main() {

	scanf("%d %d", &n, &mod);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		all.pb(mp(mp(x, y), i));
	}

	sort(all.begin(), all.end(), cmp);

	int l = 1, r = 0;

	foreach(it, all) {
		int x = it->st.st;
		int y = it->st.nd;
		while(r < y) add(a[++r]);
		while(l < x) del(a[l++]);
		while(r > y) del(a[r--]);
		while(l > x) add(a[--l]);
		ans[it->nd] = get();
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 3e4 + 5;
const int sq = 200;

class node {
	public:
	int sum1, sum0, cnt, L;
} ST[N << 2];

int F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all[N];
map< int , int > H, w;

int get(int x) {
	if(x >= 0) return F[x];
	x = -x;
	if(x & 1) return F[x];
	return mod - F[x];
}

node merge(node &x, node l, node r) {
	int t = x.L;
	x.cnt = l.cnt + r.cnt;
	x.sum1 = (get(t+1) * (ll) l.sum1 + get(t) * (ll) l.sum0) % mod;
	x.sum1 = (x.sum1 + get(t+1) * (ll) r.sum1 + get(t) * (ll) r.sum0) % mod;
	x.sum0 = (get(t) * (ll) l.sum1 + get(t-1) * (ll) l.sum0) % mod;
	x.sum0 = (x.sum0 + get(t) * (ll) r.sum1 + get(t-1) * (ll) r.sum0) % mod;
	return x;
}

node assing(int k, int bas, int son, int x, int op) {
	if(bas > x || son < x) return ST[k];
	if(bas == son) {
		if(op == 0) {
			ST[k].sum0 = ST[k].sum1 = ST[k].cnt = 0;
			return ST[k];
		}
		ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
		ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
		ST[k].cnt = 1;
		return ST[k];
	} return merge(ST[k], assing(sol, bas, orta, x, op), assing(sag, orta + 1, son, x, op));
}

node update(int k, int bas, int son, int x, int y, int t) {
	if(bas > y || son < x) return ST[k];
	if(x <= bas && son <= y) {
		ST[k].L += t;
		if(bas == son) {
			if(ST[k].cnt == 0) return ST[k];
			ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
			ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
			return ST[k];
		}
		return merge(ST[k], ST[sol], ST[sag]);
	} return merge(ST[k], update(sol, bas, orta, x, y, t), update(sag, orta + 1, son, x, y, t));
}

int get() { return ST[1].sum1; }

void add(int x) {
	if(!cnt[x]++) {
		assing(1, 1, s, x, 1);
		update(1, 1, s, x, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		assing(1, 1, s, x, 0);
		update(1, 1, s, x, s, -1);
	}
}

int get(int x, int y) {
	vector< int > v;
	FOR(i, x, y) v.pb(wh[a[i]]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	int last = 1, ans = 0;
	FOR(i, 0, v.size() - 1) ans = (ans + F[last++] * (ll) v[i] % mod) % mod;
	return ans;
}

int main() {

	scanf("%d %d", &n, &mod);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	int last = 1;

	FOR(i, 1, n) {
		if(i == n || i % sq == 0) {
			++S;
			start[S] = last, finish[S] = i;
			FOR(j, last, i) belong[j] = S;
			last = i + 1;
		}
	}

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		if(belong[x] == belong[y]) {
			ans[i] = get(x, y);
		}
		else all[belong[x]].pb(mp(mp(y, x), i));
	}

	FOR(i, 1, S) {
		sort(all[i].begin(), all[i].end());
		int last = finish[i];
		FOR(j, 1, (N << 2) - 1) ST[j].cnt = ST[j].sum0 = ST[j].sum1 = ST[j].sum0 = 0;
		memset(cnt, 0, sizeof cnt);
		foreach(it, all[i]) {
			while(last < it->st.st) add(a[++last]);
			FOR(j, it->st.nd, finish[i]) add(a[j]);
			ans[it->nd] = get();
			FOR(j, it->st.nd, finish[i]) del(a[j]);
		}
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 3e4 + 50;
const int sq = 200;

class node {
	public:
	int sum1, sum0, cnt, L;
} ST[N << 2];

int F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all;
map< int , int > H, w;

bool cmp(pair< pii , int > x, pair< pii , int > y) {
	if(x.st.st / sq == y.st.st / sq) return x.st.nd < y.st.nd;
	return x < y;
}

int get(int x) {
	if(x >= 0) return F[x];
	x = -x;
	if(x & 1) return F[x];
	return mod - F[x];
}

node merge(node &x, node l, node r) {
	int t = x.L;
	x.cnt = l.cnt + r.cnt;
	x.sum1 = (get(t+1) * (ll) l.sum1 + get(t) * (ll) l.sum0) % mod;
	x.sum1 = (x.sum1 + get(t+1) * (ll) r.sum1 + get(t) * (ll) r.sum0) % mod;
	x.sum0 = (get(t) * (ll) l.sum1 + get(t-1) * (ll) l.sum0) % mod;
	x.sum0 = (x.sum0 + get(t) * (ll) r.sum1 + get(t-1) * (ll) r.sum0) % mod;
	return x;
}

node assing(int k, int bas, int son, int x, int op) {
	if(bas > x || son < x) return ST[k];
	if(bas == son) {
		if(op == 0) {
			ST[k].sum0 = ST[k].sum1 = ST[k].cnt = 0;
			return ST[k];
		}
		ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
		ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
		ST[k].cnt = 1;
		return ST[k];
	} return merge(ST[k], assing(sol, bas, orta, x, op), assing(sag, orta + 1, son, x, op));
}

node update(int k, int bas, int son, int x, int y, int t) {
	if(bas > y || son < x) return ST[k];
	if(x <= bas && son <= y) {
		ST[k].L += t;
		if(bas == son) {
			if(ST[k].cnt == 0) return ST[k];
			ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
			ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
			return ST[k];
		}
		return merge(ST[k], ST[sol], ST[sag]);
	} return merge(ST[k], update(sol, bas, orta, x, y, t), update(sag, orta + 1, son, x, y, t));
}

int get() { return ST[1].sum1; }

void add(int x) {
	if(!cnt[x]++) {
		assing(1, 1, s, x, 1);
		update(1, 1, s, x, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		assing(1, 1, s, x, 0);
		update(1, 1, s, x, s, -1);
	}
}

int get(int x, int y) {
	vector< int > v;
	FOR(i, x, y) v.pb(wh[a[i]]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	int last = 1, ans = 0;
	FOR(i, 0, v.size() - 1) ans = (ans + F[last++] * (ll) v[i] % mod) % mod;
	return ans;
}

int main() {

	scanf("%d %d", &n, &mod);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		all.pb(mp(mp(x, y), i));
	}

	sort(all.begin(), all.end(), cmp);

	int l = 1, r = 0;

	foreach(it, all) {
		int x = it->st.st;
		int y = it->st.nd;
		while(r < y) add(a[++r]);
		while(l < x) del(a[l++]);
		while(r > y) del(a[r--]);
		while(l > x) add(a[--l]);
		ans[it->nd] = get();
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 1e4 + 5;
const int sq = 1;

class node {
	public:
	int sum1, sum0, cnt, L;
} ST[N << 2];

int F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all[N];
map< int , int > H, w;

int get(int x) {
	if(x >= 0) return F[x];
	x = -x;
	if(x & 1) return F[x];
	return mod - F[x];
}

node merge(node &x, node l, node r) {
	int t = x.L;
	x.cnt = l.cnt + r.cnt;
	x.sum1 = (get(t+1) * (ll) l.sum1 + get(t) * (ll) l.sum0) % mod;
	x.sum1 = (x.sum1 + get(t+1) * (ll) r.sum1 + get(t) * (ll) r.sum0) % mod;
	x.sum0 = (get(t) * (ll) r.sum1 + get(t-1) * (ll) l.sum0) % mod;
	x.sum0 = (x.sum0 + get(t) * (ll) r.sum1 + get(t-1) * (ll) r.sum0) % mod;
	return x;
}

node assing(int k, int bas, int son, int x, int op) {
	if(bas > x || son < x) return ST[k];
	if(bas == son) {
		if(op == 0) {
			ST[k].sum0 = ST[k].sum1 = ST[k].cnt = 0;
			return ST[k];
		}
		ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
		ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
		ST[k].cnt = 1;
		return ST[k];
	} return merge(ST[k], assing(sol, bas, orta, x, op), assing(sag, orta + 1, son, x, op));
}

node update(int k, int bas, int son, int x, int y, int t) {
	if(bas > y || son < x) return ST[k];
	if(x <= bas && son <= y) {
		ST[k].L += t;
		if(bas == son) {
			if(ST[k].cnt == 0) return ST[k];
			ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
			ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
			return ST[k];
		}
		return merge(ST[k], ST[sol], ST[sag]);
	} return merge(ST[k], update(sol, bas, orta, x, y, t - ST[k].L), update(sag, orta + 1, son, x, y, t - ST[k].L));
}

int query(int k, int bas, int son, int x, int y) {
	if(bas > y || son < x) return 0;
	if(x <= bas && son <= y) return ST[k].cnt;
	return query(sol, bas, orta, x, y) + query(sag, orta + 1, son, x, y);
}

int get() { return ST[1].sum1; }

void add(int x) {
	if(!cnt[x]++) {
		assing(1, 1, s, x, 1);
		update(1, 1, s, x, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		assing(1, 1, s, x, 0);
		update(1, 1, s, x, s, -1);
	}
}

int get(int x, int y) {
	vector< int > v;
	FOR(i, x, y) v.pb(a[i]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	int last = 1, ans = 0;
	FOR(i, 0, v.size() - 1) ans = (ans + F[last++] * (ll) wh[v[i]] % mod) % mod;
	return ans;
}

int main() {

	scanf("%d %d", &n, &mod);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	int last = 1;

	FOR(i, 1, n) {
		if(i == n || i % sq == 0) {
			++S;
			start[S] = last, finish[S] = i;
			FOR(j, last, i) belong[j] = S;
			last = i + 1;
		}
	}

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		if(belong[x] == belong[y]) {
			ans[i] = get(x, y);
		}
		else all[belong[x]].pb(mp(mp(y, x), i));
	}

	FOR(i, 1, S) {
		sort(all[i].begin(), all[i].end());
		int last = finish[i];
		foreach(it, all[i]) {
			while(last < it->st.st) add(a[++last]);
			FOR(j, it->st.nd, finish[i]) add(a[j]);
			ans[it->nd] = get();
			FOR(j, it->st.nd, finish[i]) del(a[j]);
		} while(last > start[i]) del(a[last--]);
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 3e4 + 5;
const int sq = 100;

class node {
	public:
	int sum1, sum0, cnt, L;
} ST[N << 2];

int F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all[N];
map< int , int > H, w;

int get(int x) {
	if(x >= 0) return F[x];
	x = -x;
	if(x & 1) return F[x];
	return mod - F[x];
}

node merge(node &x, node l, node r) {
	int t = x.L;
	x.cnt = l.cnt + r.cnt;
	x.sum1 = (get(t+1) * (ll) l.sum1 + get(t) * (ll) l.sum0) % mod;
	x.sum1 = (x.sum1 + get(t+1) * (ll) r.sum1 + get(t) * (ll) r.sum0) % mod;
	x.sum0 = (get(t) * (ll) l.sum1 + get(t-1) * (ll) l.sum0) % mod;
	x.sum0 = (x.sum0 + get(t) * (ll) r.sum1 + get(t-1) * (ll) r.sum0) % mod;
	return x;
}

node assing(int k, int bas, int son, int x, int op) {
	if(bas > x || son < x) return ST[k];
	if(bas == son) {
		if(op == 0) {
			ST[k].sum0 = ST[k].sum1 = ST[k].cnt = 0;
			return ST[k];
		}
		ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
		ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
		ST[k].cnt = 1;
		return ST[k];
	} return merge(ST[k], assing(sol, bas, orta, x, op), assing(sag, orta + 1, son, x, op));
}

node update(int k, int bas, int son, int x, int y, int t) {
	if(bas > y || son < x) return ST[k];
	if(x <= bas && son <= y) {
		ST[k].L += t;
		if(bas == son) {
			if(ST[k].cnt == 0) return ST[k];
			ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
			ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
			return ST[k];
		}
		return merge(ST[k], ST[sol], ST[sag]);
	} return merge(ST[k], update(sol, bas, orta, x, y, t), update(sag, orta + 1, son, x, y, t));
}

int get() { return ST[1].sum1; }

void add(int x) {
	if(!cnt[x]++) {
		assing(1, 1, s, x, 1);
		update(1, 1, s, x, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		assing(1, 1, s, x, 0);
		update(1, 1, s, x, s, -1);
	}
}

int get(int x, int y) {
	vector< int > v;
	FOR(i, x, y) v.pb(wh[a[i]]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	int last = 1, ans = 0;
	FOR(i, 0, v.size() - 1) ans = (ans + F[last++] * (ll) v[i] % mod) % mod;
	return ans;
}

int main() {

	scanf("%d %d", &n, &mod);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	int last = 1;

	FOR(i, 1, n) {
		if(i == n || i % sq == 0) {
			++S;
			start[S] = last, finish[S] = i;
			FOR(j, last, i) belong[j] = S;
			last = i + 1;
		}
	}

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		if(belong[x] == belong[y]) {
			ans[i] = get(x, y);
		}
		else all[belong[x]].pb(mp(mp(y, x), i));
	}

	FOR(i, 1, S) {
		sort(all[i].begin(), all[i].end());
		int last = finish[i];
		FOR(j, 1, (N << 2) - 1) ST[j].cnt = ST[j].sum0 = ST[j].sum1 = ST[j].L = 0;
		memset(cnt, 0, sizeof cnt);
		foreach(it, all[i]) {
			while(last < it->st.st) add(a[++last]);
			FOR(j, it->st.nd, finish[i]) add(a[j]);
			ans[it->nd] = get();
			FOR(j, it->st.nd, finish[i]) del(a[j]);
		}
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 3e4 + 50;
const int sq = 100;

class node {
	public:
	int sum1, sum0, cnt, L;
} ST[N << 2];

int F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all;
map< int , int > H, w;

bool cmp(pair< pii , int > x, pair< pii , int > y) {
	if(x.st.st / sq == y.st.st / sq) return x.st.nd < y.st.nd;
	return x < y;
}

int get(int x) {
	if(x >= 0) return F[x];
	x = -x;
	if(x & 1) return F[x];
	return mod - F[x];
}

node merge(node &x, node l, node r) {
	int t = x.L;
	x.cnt = l.cnt + r.cnt;
	x.sum1 = (get(t+1) * (ll) l.sum1 + get(t) * (ll) l.sum0) % mod;
	x.sum1 = (x.sum1 + get(t+1) * (ll) r.sum1 + get(t) * (ll) r.sum0) % mod;
	x.sum0 = (get(t) * (ll) l.sum1 + get(t-1) * (ll) l.sum0) % mod;
	x.sum0 = (x.sum0 + get(t) * (ll) r.sum1 + get(t-1) * (ll) r.sum0) % mod;
	return x;
}

node assing(int k, int bas, int son, int x, int op) {
	if(bas > x || son < x) return ST[k];
	if(bas == son) {
		if(op == 0) {
			ST[k].sum0 = ST[k].sum1 = ST[k].cnt = 0;
			return ST[k];
		}
		ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
		ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
		ST[k].cnt = 1;
		return ST[k];
	} return merge(ST[k], assing(sol, bas, orta, x, op), assing(sag, orta + 1, son, x, op));
}

node update(int k, int bas, int son, int x, int y, int t) {
	if(bas > y || son < x) return ST[k];
	if(x <= bas && son <= y) {
		ST[k].L += t;
		if(bas == son) {
			if(ST[k].cnt == 0) return ST[k];
			ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
			ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
			return ST[k];
		}
		return merge(ST[k], ST[sol], ST[sag]);
	} return merge(ST[k], update(sol, bas, orta, x, y, t), update(sag, orta + 1, son, x, y, t));
}

int get() { return ST[1].sum1; }

void add(int x) {
	if(!cnt[x]++) {
		assing(1, 1, s, x, 1);
		update(1, 1, s, x, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		assing(1, 1, s, x, 0);
		update(1, 1, s, x, s, -1);
	}
}

int get(int x, int y) {
	vector< int > v;
	FOR(i, x, y) v.pb(wh[a[i]]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	int last = 1, ans = 0;
	FOR(i, 0, v.size() - 1) ans = (ans + F[last++] * (ll) v[i] % mod) % mod;
	return ans;
}

int main() {

	scanf("%d %d", &n, &mod);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		all.pb(mp(mp(x, y), i));
	}

	sort(all.begin(), all.end(), cmp);

	int l = 1, r = 0;

	foreach(it, all) {
		int x = it->st.st;
		int y = it->st.nd;
		while(r < y) add(a[++r]);
		while(l < x) del(a[l++]);
		while(r > y) del(a[r--]);
		while(l > x) add(a[--l]);
		ans[it->nd] = get();
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 1e5 + 5;
const int sq = 1;

class node {
	public:
	int sum1, sum0, cnt, L;
} ST[N << 2];

int F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all[N];
map< int , int > H, w;

int get(int x) {
	if(x >= 0) return F[x];
	x = -x;
	if(x & 1) return F[x];
	return mod - F[x];
}

node merge(node &x, node l, node r) {
	int t = x.L;
	x.cnt = l.cnt + r.cnt;
	x.sum1 = (get(t+1) * (ll) l.sum1 + get(t) * (ll) l.sum0) % mod;
	x.sum1 = (x.sum1 + get(t+1) * (ll) r.sum1 + get(t) * (ll) r.sum0) % mod;
	x.sum0 = (get(t) * (ll) l.sum1 + get(t-1) * (ll) l.sum0) % mod;
	x.sum0 = (x.sum0 + get(t) * (ll) r.sum1 + get(t-1) * (ll) r.sum0) % mod;
	return x;
}

node assing(int k, int bas, int son, int x, int op) {
	if(bas > x || son < x) return ST[k];
	if(bas == son) {
		if(op == 0) {
			ST[k].sum0 = ST[k].sum1 = ST[k].cnt = 0;
			return ST[k];
		}
		ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
		ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
		ST[k].cnt = 1;
		return ST[k];
	} return merge(ST[k], assing(sol, bas, orta, x, op), assing(sag, orta + 1, son, x, op));
}

node update(int k, int bas, int son, int x, int y, int t) {
	if(bas > y || son < x) return ST[k];
	if(x <= bas && son <= y) {
		ST[k].L += t;
		if(bas == son) {
			if(ST[k].cnt == 0) return ST[k];
			ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
			ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
			return ST[k];
		}
		return merge(ST[k], ST[sol], ST[sag]);
	} return merge(ST[k], update(sol, bas, orta, x, y, t), update(sag, orta + 1, son, x, y, t));
}

int query(int k, int bas, int son, int x, int y) {
	if(bas > y || son < x) return 0;
	if(x <= bas && son <= y) return ST[k].cnt;
	return query(sol, bas, orta, x, y) + query(sag, orta + 1, son, x, y);
}

int get() { return ST[1].sum1; }

void add(int x) {
	if(!cnt[x]++) {
		assing(1, 1, s, x, 1);
		update(1, 1, s, x, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		assing(1, 1, s, x, 0);
		update(1, 1, s, x, s, -1);
	}
}

int get(int x, int y) {
	vector< int > v;
	FOR(i, x, y) v.pb(a[i]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	int last = 1, ans = 0;
	FOR(i, 0, v.size() - 1) ans = (ans + F[last++] * (ll) wh[v[i]] % mod) % mod;
	return ans;
}

int main() {

	scanf("%d %d", &n, &mod);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	int last = 1;

	FOR(i, 1, n) {
		if(i == n || i % sq == 0) {
			++S;
			start[S] = last, finish[S] = i;
			FOR(j, last, i) belong[j] = S;
			last = i + 1;
		}
	}

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		if(belong[x] == belong[y]) {
			ans[i] = get(x, y);
		}
		else all[belong[x]].pb(mp(mp(y, x), i));
	}

	FOR(i, 1, S) {
		sort(all[i].begin(), all[i].end());
		int last = finish[i];
		foreach(it, all[i]) {
			while(last < it->st.st) add(a[++last]);
			FOR(j, it->st.nd, finish[i]) add(a[j]);
			ans[it->nd] = get();
			FOR(j, it->st.nd, finish[i]) del(a[j]);
		} while(last > start[i]) del(a[last--]);
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 25000;
const int sq = 300;

class node {
	public:
	int sum1, sum0, cnt, L;
} ST[N << 2];

int F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all;
map< int , int > H, w;

bool cmp(pair< pii , int > x, pair< pii , int > y) {
	if(x.st.st / sq == y.st.st / sq) return x.st.nd < y.st.nd;
	return x < y;
}

int get(int x) {
	if(x >= 0) return F[x];
	x = -x;
	if(x & 1) return F[x];
	return mod - F[x];
}

node merge(node &x, node l, node r) {
	int t = x.L;
	x.cnt = l.cnt + r.cnt;
	x.sum1 = (get(t+1) * (ll) l.sum1 + get(t) * (ll) l.sum0) % mod;
	x.sum1 = (x.sum1 + get(t+1) * (ll) r.sum1 + get(t) * (ll) r.sum0) % mod;
	x.sum0 = (get(t) * (ll) l.sum1 + get(t-1) * (ll) l.sum0) % mod;
	x.sum0 = (x.sum0 + get(t) * (ll) r.sum1 + get(t-1) * (ll) r.sum0) % mod;
	return x;
}

node assing(int k, int bas, int son, int x, int op) {
	if(bas > x || son < x) return ST[k];
	if(bas == son) {
		if(op == 0) {
			ST[k].sum0 = ST[k].sum1 = ST[k].cnt = 0;
			return ST[k];
		}
		ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
		ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
		ST[k].cnt = 1;
		return ST[k];
	} return merge(ST[k], assing(sol, bas, orta, x, op), assing(sag, orta + 1, son, x, op));
}

node update(int k, int bas, int son, int x, int y, int t) {
	if(bas > y || son < x) return ST[k];
	if(x <= bas && son <= y) {
		ST[k].L += t;
		if(bas == son) {
			if(ST[k].cnt == 0) return ST[k];
			ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
			ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
			return ST[k];
		}
		return merge(ST[k], ST[sol], ST[sag]);
	} return merge(ST[k], update(sol, bas, orta, x, y, t), update(sag, orta + 1, son, x, y, t));
}

int get() { return ST[1].sum1; }

void add(int x) {
	if(!cnt[x]++) {
		assing(1, 1, s, x, 1);
		update(1, 1, s, x, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		assing(1, 1, s, x, 0);
		update(1, 1, s, x, s, -1);
	}
}

int get(int x, int y) {
	vector< int > v;
	FOR(i, x, y) v.pb(wh[a[i]]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	int last = 1, ans = 0;
	FOR(i, 0, v.size() - 1) ans = (ans + F[last++] * (ll) v[i] % mod) % mod;
	return ans;
}

int main() {

	scanf("%d %d", &n, &mod);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		all.pb(mp(mp(x, y), i));
	}

	sort(all.begin(), all.end(), cmp);

	int l = 1, r = 0;

	foreach(it, all) {
		int x = it->st.st;
		int y = it->st.nd;
		while(r < y) add(a[++r]);
		while(l < x) del(a[l++]);
		while(r > y) del(a[r--]);
		while(l > x) add(a[--l]);
		ans[it->nd] = get();
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 1e3 + 5;
const int sq = 200;

int mod, cnt[N], s, n, m, y, x, t, inv[N], F[N], ans[N], a[N], wh[N];
map< int , int > w, H;
vector< pair< pii , int > > all;

bool cmp(pair< pii , int > x, pair< pii , int > y) {
	if(x.st.st / sq == y.st.st / sq) return x.st.nd < y.st.nd;
	return x < y;
}

class node {
    public:
    int sum1, sum0, cnt, L;
} ST[N << 2];

node update(int k, int bas, int son, int x, int y, int t);

node merge(node x, node y) {
	node t;
	t.L = 0; t.cnt = x.cnt + y.cnt;
	t.sum1 = (x.sum1 + y.sum1) % mod;
	t.sum0 = (x.sum0 + y.sum0) % mod;
	return t;
}

node update(int k, int bas, int son, int x, int y, int t);

void push(int k, int bas, int son) {
	if(ST[k].L) {
		update(sol, bas, orta, bas, orta, ST[k].L);
		update(sag, orta + 1, son, orta + 1, son, ST[k].L);
	} ST[k].L = 0;
}

node update(int k, int bas, int son, int x, int t) {
    if(bas > x || son < x) return ST[k];
    if(bas == son) {
        if(t == 0) {
            ST[k].sum1 = ST[k].sum0 = ST[k].cnt = ST[k].L = 0;
            return ST[k];
        }
        ST[k].sum1 = wh[a[bas]] * (ll) abs(F[t]) % mod;
        ST[k].sum0 = wh[a[bas]] * (ll) abs(F[t-1]) % mod;
        ST[k].cnt = 1;
        return ST[k];
    } push(k, bas, son);
    return ST[k] = merge(update(sol, bas, orta, x, t), update(sag, orta + 1, son, x, t));
}

node update(int k, int bas, int son, int x, int y, int t) {
    if(bas > y || son < x) return ST[k];
    if(x <= bas && son <= y) {
   		ST[k].L += t;
		if(t) {
			int a, b, c, d, l = t;
			if(l>0) {
				a = abs(F[l+1]);
				b = c = abs(l);
				d = abs(F[l]);
			}
			else if(l<0) {
				a = F[-(l+1)];
				b = c = F[-l];
				d = F[-(l-1)];
			}
			a = (a*ST[k].sum0%mod+mod)%mod;
			c = (c*ST[k].sum0%mod+mod)%mod;
			b = (b*ST[k].sum1%mod+mod)%mod;
			d = (d*ST[k].sum1%mod+mod)%mod;
			ST[k].sum0 = (a + b) % mod;
			ST[k].sum1 = (c + d) % mod;
		} ST[k].L = 0;
		return ST[k];
	}  push(k, bas, son);
	return ST[k] = merge(update(sol, bas, orta, x, y, t), update(sag, orta + 1, son, x, y, t));
}

node query(int k, int bas, int son, int x, int y) {
    if(bas > y || son < x) return (node) {0, 0, 0, 0};
    if(x <= bas && son <= y) return ST[k];
    push(k, bas, son);
	return merge(query(sol, bas, orta, x, y), query(sag, orta + 1, son, x, y));
}

void add(int x) {
	x = a[x];
	if(!cnt[x]++) {
        int bef = query(1, 1, s, 1, x - 1).cnt + 1;
        update(1, 1, n, x, bef);
        update(1, 1, n, x + 1, n, 1);
    }
}

void del(int x) {
	x = a[x];
    if(!--cnt[x]) {
        int bef = query(1, 1, s, 1, x - 1).cnt + 1;
        update(1, 1, n, x, 0);
        update(1, 1, n, 1, x - 1, -1);
    }
}

int main() {

    scanf("%d %d", &n, &mod);

    F[1] = 1;
    FOR(i, 2, n)
        F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n/2)
		F[i * 2] = -F[i * 2];

    FOR(i, 1, n) {
        scanf("%d", &a[i]);
        H[a[i]] = 1;
    }

    foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

    FOR(i, 1, n) a[i] = w[a[i]];

	scanf("%d", &m);

    FOR(i, 1, m) {
        scanf("%d %d", &x, &y);
        all.pb(mp(mp(x, y), i));
    }

    sort(all.begin(), all.end());

    int l = 1, r = 0;

    foreach(it, all) {
        while(r < it->st.nd) add(++r);
        while(l > it->st.st) del(--l);
        while(l < it->st.st) del(l++);
        while(r > it->st.nd) del(r--);
		ans[it->nd] = ST[1].sum1;
    }

	FOR(i, 1, m)
    	printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 3e4 + 50;
const int sq = 100;

int sum1[N << 2], sum0[N << 2], L[N << 2];

int l, r, F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all;
map< int , int > H, w;

bool cmp(pair< pii , int > x, pair< pii , int > y) {
	if(x.st.st / sq == y.st.st / sq) return x.st.nd < y.st.nd;
	return x < y;
}

int get(int x) {
	if(x >= 0) return F[x];
	if((-x) & 1) return F[-x]; return mod - F[-x];
}

void merge(int &k) {
	t = L[k];
	l = sol, r = sag;
	sum1[k] = (get(t+1) * sum1[l] + get(t) * sum0[l]) % mod;
	sum1[k] = (sum1[k] + get(t+1) * sum1[r] + get(t) * sum0[r]) % mod;
	sum0[k] = (get(t) * sum1[l] + get(t-1) * sum0[l]) % mod;
	sum0[k] = (sum0[k] + get(t) * sum1[r] + get(t-1) * sum0[r]) % mod;
}

void assign(int k, int bas, int son, int x, int op) {
	if(bas > x || son < x) return ;
	if(bas == son) {
		if(op == 0) {
			sum0[k] = sum1[k] = 0;
			return ;
		}
		sum1[k] = wh[bas] * get(L[k]) % mod;
		sum0[k] = wh[bas] * get(L[k] - 1) % mod;
		return ;
	}
	assign(sol, bas, orta, x, op); assign(sag, orta + 1, son, x, op);
	merge(k);
}

void update(int k, int bas, int son, int x, int y, int t) {
	if(bas > y || son < x) return ;
	if(x <= bas && son <= y) {
		L[k] += t;
		if(bas == son) {
			if(!cnt[bas]) return ;
			sum1[k] = wh[bas] * get(L[k]) % mod;
			sum0[k] = wh[bas] * get(L[k] - 1) % mod;
			return ;
		}
		merge(k);
		return ;
	}
	update(sol, bas, orta, x, y, t); update(sag, orta + 1, son, x, y, t);
	merge(k);
}

int get() { return sum1[1]; }

void add(int x) {
	if(!cnt[x]++) {
		assign(1, 1, s, x, 1);
		update(1, 1, s, x, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		assign(1, 1, s, x, 0);
		update(1, 1, s, x, s, -1);
	}
}

int main() {

	scanf("%d %d", &n, &mod);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		all.pb(mp(mp(x, y), i));
	}

	sort(all.begin(), all.end(), cmp);

	int l = 1, r = 0;

	foreach(it, all) {
		int x = it->st.st;
		int y = it->st.nd;
		while(r < y) add(a[++r]);
		while(l < x) del(a[l++]);
		while(r > y) del(a[r--]);
		while(l > x) add(a[--l]);
		ans[it->nd] = get();
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 1e3 + 5;
const int sq = 200;

int mod, cnt[N], s, n, m, y, x, t, inv[N], F[N], ans[N], a[N], wh[N];
map< int , int > w, H;
vector< pair< pii , int > > all;

bool cmp(pair< pii , int > x, pair< pii , int > y) {
	if(x.st.st / sq == y.st.st / sq) return x.st.nd < y.st.nd;
	return x < y;
}

class node {
    public:
    int sum1, sum0, cnt, L;
} ST[N << 2];

int get(int t0, int t1, int tt) {
	if(tt > 0) return (t1 * (ll) abs(F[tt+1]) % mod + abs(F[tt]) * (ll) t0 % mod + mod) % mod;
	if(tt < 0) return (t1 * (ll) F[-tt-1] % mod + F[-tt] * (ll) t0 % mod + mod) % mod;
	return t1;
}

node update(int k, int bas, int son, int x, int y, int t);

node merge(node x, node y) {
	node t;
	t.L = 0; t.cnt = x.cnt + y.cnt;
	t.sum1 = (x.sum1 + y.sum1) % mod;
	t.sum0 = (x.sum0 + y.sum0) % mod;
	return t;
}

node update(int k, int bas, int son, int x, int y, int t);

void push(int k, int bas, int son) {
	if(ST[k].L)	{
		update(sol, bas, orta, bas, orta, ST[k].L);
		update(sag, orta + 1, son, orta + 1, son, ST[k].L);
	} ST[k].L = 0;
}

node update(int k, int bas, int son, int x, int t) {
    if(bas > x || son < x) return ST[k];
    if(bas == son) {
        if(t == 0) {
            ST[k].sum1 = ST[k].sum0 = ST[k].cnt = ST[k].L = 0;
            return ST[k];
        }
        ST[k].sum1 = wh[a[bas]] * (ll) abs(F[t]) % mod;
        ST[k].sum0 = wh[a[bas]] * (ll) abs(F[t-1]) % mod;
        ST[k].cnt = 1;
        return ST[k];
    } push(k, bas, son);
    return ST[k] = merge(update(sol, bas, orta, x, t), update(sag, orta + 1, son, x, t));
}

node update(int k, int bas, int son, int x, int y, int t) {
    if(bas > y || son < x) return ST[k];
    if(x <= bas && son <= y) {
   		ST[k].L += t;
		int l0 = ST[k].sum0;
		int l1 = ST[k].sum1;
		ST[k].sum0 = get(l0, l1, t - 1);
		ST[k].sum1 = get(l0, l1, t);
		return ST[k];
	}  push(k, bas, son);
    return ST[k] = merge(update(sol, bas, orta, x, y, t), update(sag, orta + 1, son, x, y, t));
}

node query(int k, int bas, int son, int x, int y) {
    if(bas > y || son < x) return (node) {0, 0, 0, 0};
    if(x <= bas && son <= y) return ST[k];
    push(k, bas, son);
	return merge(query(sol, bas, orta, x, y), query(sag, orta + 1, son, x, y));
}

void add(int x) {
	x = a[x];
	if(!cnt[x]++) {
        int bef = query(1, 1, s, 1, x - 1).cnt + 1;
        update(1, 1, n, x, bef);
        update(1, 1, n, x + 1, n, 1);
    }
}

void del(int x) {
	x = a[x];
    if(!--cnt[x]) {
        int bef = query(1, 1, s, 1, x - 1).cnt + 1;
        update(1, 1, n, x, 0);
        update(1, 1, n, 1, x - 1, -1);
    }
}

int main() {

    scanf("%d %d", &n, &mod);

    F[1] = 1;
    FOR(i, 2, n)
        F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n/2)
		F[i * 2] = -F[i * 2];

    FOR(i, 1, n) {
        scanf("%d", &a[i]);
        H[a[i]] = 1;
    }

    foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

    FOR(i, 1, n) a[i] = w[a[i]];

	scanf("%d", &m);

    FOR(i, 1, m) {
        scanf("%d %d", &x, &y);
        all.pb(mp(mp(x, y), i));
    }

    sort(all.begin(), all.end());

    int l = 1, r = 0;

    foreach(it, all) {
        while(r < it->st.nd) add(++r);
        while(l > it->st.st) del(--l);
        while(l < it->st.st) del(l++);
        while(r > it->st.nd) del(r--);
		ans[it->nd] = ST[1].sum1;
    }

	FOR(i, 1, m)
    	printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 1e5 + 5;
const int sq = 300;

class node {
	public:
	int sum1, sum0, cnt, L;
} ST[N << 2];

int F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all[N];
map< int , int > H, w;

int get(int x) {
	if(x >= 0) return F[x];
	x = -x;
	if(x & 1) return F[x];
	return mod - F[x];
}

node merge(node &x, node l, node r) {
	int t = x.L;
	x.cnt = l.cnt + r.cnt;
	x.sum1 = (get(t+1) * (ll) l.sum1 + get(t) * (ll) l.sum0) % mod;
	x.sum1 = (x.sum1 + get(t+1) * (ll) r.sum1 + get(t) * (ll) r.sum0) % mod;
	x.sum0 = (get(t) * (ll) l.sum1 + get(t-1) * (ll) l.sum0) % mod;
	x.sum0 = (x.sum0 + get(t) * (ll) r.sum1 + get(t-1) * (ll) r.sum0) % mod;
	return x;
}

node assing(int k, int bas, int son, int x, int op) {
	if(bas > x || son < x) return ST[k];
	if(bas == son) {
		if(op == 0) {
			ST[k].sum0 = ST[k].sum1 = ST[k].cnt = 0;
			return ST[k];
		}
		ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
		ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
		ST[k].cnt = 1;
		return ST[k];
	} return merge(ST[k], assing(sol, bas, orta, x, op), assing(sag, orta + 1, son, x, op));
}

node update(int k, int bas, int son, int x, int y, int t) {
	if(bas > y || son < x) return ST[k];
	if(x <= bas && son <= y) {
		ST[k].L += t;
		if(bas == son) {
			if(ST[k].cnt == 0) return ST[k];
			ST[k].sum1 = wh[bas] * (ll) get(ST[k].L) % mod;
			ST[k].sum0 = wh[bas] * (ll) get(ST[k].L - 1) % mod;
			return ST[k];
		}
		return merge(ST[k], ST[sol], ST[sag]);
	} return merge(ST[k], update(sol, bas, orta, x, y, t), update(sag, orta + 1, son, x, y, t));
}

int query(int k, int bas, int son, int x, int y) {
	if(bas > y || son < x) return 0;
	if(x <= bas && son <= y) return ST[k].cnt;
	return query(sol, bas, orta, x, y) + query(sag, orta + 1, son, x, y);
}

int get() { return ST[1].sum1; }

void add(int x) {
	if(!cnt[x]++) {
		assing(1, 1, s, x, 1);
		update(1, 1, s, x, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		assing(1, 1, s, x, 0);
		update(1, 1, s, x, s, -1);
	}
}

int get(int x, int y) {
	vector< int > v;
	FOR(i, x, y) v.pb(wh[a[i]]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	int last = 1, ans = 0;
	FOR(i, 0, v.size() - 1) ans = (ans + F[last++] * (ll) v[i] % mod) % mod;
	return ans;
}

int main() {

	scanf("%d %d", &n, &mod);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	int last = 1;

	FOR(i, 1, n) {
		if(i == n || i % sq == 0) {
			++S;
			start[S] = last, finish[S] = i;
			FOR(j, last, i) belong[j] = S;
			last = i + 1;
		}
	}

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		if(belong[x] == belong[y]) {
			ans[i] = get(x, y);
		}
		else all[belong[x]].pb(mp(mp(y, x), i));
	}

	FOR(i, 1, S) {
		sort(all[i].begin(), all[i].end());
		int last = finish[i];
		foreach(it, all[i]) {
			while(last < it->st.st) add(a[++last]);
			FOR(j, it->st.nd, finish[i]) add(a[j]);
			ans[it->nd] = get();
			FOR(j, it->st.nd, finish[i]) del(a[j]);
		} while(last > start[i]) del(a[last--]);
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 3e4 + 50;
const int sq = 150;

int sum1[N << 2], sum0[N << 2], L[N << 2];

int l, r, F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all;
map< int , int > H, w;

bool cmp(pair< pii , int > x, pair< pii , int > y) {
	if(x.st.st / sq == y.st.st / sq) return x.st.nd < y.st.nd;
	return x < y;
}

int get(int x) {
	if(x >= 0) return F[x];
	if((-x) & 1) return F[-x]; return mod - F[-x];
}

void merge(int &k) {
	t = L[k];
	l = sol, r = sag;
	sum1[k] = (get(t+1) * sum1[l] + get(t) * sum0[l]) % mod;
	sum1[k] = (sum1[k] + get(t+1) * sum1[r] + get(t) * sum0[r]) % mod;
	sum0[k] = (get(t) * sum1[l] + get(t-1) * sum0[l]) % mod;
	sum0[k] = (sum0[k] + get(t) * sum1[r] + get(t-1) * sum0[r]) % mod;
}

void assign(int k, int bas, int son, int x, int op) {
	if(bas > x || son < x) return ;
	if(bas == son) {
		if(op == 0) {
			sum0[k] = sum1[k] = 0;
			return ;
		}
		sum1[k] = wh[bas] * get(L[k]) % mod;
		sum0[k] = wh[bas] * get(L[k] - 1) % mod;
		return ;
	}
	assign(sol, bas, orta, x, op); assign(sag, orta + 1, son, x, op);
	merge(k);
}

void update(int k, int bas, int son, int x, int y, int t) {
	if(bas > y || son < x) return ;
	if(x <= bas && son <= y) {
		L[k] += t;
		if(bas == son) {
			if(!cnt[bas]) return ;
			sum1[k] = wh[bas] * get(L[k]) % mod;
			sum0[k] = wh[bas] * get(L[k] - 1) % mod;
			return ;
		}
		merge(k);
		return ;
	}
	update(sol, bas, orta, x, y, t); update(sag, orta + 1, son, x, y, t);
	merge(k);
}

int get() { return sum1[1]; }

void add(int x) {
	if(!cnt[x]++) {
		assign(1, 1, s, x, 1);
		update(1, 1, s, x, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		assign(1, 1, s, x, 0);
		update(1, 1, s, x, s, -1);
	}
}

int main() {

	scanf("%d %d", &n, &mod);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		all.pb(mp(mp(x, y), i));
	}

	sort(all.begin(), all.end(), cmp);

	int l = 1, r = 0;

	foreach(it, all) {
		int x = it->st.st;
		int y = it->st.nd;
		while(r < y) add(a[++r]);
		while(l < x) del(a[l++]);
		while(r > y) del(a[r--]);
		while(l > x) add(a[--l]);
		ans[it->nd] = get();
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int logN = 17;
const int inf = 1e9;
const int N = 3e4 + 50;
const int sq = 150;

class node {
	public:
	int sum1, sum0, cnt, L;
} ST[N << 2];

int F[N], s, ans[N], mod, cnt[N], n, m, x, y, z, a[N], wh[N], start[N], finish[N], belong[N], S, t;
vector< pair< pii , int > > all;
map< int , int > H, w;

bool cmp(pair< pii , int > x, pair< pii , int > y) {
	if(x.st.st / sq == y.st.st / sq) return x.st.nd < y.st.nd;
	return x < y;
}

int get(int x) {
	if(x >= 0) return F[x]; x = -x;
	if(x & 1) return F[x]; return mod - F[x];
}

void merge(node &x, node &l, node &r) {
	int t = x.L;
	x.cnt = l.cnt + r.cnt;
	x.sum1 = (get(t+1) * l.sum1 + get(t) * l.sum0) % mod;
	x.sum1 = (x.sum1 + get(t+1) * r.sum1 + get(t) * r.sum0) % mod;
	x.sum0 = (get(t) * l.sum1 + get(t-1) * l.sum0) % mod;
	x.sum0 = (x.sum0 + get(t) * r.sum1 + get(t-1) * r.sum0) % mod;
}

void assing(int k, int bas, int son, int x, int op) {
	if(bas > x || son < x) return ;
	if(bas == son) {
		if(op == 0) {
			ST[k].sum0 = ST[k].sum1 = ST[k].cnt = 0;
			return ;
		}
		ST[k].sum1 = wh[bas] * get(ST[k].L) % mod;
		ST[k].sum0 = wh[bas] * get(ST[k].L - 1) % mod;
		ST[k].cnt = 1;
		return ;
	}
	assing(sol, bas, orta, x, op); assing(sag, orta + 1, son, x, op);
	merge(ST[k], ST[sol], ST[sag]);
}

void update(int k, int bas, int son, int x, int y, int t) {
	if(bas > y || son < x) return ;
	if(x <= bas && son <= y) {
		ST[k].L += t;
		if(bas == son) {
			if(ST[k].cnt == 0) return ;
			ST[k].sum1 = wh[bas] * get(ST[k].L) % mod;
			ST[k].sum0 = wh[bas] * get(ST[k].L - 1) % mod;
			return ;
		}
		merge(ST[k], ST[sol], ST[sag]);
		return ;
	}
	update(sol, bas, orta, x, y, t); update(sag, orta + 1, son, x, y, t);
	merge(ST[k], ST[sol], ST[sag]);
}

int get() { return ST[1].sum1; }

void add(int x) {
	if(!cnt[x]++) {
		assing(1, 1, s, x, 1);
		update(1, 1, s, x, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		assing(1, 1, s, x, 0);
		update(1, 1, s, x, s, -1);
	}
}

int get(int x, int y) {
	vector< int > v;
	FOR(i, x, y) v.pb(wh[a[i]]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	int last = 1, ans = 0;
	FOR(i, 0, v.size() - 1) ans = (ans + F[last++] * (ll) v[i] % mod) % mod;
	return ans;
}

int main() {

	scanf("%d %d", &n, &mod);

	F[1] = 1;
	FOR(i, 2, n)
		F[i] = (F[i-1] + F[i-2]) % mod;

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
		H[a[i]] = 1;
	}

	foreach(it, H) { w[it->st] = ++s; wh[s] = it->st; }

	FOR(i, 1, n) a[i] = w[a[i]];

	scanf("%d", &m);

	FOR(i, 1, m) {
		scanf("%d %d", &x, &y);
		all.pb(mp(mp(x, y), i));
	}

	sort(all.begin(), all.end(), cmp);

	int l = 1, r = 0;

	foreach(it, all) {
		int x = it->st.st;
		int y = it->st.nd;
		while(r < y) add(a[++r]);
		while(l < x) del(a[l++]);
		while(r > y) del(a[r--]);
		while(l > x) add(a[--l]);
		ans[it->nd] = get();
	}

	FOR(i, 1, m)
		printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define fore(i, n) for(int i = 0; i < n; i++)
#define lop(i, n) for(int i = 1; i <= n; i++)
#define ops(i, n) for(int  i = n-1; i >= 0 ; i--)
#define forall(it, g)  for(typeof(g.begin()) it = g.begin(); it != g.end(); it++)
#define PI  3.141592653589793
#define mod  1000000007
#define inf 2000000000
#define INF -2000000000000000
#define modulo 1000000009
#define MH 1234533333333337
#define MH2 7779845079489377
#define enter endl
//ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using namespace std;
typedef vector <int> vi;
typedef vector <vector <int> > vv;
typedef vector <pair <int,int > >vp;
typedef vector <vector <pair <int ,int > > > vvp;
typedef vector <pair <int ,pair <int ,int > > > vpp;
typedef pair<int,int> pp;
typedef long long ll;
const int M = 30005;
int f[M], a[M];
int n, m;
pp p[M];
inline void writeInt (int n)
{
  int N = n, rev, count = 0;
  rev = N;
  if (N == 0) { putchar('0'); putchar('\n'); return ;}
  while ((rev % 10) == 0) { count++; rev /= 10;}
  rev = 0;
  while (N != 0) { rev = (rev<<3) + (rev<<1) + N % 10; N /= 10;}
  while (rev != 0) { putchar(rev % 10 + '0'); rev /= 10;}
  while (count--) putchar('0');
  putchar('\n');
}
inline int FAST_IO()
{
  char ch;
  int val=0;
  ch=getchar();
  while(ch == '\n' || ch == ' ')
    ch=getchar();
  val=0;
  while(ch >= '0' && ch <= '9')
  {
    val=(val*10)+(ch-'0');
    ch=getchar();
  }
  return val;
}
void calc()
{
    f[0] = f[1] = 1;
    for(int i = 2; i <= n; i++)
    {
        f[i] = f[i - 1] + f[i - 2];
        if(f[i] > m)
        {
            f[i]-=m;
        }
    }
}
int main()
{
    //ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    n = FAST_IO();
    m = FAST_IO();
    calc();
    fore(i, n)
    {
        p[i].f = FAST_IO();
        p[i].s = i + 1;
    }
    sort(p, p + n);
    int q;
    q = FAST_IO();
    while(q--)
    {
        int l, r;
        l = FAST_IO();
        r = FAST_IO();
        ll ans = 0;
        int c = 0;
        int last = -1;
        fore(i, n)
        {
            int x = p[i].s, y = p[i].f;
            if(x >= l && x <= r && y > last)
            {
                ans = ans + 1LL * y * f[c++];
                last = y;
                if(c == r - l + 1)break;
            }
        }
        writeInt(ans%m);
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define fore(i, n) for(int i = 0; i < n; i++)
#define lop(i, n) for(int i = 1; i <= n; i++)
#define ops(i, n) for(int  i = n-1; i >= 0 ; i--)
#define forall(it, g)  for(typeof(g.begin()) it = g.begin(); it != g.end(); it++)
#define PI  3.141592653589793
#define mod  1000000007
#define inf 2000000000
#define INF -2000000000000000
#define modulo 1000000009
#define MH 1234533333333337
#define MH2 7779845079489377
#define enter endl
//ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using namespace std;
typedef vector <int> vi;
typedef vector <vector <int> > vv;
typedef vector <pair <int,int > >vp;
typedef vector <vector <pair <int ,int > > > vvp;
typedef vector <pair <int ,pair <int ,int > > > vpp;
typedef pair<int,int> pp;
typedef long long ll;
const int M = 30005;
int f[M], a[M];
int n, m;
pp p[M];
void calc()
{
    f[0] = f[1] = 1;
    for(int i = 2; i <= n; i++)
    {
        f[i] = f[i - 1] + f[i - 2];
        if(f[i] > m)
        {
            f[i]-=m;
        }
    }
}
int main()
{
    //ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    scanf("%d %d", &n, &m);
    calc();
    fore(i, n)
    {
        scanf("%d", &p[i].f);
        p[i].s = i + 1;
    }
    sort(p, p + n);
    int q;
    scanf("%d", &q);
    a[0] = -1;
    while(q--)
    {
        int l, r;
        scanf("%d %d", &l, &r);
        int ans = 0, c = 0;
        fore(i, n)
        {
            int x = p[i].s, y = p[i].f;
            if(x >= l && x <= r && y > a[c])
            {
                ans = (ans + 1LL * y * f[c++])%m;
                a[c] = y;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define fore(i, n) for(int i = 0; i < n; i++)
#define lop(i, n) for(int i = 1; i <= n; i++)
#define ops(i, n) for(int  i = n-1; i >= 0 ; i--)
#define forall(it, g)  for(typeof(g.begin()) it = g.begin(); it != g.end(); it++)
#define PI  3.141592653589793
#define mod  1000000007
#define inf 2000000000
#define INF -2000000000000000
#define modulo 1000000009
#define MH 1234533333333337
#define MH2 7779845079489377
#define enter endl
//ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using namespace std;
typedef vector <int> vi;
typedef vector <vector <int> > vv;
typedef vector <pair <int,int > >vp;
typedef vector <vector <pair <int ,int > > > vvp;
typedef vector <pair <int ,pair <int ,int > > > vpp;
typedef pair<int,int> pp;
typedef long long ll;
const int M = 30005;
int f[M];
int n, m;
pp p[M];
void calc()
{
    f[0] = f[1] = 1;
    for(int i = 2; i <= n; i++)
    {
        f[i] = f[i - 1] + f[i - 2];
        if(f[i] > m)
        {
            f[i]-=m;
        }
    }
}
int main()
{
    //ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    scanf("%d %d", &n, &m);
    calc();
    fore(i, n)
    {
        scanf("%d", &p[i].f);
        p[i].s = i + 1;
    }
    sort(p, p + n);
    int q;
    scanf("%d", &q);
    while(q--)
    {
        int l, r;
        scanf("%d %d", &l, &r);
        int ans = 0, c = 0;
        set<int> s;
        fore(i, n)
        {
            int x = p[i].s, y = p[i].f;
            if(x >= l && x <= r)
            {
                if(s.find(y) != s.end())continue;
                ans = (ans + 1LL * y * f[c++])%m;
                s.insert(y);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define fore(i, n) for(int i = 0; i < n; i++)
#define lop(i, n) for(int i = 1; i <= n; i++)
#define ops(i, n) for(int  i = n-1; i >= 0 ; i--)
#define forall(it, g)  for(typeof(g.begin()) it = g.begin(); it != g.end(); it++)
#define PI  3.141592653589793
#define mod  1000000007
#define inf 2000000000
#define INF -2000000000000000
#define modulo 1000000009
#define MH 1234533333333337
#define MH2 7779845079489377
#define enter endl
//ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using namespace std;
typedef vector <int> vi;
typedef vector <vector <int> > vv;
typedef vector <pair <int,int > >vp;
typedef vector <vector <pair <int ,int > > > vvp;
typedef vector <pair <int ,pair <int ,int > > > vpp;
typedef pair<int,int> pp;
typedef long long ll;
const int M = 30005;
int f[M];
int n, m;
pp p[M];
inline void writeInt (int n)
{
  int N = n, rev, count = 0;
  rev = N;
  if (N == 0) { putchar('0'); putchar('\n'); return ;}
  while ((rev % 10) == 0) { count++; rev /= 10;}
  rev = 0;
  while (N != 0) { rev = (rev<<3) + (rev<<1) + N % 10; N /= 10;}
  while (rev != 0) { putchar(rev % 10 + '0'); rev /= 10;}
  while (count--) putchar('0');
  putchar('\n');
}
inline int FAST_IO()
{
  char ch;
  int val=0;
  ch=getchar();
  while(ch == '\n' || ch == ' ')
    ch=getchar();
  val=0;
  while(ch >= '0' && ch <= '9')
  {
    val=(val*10)+(ch-'0');
    ch=getchar();
  }
  return val;
}
void calc()
{
    f[0] = f[1] = 1;
    for(int i = 2; i <= n; i++)
    {
        f[i] = f[i - 1] + f[i - 2];
        if(f[i] > m)
        {
            f[i]-=m;
        }
    }
}
int main()
{
    //ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    n = FAST_IO();
    m = FAST_IO();
    calc();
    fore(i, n)
    {
        p[i].f = FAST_IO();
        p[i].s = i + 1;
    }
    sort(p, p + n);
    int q;
    q = FAST_IO();
    while(q--)
    {
        int l, r;
        l = FAST_IO();
        r = FAST_IO();
        ll ans = 0;
        int c = 0;
        int last = -1;
        fore(i, n)
        {
            int x = p[i].s, y = p[i].f;
            if(x >= l && x <= r && y > last)
            {
                ans = ans + 1LL * y * f[c++];
                last = y;
            }
        }
        writeInt(ans%m);
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define fore(i, n) for(int i = 0; i < n; i++)
#define lop(i, n) for(int i = 1; i <= n; i++)
#define ops(i, n) for(int  i = n-1; i >= 0 ; i--)
#define forall(it, g)  for(typeof(g.begin()) it = g.begin(); it != g.end(); it++)
#define PI  3.141592653589793
#define mod  1000000007
#define inf 2000000000
#define INF -2000000000000000
#define modulo 1000000009
#define MH 1234533333333337
#define MH2 7779845079489377
#define enter endl
//ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using namespace std;
typedef vector <int> vi;
typedef vector <vector <int> > vv;
typedef vector <pair <int,int > >vp;
typedef vector <vector <pair <int ,int > > > vvp;
typedef vector <pair <int ,pair <int ,int > > > vpp;
typedef pair<int,int> pp;
typedef long long ll;
const int M = 30005;
int f[M], a[M];
int n, m;
pp p[M];
inline void writeInt (int n)
{
  int N = n, rev, count = 0;
  rev = N;
  if (N == 0) { putchar('0'); putchar('\n'); return ;}
  while ((rev % 10) == 0) { count++; rev /= 10;}
  rev = 0;
  while (N != 0) { rev = (rev<<3) + (rev<<1) + N % 10; N /= 10;}
  while (rev != 0) { putchar(rev % 10 + '0'); rev /= 10;}
  while (count--) putchar('0');
  putchar('\n');
}
inline int FAST_IO()
{
  char ch;
  int val=0;
  ch=getchar();
  while(ch == '\n' || ch == ' ')
    ch=getchar();
  val=0;
  while(ch >= '0' && ch <= '9')
  {
    val=(val*10)+(ch-'0');
    ch=getchar();
  }
  return val;
}
void calc()
{
    f[0] = f[1] = 1;
    for(int i = 2; i <= n; i++)
    {
        f[i] = f[i - 1] + f[i - 2];
        if(f[i] > m)
        {
            f[i]-=m;
        }
    }
}
int main()
{
    //ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    n = FAST_IO();
    m = FAST_IO();
    calc();
    fore(i, n)
    {
        p[i].f = FAST_IO();
        p[i].s = i + 1;
    }
    sort(p, p + n);
    int q;
    q = FAST_IO();
    a[0] = -1;
    while(q--)
    {
        int l, r;
        l = FAST_IO();
        r = FAST_IO();
        ll ans = 0;
        int c = 0;
        fore(i, n)
        {
            int x = p[i].s, y = p[i].f;
            if(x >= l && x <= r && y > a[c])
            {
                ans = ans + 1LL * y * f[c++];
                a[c] = y;
                if(c == r - l + 1)break;
            }
        }
        writeInt(ans%m);
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define fore(i, n) for(int i = 0; i < n; i++)
#define lop(i, n) for(int i = 1; i <= n; i++)
#define ops(i, n) for(int  i = n-1; i >= 0 ; i--)
#define forall(it, g)  for(typeof(g.begin()) it = g.begin(); it != g.end(); it++)
#define PI  3.141592653589793
#define mod  1000000007
#define inf 2000000000
#define INF -2000000000000000
#define modulo 1000000009
#define MH 1234533333333337
#define MH2 7779845079489377
#define enter endl
//ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using namespace std;
typedef vector <int> vi;
typedef vector <vector <int> > vv;
typedef vector <pair <int,int > >vp;
typedef vector <vector <pair <int ,int > > > vvp;
typedef vector <pair <int ,pair <int ,int > > > vpp;
typedef pair<int,int> pp;
typedef long long ll;
const int M = 30005;
int f[M], a[M];
int n, m;
pp p[M];
inline void writeInt (int n)
{
  int N = n, rev, count = 0;
  rev = N;
  if (N == 0) { putchar('0'); putchar('\n'); return ;}
  while ((rev % 10) == 0) { count++; rev /= 10;}
  rev = 0;
  while (N != 0) { rev = (rev<<3) + (rev<<1) + N % 10; N /= 10;}
  while (rev != 0) { putchar(rev % 10 + '0'); rev /= 10;}
  while (count--) putchar('0');
  putchar('\n');
}
inline int FAST_IO()
{
  char ch;
  int val=0;
  ch=getchar();
  while(ch == '\n' || ch == ' ')
    ch=getchar();
  val=0;
  while(ch >= '0' && ch <= '9')
  {
    val=(val*10)+(ch-'0');
    ch=getchar();
  }
  return val;
}
void calc()
{
    f[0] = f[1] = 1;
    for(int i = 2; i <= n; i++)
    {
        f[i] = f[i - 1] + f[i - 2];
        if(f[i] > m)
        {
            f[i]-=m;
        }
    }
}
int main()
{
    //ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    n = FAST_IO();
    m = FAST_IO();
    calc();
    fore(i, n)
    {
        p[i].f = FAST_IO();
        p[i].s = i + 1;
        p[i].f%=m;
    }
    sort(p, p + n);
    int q;
    q = FAST_IO();
    a[0] = -1;
    while(q--)
    {
        int l, r;
        l = FAST_IO();
        r = FAST_IO();
        int ans = 0, c = 0;
        fore(i, n)
        {
            int x = p[i].s, y = p[i].f;
            if(x >= l && x <= r && y > a[c])
            {
                ans = (ans + y * f[c++])%m;
                a[c] = y;
                if(c == r - l + 1)break;
            }
        }
        writeInt(ans);
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define fore(i, n) for(int i = 0; i < n; i++)
#define lop(i, n) for(int i = 1; i <= n; i++)
#define ops(i, n) for(int  i = n-1; i >= 0 ; i--)
#define forall(it, g)  for(typeof(g.begin()) it = g.begin(); it != g.end(); it++)
#define PI  3.141592653589793
#define mod  1000000007
#define inf 2000000000
#define INF -2000000000000000
#define modulo 1000000009
#define MH 1234533333333337
#define MH2 7779845079489377
#define enter endl
//ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using namespace std;
typedef vector <int> vi;
typedef vector <vector <int> > vv;
typedef vector <pair <int,int > >vp;
typedef vector <vector <pair <int ,int > > > vvp;
typedef vector <pair <int ,pair <int ,int > > > vpp;
typedef pair<int,int> pp;
typedef long long ll;
const int M = 30005;
int f[M], a[M];
int n, m;
pp p[M];
inline void writeInt (int n)
{
  int N = n, rev, count = 0;
  rev = N;
  if (N == 0) { putchar('0'); putchar('\n'); return ;}
  while ((rev % 10) == 0) { count++; rev /= 10;}
  rev = 0;
  while (N != 0) { rev = (rev<<3) + (rev<<1) + N % 10; N /= 10;}
  while (rev != 0) { putchar(rev % 10 + '0'); rev /= 10;}
  while (count--) putchar('0');
  putchar('\n');
}
inline int FAST_IO()
{
  char ch;
  int val=0;
  ch=getchar();
  while(ch == '\n' || ch == ' ')
    ch=getchar();
  val=0;
  while(ch >= '0' && ch <= '9')
  {
    val=(val*10)+(ch-'0');
    ch=getchar();
  }
  return val;
}
void calc()
{
    f[0] = f[1] = 1;
    for(int i = 2; i <= n; i++)
    {
        f[i] = f[i - 1] + f[i - 2];
        if(f[i] > m)
        {
            f[i]-=m;
        }
    }
}
int main()
{
    //ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    n = FAST_IO();
    m = FAST_IO();
    calc();
    fore(i, n)
    {
        p[i].f = FAST_IO();
        p[i].s = i + 1;
    }
    sort(p, p + n);
    int q;
    q = FAST_IO();
    a[0] = -1;
    while(q--)
    {
        int l, r;
        l = FAST_IO();
        r = FAST_IO();
        int ans = 0, c = 0;
        fore(i, n)
        {
            int x = p[i].s, y = p[i].f;
            if(x >= l && x <= r && y > a[c])
            {
                ans = (ans + 1LL * y * f[c++])%m;
                a[c] = y;
                if(c == r - l + 1)break;
            }
        }
        writeInt(ans);
    }
    return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=30010;
int p;

int fi[maxn];
int f(int x) {if (x>=0) return fi[x];else return ((-x)&1)?fi[-x]:p-fi[-x];}

struct Segment_Tree
{
	int n;

	int lsize[maxn];
	int lowbit(int x) {return x&-x;}
	int Query(int x) {int res=0;while (x) res+=lsize[x],x-=lowbit(x);return res;}
	void Add(int x,int c) {while (x<=n) lsize[x]+=c,x+=lowbit(x);}

	int sum1[maxn<<2],sum2[maxn<<2];
	int size[maxn<<2];
	int tag_k[maxn<<2];

	#define ls (o<<1)
	#define rs (o<<1|1)
	#define mid ((l+r)>>1)

	void Update(int o) {sum1[o]=(sum1[ls]+sum1[rs])%p;sum2[o]=(sum2[ls]+sum2[rs])%p;size[o]=size[ls]+size[rs];}
	void Mark(int o,int k) {int x1=(sum1[o]*f(k+1)+sum2[o]*f(k))%p,x2=(sum1[o]*f(k)+sum2[o]*f(k-1))%p;sum1[o]=x1;sum2[o]=x2;tag_k[o]+=k;}

	void Push_Down(int o)
	{
		if (tag_k[o])
		{
			Mark(ls,tag_k[o]);
			Mark(rs,tag_k[o]);
			tag_k[o]=0;
		}
	}

	int Query_Size(int o,int l,int r,int a,int b)
	{
		if (a<=l && r<=b) return size[o];
		Push_Down(o);
		int res=0;
		if (a<=mid) res+=Query_Size(ls,l,mid,a,b);
		if (b> mid) res+=Query_Size(rs,mid+1,r,a,b);
		return res;
	}

	void Change_Add(int o,int l,int r,int x,int v)
	{
		if (l==r) {int w=Query(l);size[o]=1;sum1[o]=v*f(w)%p;sum2[o]=v*f(w-1)%p;return;}
		Push_Down(o);
		if (x<=mid) Change_Add(ls,l,mid,x,v);
		else Change_Add(rs,mid+1,r,x,v);
		Update(o);
	}

	void Change_Delete(int o,int l,int r,int x)
	{
		if (l==r) {size[o]=0;sum1[o]=sum2[o]=0;return;}
		Push_Down(o);
		if (x<=mid) Change_Delete(ls,l,mid,x);
		else Change_Delete(rs,mid+1,r,x);
		Update(o);
	}

	void Mark_Tagk(int o,int l,int r,int a,int b,int c)
	{
		if (a<=l && r<=b) {Mark(o,c);return;}
		Push_Down(o);
		if (a<=mid) Mark_Tagk(ls,l,mid,a,b,c);
		if (b> mid) Mark_Tagk(rs,mid+1,r,a,b,c);
		Update(o);
	}

	void Change(int x,int v,int c)
	{
		Add(x,c);
		if (c==1) Change_Add(1,1,n,x,v);
		else Change_Delete(1,1,n,x);
		if (x<n) Mark_Tagk(1,1,n,x+1,n,c);
	}

	int Result() {return sum1[1];}
}T;

int a[maxn],temp[maxn];
int cnt[maxn];
int ans[maxn];
int n,q,blocksize,ts;

struct Query {int l,r,block,id;bool operator< (const Query &b) const {return block<b.block || block==b.block && r<b.r;}}Q[maxn];

int main()
{
	//freopen("test.in","r",stdin);
	//freopen("test.out","w",stdout);
	scanf("%d%d",&n,&p);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	memcpy(temp,a,sizeof(temp));
	sort(temp+1,temp+1+n);
	ts=unique(temp+1,temp+1+n)-(temp+1);
	for (int i=1;i<=n;i++) a[i]=lower_bound(temp+1,temp+1+ts,a[i])-temp;
	for (int i=1;i<=ts;i++) temp[i]%=p;
	T.n=ts;
	fi[1]=1;fi[2]=1;
	for (int i=3;i<=ts;i++) fi[i]=(fi[i-1]+fi[i-2])%p;
	blocksize=sqrt(n);
	scanf("%d",&q);
	for (int i=1;i<=q;i++) scanf("%d%d",&Q[i].l,&Q[i].r),Q[i].block=Q[i].l/blocksize,Q[i].id=i;
	sort(Q+1,Q+1+q);
	for (int i=1,l=1,r=0;i<=q;i++)
	{
		while (r<Q[i].r) {r++;cnt[a[r]]++;if (cnt[a[r]]==1) T.Change(a[r],temp[a[r]], 1);}
		while (r>Q[i].r) {cnt[a[r]]--;if (cnt[a[r]]==0) T.Change(a[r],temp[a[r]],-1);r--;}
		while (l>Q[i].l) {l--;cnt[a[l]]++;if (cnt[a[l]]==1) T.Change(a[l],temp[a[l]], 1);}
		while (l<Q[i].l) {cnt[a[l]]--;if (cnt[a[l]]==0) T.Change(a[l],temp[a[l]],-1);l++;}
		ans[Q[i].id]=T.Result();
	}
	for (int i=1;i<=q;i++) printf("%d\n",ans[i]);
	return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=30010;
int p;

int fi[maxn];
int f(int x) {if (x>=0) return fi[x];else return ((-x)&1)?fi[x]:p-fi[x];}

struct Segment_Tree
{
	int sum1[maxn<<2],sum2[maxn<<2];
	int size[maxn<<2];
	int tag_k[maxn<<2];
	int n;

	#define ls (o<<1)
	#define rs (o<<1|1)
	#define mid ((l+r)>>1)

	void Update(int o) {sum1[o]=(sum1[ls]+sum1[rs])%p;sum2[o]=(sum2[ls]+sum2[rs])%p;size[o]=size[ls]+size[rs];}
	void Mark(int o,int k) {int x1=(sum1[o]*f(k+1)+sum2[o]*f(k))%p,x2=(sum1[o]*f(k)+sum2[o]*f(k-1));sum1[o]=x1;sum2[o]=x2;tag_k[o]+=k;return;}

	void Push_Down(int o)
	{
		if (tag_k[o])
		{
			Mark(ls,tag_k[o]);
			Mark(rs,tag_k[o]);
			tag_k[o]=0;
		}
	}

	int Query_Size(int o,int l,int r,int a,int b)
	{
		if (a<=l && r<=b) return size[o];
		Push_Down(o);
		int res=0;
		if (a<=mid) res+=Query_Size(ls,l,mid,a,b);
		if (b> mid) res+=Query_Size(rs,mid+1,r,a,b);
		return res;
	}

	void Change_Add(int o,int l,int r,int x,int v,int w)
	{
		if (l==r) {size[o]=1;sum1[o]=v*f(w)%p;sum2[o]=v*f(w-1)%p;return;}
		Push_Down(o);
		if (x<=mid) Change_Add(ls,l,mid,x,v,w);
		else Change_Add(rs,mid+1,r,x,v,w);
		Update(o);
	}

	void Change_Delete(int o,int l,int r,int x)
	{
		if (l==r) {size[o]=0;sum1[o]=sum2[o]=0;return;}
		Push_Down(o);
		if (x<=mid) Change_Delete(ls,l,mid,x);
		else Change_Delete(rs,mid+1,r,x);
		Update(o);
	}

	void Mark_Tagk(int o,int l,int r,int a,int b,int c)
	{
		if (a<=l && r<=b) {Mark(o,c);return;}
		Push_Down(o);
		if (a<=mid) Mark_Tagk(ls,l,mid,a,b,c);
		if (b> mid) Mark_Tagk(rs,mid+1,r,a,b,c);
		Update(o);
	}

	void Change(int x,int v,int c)
	{
		if (c) Change_Add(1,1,n,x,v,(x==1)?1:Query_Size(1,1,n,1,x-1)+1);
		else Change_Delete(1,1,n,x);
		if (x<n) Mark_Tagk(1,1,n,x+1,n,c);
	}

	int Result() {return sum1[1];}
}T;

int a[maxn],temp[maxn];
int cnt[maxn];
int ans[maxn];
int n,q,blocksize,ts;

struct Query {int l,r,block,id;bool operator< (const Query &b) const {return block<b.block || block==b.block && r<b.r;}}Q[maxn];

int main()
{
	//freopen("test.in","r",stdin);
	scanf("%d%d",&n,&p);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	memcpy(temp,a,sizeof(temp));
	sort(temp+1,temp+1+n);
	ts=unique(temp+1,temp+1+n)-(temp+1);
	for (int i=1;i<=n;i++) a[i]=lower_bound(temp+1,temp+1+ts,a[i])-temp;
	T.n=ts;
	fi[1]=1;fi[2]=1;
	for (int i=3;i<=ts;i++) fi[i]=(fi[i-1]+fi[i-2])%p;
	blocksize=sqrt(n)+1;
	scanf("%d",&q);
	for (int i=1;i<=q;i++) scanf("%d%d",&Q[i].l,&Q[i].r),Q[i].block=Q[i].l/blocksize,Q[i].id=i;
	sort(Q+1,Q+1+q);
	for (int i=1,l=1,r=0;i<=q;i++)
	{
		while (r<Q[i].r) {r++;cnt[a[r]]++;if (cnt[a[r]]==1) T.Change(a[r],temp[a[r]], 1);}
		while (r>Q[i].r) {cnt[a[r]]--;if (cnt[a[r]]==0) T.Change(a[r],temp[a[r]],-1);r--;}
		while (l>Q[i].l) {l--;cnt[a[l]]++;if (cnt[a[l]]==1) T.Change(a[l],temp[a[l]], 1);}
		while (l<Q[i].l) {cnt[a[l]]--;if (cnt[a[l]]==0) T.Change(a[l],temp[a[l]],-1);l++;}
		ans[Q[i].id]=T.Result();
	}
	for (int i=1;i<=q;i++) printf("%d\n",ans[i]);
	return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=30010;
int p;

int fi[maxn];
int f(int x) {if (x>=0) return fi[x];else return ((-x)&1)?fi[-x]:p-fi[-x];}

struct Segment_Tree
{
	int sum1[maxn<<2],sum2[maxn<<2];
	int size[maxn<<2];
	int tag_k[maxn<<2];
	int n;

	#define ls (o<<1)
	#define rs (o<<1|1)
	#define mid ((l+r)>>1)

	void Update(int o) {sum1[o]=(sum1[ls]+sum1[rs])%p;sum2[o]=(sum2[ls]+sum2[rs])%p;size[o]=size[ls]+size[rs];}
	void Mark(int o,int k) {int x1=(sum1[o]*f(k+1)+sum2[o]*f(k))%p,x2=(sum1[o]*f(k)+sum2[o]*f(k-1))%p;sum1[o]=x1;sum2[o]=x2;tag_k[o]+=k;return;}

	void Push_Down(int o)
	{
		if (tag_k[o])
		{
			Mark(ls,tag_k[o]);
			Mark(rs,tag_k[o]);
			tag_k[o]=0;
		}
	}

	int Query_Size(int o,int l,int r,int a,int b)
	{
		if (a<=l && r<=b) return size[o];
		Push_Down(o);
		int res=0;
		if (a<=mid) res+=Query_Size(ls,l,mid,a,b);
		if (b> mid) res+=Query_Size(rs,mid+1,r,a,b);
		return res;
	}

	void Change_Add(int o,int l,int r,int x,int v,int w)
	{
		if (l==r) {size[o]=1;sum1[o]=v*f(w)%p;sum2[o]=v*f(w-1)%p;return;}
		Push_Down(o);
		if (x<=mid) Change_Add(ls,l,mid,x,v,w);
		else Change_Add(rs,mid+1,r,x,v,w);
		Update(o);
	}

	void Change_Delete(int o,int l,int r,int x)
	{
		if (l==r) {size[o]=0;sum1[o]=sum2[o]=0;return;}
		Push_Down(o);
		if (x<=mid) Change_Delete(ls,l,mid,x);
		else Change_Delete(rs,mid+1,r,x);
		Update(o);
	}

	void Mark_Tagk(int o,int l,int r,int a,int b,int c)
	{
		if (a<=l && r<=b) {Mark(o,c);return;}
		Push_Down(o);
		if (a<=mid) Mark_Tagk(ls,l,mid,a,b,c);
		if (b> mid) Mark_Tagk(rs,mid+1,r,a,b,c);
		Update(o);
	}

	void Change(int x,int v,int c)
	{
		if (c==1) Change_Add(1,1,n,x,v,(x==1)?1:Query_Size(1,1,n,1,x-1)+1);
		else Change_Delete(1,1,n,x);
		if (x<n) Mark_Tagk(1,1,n,x+1,n,c);
	}

	int Result() {return sum1[1];}
}T;

int a[maxn],temp[maxn];
int cnt[maxn];
int ans[maxn];
int n,q,blocksize,ts;

struct Query {int l,r,block,id;bool operator< (const Query &b) const {return block<b.block || block==b.block && r<b.r;}}Q[maxn];

int main()
{
	//freopen("test.in","r",stdin);
	//freopen("test.out","w",stdout);
	scanf("%d%d",&n,&p);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	memcpy(temp,a,sizeof(temp));
	sort(temp+1,temp+1+n);
	ts=unique(temp+1,temp+1+n)-(temp+1);
	for (int i=1;i<=n;i++) a[i]=lower_bound(temp+1,temp+1+ts,a[i])-temp;
	T.n=ts;
	fi[1]=1;fi[2]=1;
	for (int i=3;i<=ts;i++) fi[i]=(fi[i-1]+fi[i-2])%p;
	blocksize=sqrt(n)+1;
	scanf("%d",&q);
	for (int i=1;i<=q;i++) scanf("%d%d",&Q[i].l,&Q[i].r),Q[i].block=Q[i].l/blocksize,Q[i].id=i;
	sort(Q+1,Q+1+q);
	for (int i=1,l=1,r=0;i<=q;i++)
	{
		while (r<Q[i].r) {r++;cnt[a[r]]++;if (cnt[a[r]]==1) T.Change(a[r],temp[a[r]], 1);}
		while (r>Q[i].r) {cnt[a[r]]--;if (cnt[a[r]]==0) T.Change(a[r],temp[a[r]],-1);r--;}
		while (l>Q[i].l) {l--;cnt[a[l]]++;if (cnt[a[l]]==1) T.Change(a[l],temp[a[l]], 1);}
		while (l<Q[i].l) {cnt[a[l]]--;if (cnt[a[l]]==0) T.Change(a[l],temp[a[l]],-1);l++;}
		ans[Q[i].id]=T.Result();
	}
	for (int i=1;i<=q;i++) printf("%d\n",ans[i]);
	return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=30010;
int p;

int fi[maxn];
int f(int x) {if (x>=0) return fi[x];else return ((-x)&1)?fi[-x]:p-fi[-x];}

struct Segment_Tree
{
	int sum1[maxn<<2],sum2[maxn<<2];
	int size[maxn<<2];
	int tag_k[maxn<<2];
	int n;

	#define ls (o<<1)
	#define rs (o<<1|1)
	#define mid ((l+r)>>1)

	void Update(int o) {sum1[o]=(sum1[ls]+sum1[rs])%p;sum2[o]=(sum2[ls]+sum2[rs])%p;size[o]=size[ls]+size[rs];}
	void Mark(int o,int k) {int x1=(sum1[o]*f(k+1)+sum2[o]*f(k))%p,x2=(sum1[o]*f(k)+sum2[o]*f(k-1))%p;sum1[o]=x1;sum2[o]=x2;tag_k[o]+=k;return;}

	void Push_Down(int o)
	{
		if (tag_k[o])
		{
			Mark(ls,tag_k[o]);
			Mark(rs,tag_k[o]);
			tag_k[o]=0;
		}
	}

	int Query_Size(int o,int l,int r,int a,int b)
	{
		if (a<=l && r<=b) return size[o];
		Push_Down(o);
		int res=0;
		if (a<=mid) res+=Query_Size(ls,l,mid,a,b);
		if (b> mid) res+=Query_Size(rs,mid+1,r,a,b);
		return res;
	}

	void Change_Add(int o,int l,int r,int x,int v,int w)
	{
		if (l==r) {size[o]=1;sum1[o]=v*f(w)%p;sum2[o]=v*f(w-1)%p;return;}
		Push_Down(o);
		if (x<=mid) Change_Add(ls,l,mid,x,v,w);
		else Change_Add(rs,mid+1,r,x,v,w);
		Update(o);
	}

	void Change_Delete(int o,int l,int r,int x)
	{
		if (l==r) {size[o]=0;sum1[o]=sum2[o]=0;return;}
		Push_Down(o);
		if (x<=mid) Change_Delete(ls,l,mid,x);
		else Change_Delete(rs,mid+1,r,x);
		Update(o);
	}

	void Mark_Tagk(int o,int l,int r,int a,int b,int c)
	{
		if (a<=l && r<=b) {Mark(o,c);return;}
		Push_Down(o);
		if (a<=mid) Mark_Tagk(ls,l,mid,a,b,c);
		if (b> mid) Mark_Tagk(rs,mid+1,r,a,b,c);
		Update(o);
	}

	void Change(int x,int v,int c)
	{
		if (c==1) Change_Add(1,1,n,x,v,(x==1)?1:Query_Size(1,1,n,1,x-1)+1);
		else Change_Delete(1,1,n,x);
		if (x<n) Mark_Tagk(1,1,n,x+1,n,c);
	}

	int Result() {return sum1[1];}
}T;

int a[maxn],temp[maxn];
int cnt[maxn];
int ans[maxn];
int n,q,blocksize,ts;

struct Query {int l,r,block,id;bool operator< (const Query &b) const {return block<b.block || block==b.block && r<b.r;}}Q[maxn];

int main()
{
	//freopen("test.in","r",stdin);
	//freopen("test.out","w",stdout);
	scanf("%d%d",&n,&p);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]),a[i]%=p;
	memcpy(temp,a,sizeof(temp));
	sort(temp+1,temp+1+n);
	ts=unique(temp+1,temp+1+n)-(temp+1);
	for (int i=1;i<=n;i++) a[i]=lower_bound(temp+1,temp+1+ts,a[i])-temp;
	T.n=ts;
	fi[1]=1;fi[2]=1;
	for (int i=3;i<=ts;i++) fi[i]=(fi[i-1]+fi[i-2])%p;
	blocksize=sqrt(n)+1;
	scanf("%d",&q);
	for (int i=1;i<=q;i++) scanf("%d%d",&Q[i].l,&Q[i].r),Q[i].block=Q[i].l/blocksize,Q[i].id=i;
	sort(Q+1,Q+1+q);
	for (int i=1,l=1,r=0;i<=q;i++)
	{
		while (r<Q[i].r) {r++;cnt[a[r]]++;if (cnt[a[r]]==1) T.Change(a[r],temp[a[r]], 1);}
		while (r>Q[i].r) {cnt[a[r]]--;if (cnt[a[r]]==0) T.Change(a[r],temp[a[r]],-1);r--;}
		while (l>Q[i].l) {l--;cnt[a[l]]++;if (cnt[a[l]]==1) T.Change(a[l],temp[a[l]], 1);}
		while (l<Q[i].l) {cnt[a[l]]--;if (cnt[a[l]]==0) T.Change(a[l],temp[a[l]],-1);l++;}
		ans[Q[i].id]=T.Result();
	}
	for (int i=1;i<=q;i++) printf("%d\n",ans[i]);
	return 0;
}