#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

struct unit
{int l,r,num;
};
struct unit q[60010];

struct fib
{int x,y;
};
struct fib f1[60010],f2[60010];

struct node
{int now,size,ss,s1,s2;
};
struct node t[300010];

int n,nq,mod,lim,x,y,nl,nr,tot;
int a[60010],b[60010],sa[60010],f[60010];

fib operator +(fib A,fib B){return (fib){(A.x+B.x)%mod,(A.y+B.y)%mod};}
fib operator -(fib A,fib B){return (fib){(A.x-B.x)%mod,(A.y-B.y)%mod};}

bool cmp(unit A,unit B)
{if(A.l/lim!=B.l/lim) return A.l<B.l;
 if((A.l/lim)&1)
 	return A.r<B.r;
 else
 	return A.r>B.r;
}

void update(int k,int p)
{int x,y;
 if(t[p].now==k) return;
 x=t[p].s1;
 y=t[p].s2;
 if(t[p].now<k)
 {	t[p].s1=((long long)f1[k-t[p].now+1].x*x%mod+(long long)f1[k-t[p].now+1].y*y%mod)%mod;
 	t[p].s2=((long long)f1[k+2-t[p].now].x*x%mod+(long long)f1[k+2-t[p].now].y*y%mod)%mod;
 }
 else
 {	t[p].s1=((long long)f2[2+t[p].now-k].x*x%mod+(long long)f2[2+t[p].now-k].y*y%mod)%mod;
 	t[p].s2=((long long)f2[1+t[p].now-k].x*x%mod+(long long)f2[1+t[p].now-k].y*y%mod)%mod;
 }
 t[p].now=k;
}

void change(int p,int l,int r,int k)
{if(l==r)
 {	if((t[p].ss==1 && y==-1) || (t[p].ss==0 && y==1))
	{	if(y==-1)
			t[p].s1=t[p].s2=t[p].size=t[p].now=0;
		else
		{	t[p].now=k+1;
			t[p].s1=(long long)sa[l]*f[k+1]%mod;
			t[p].s2=(long long)sa[l]*f[k+2]%mod;
			t[p].size=1;
		}
	}
	t[p].ss+=y;
 	return;
 }
 if(x<=(l+r)/2)
 {	change(p*2,l,(l+r)/2,k);
 	update(k+t[p*2].size+1,p*2+1);
 }
 else
 	change(p*2+1,(l+r)/2+1,r,k+t[p*2].size);
 t[p].size=t[p*2].size+t[p*2+1].size;
 t[p].now=k+1;
 t[p].s1=(t[p*2].s1+t[p*2+1].s1)%mod;
 t[p].s2=(t[p*2].s2+t[p*2+1].s2)%mod;
}

void trans(int l,int r)
{int i;
 if(nl>r || nr<l)
 {	for(i=nl;i<=nr;i++)
 	{	x=a[i],y=-1;
 		change(1,1,n,0);
 	}
 	for(i=l;i<=r;i++)
 	{	x=a[i],y=1;
 		change(1,1,n,0);
 	}
 	nl=l;
 	nr=r;
 	return;
 }
 while(nr<r)
 {	x=a[nr+1],y=1;
 	change(1,1,n,0);
 	nr++;
 }
 while(nr>r)
 {	x=a[nr],y=-1;
 	change(1,1,n,0);
 	nr--;
 }
 while(nl<l)
 {	x=a[nl],y=-1;
 	change(1,1,n,0);
 	nl++;
 }
 while(nl>l)
 {	x=a[nl-1],y=1;
 	change(1,1,n,0);
 	nl--;
 }
}

int main()
{int i,j;

 scanf("%d%d",&n,&mod);
 lim=(int)sqrt(n);
 for(i=1;i<=n;i++)
 {	scanf("%d",&a[i]);
 	sa[++tot]=a[i];
 }
 sort(sa+1,sa+tot+1);
 for(i=1;i<=n;i++) a[i]=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 scanf("%d",&nq);
 for(i=1;i<=nq;i++)
 {	scanf("%d%d",&q[i].l,&q[i].r);
 	q[i].num=i;
 }
 sort(q+1,q+nq+1,cmp);
 f1[1]=(fib){1,0};
 f1[2]=(fib){0,1};
 for(i=3;i<=n;i++) f1[i]=f1[i-1]+f1[i-2];
 f2[1]=(fib){0,1};
 f2[2]=(fib){1,0};
 for(i=3;i<=n;i++) f2[i]=f2[i-2]-f2[i-1];
 f[1]=f[2]=1;
 for(i=3;i<=n;i++) f[i]=(f[i-1]+f[i-2])%mod;
 for(i=q[1].l;i<=q[1].r;i++)
 {	x=a[i];
 	y=1;
 	change(1,1,n,0);
 }
 nl=q[1].l;
 nr=q[1].r;
 b[q[1].num]=t[1].s1;
 for(i=2;i<=nq;i++)
 {	trans(q[i].l,q[i].r);
 	b[q[i].num]=(t[1].s1+mod)%mod;
 }
 for(i=1;i<=nq;i++)
 	printf("%d\n",b[i]);
 return 0;
}


#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

struct unit
{int l,r,num;
};
struct unit q[60010];

struct fib
{int x,y;
};
struct fib f1[60010],f2[60010];

struct node
{int now,size,ss,s1,s2;
};
struct node t[300010];

int n,nq,mod,lim,x,y,nl,nr,tot;
int a[60010],b[60010],sa[60010],f[60010];

fib operator +(fib A,fib B){return (fib){(A.x+B.x)%mod,(A.y+B.y)%mod};}
fib operator -(fib A,fib B){return (fib){(A.x-B.x)%mod,(A.y-B.y)%mod};}

bool cmp(unit A,unit B)
{if(A.l/lim==B.l/lim) return A.r<B.r;
 return A.l<B.l;
}

void update(int k,int p)
{int x,y;
 if(t[p].now==k) return;
 x=t[p].s1;
 y=t[p].s2;
 if(t[p].now<k)
 {	t[p].s1=((long long)f1[k-t[p].now+1].x*x%mod+(long long)f1[k-t[p].now+1].y*y%mod)%mod;
 	t[p].s2=((long long)f1[k+2-t[p].now].x*x%mod+(long long)f1[k+2-t[p].now].y*y%mod)%mod;
 }
 else
 {	t[p].s1=((long long)f2[2+t[p].now-k].x*x%mod+(long long)f2[2+t[p].now-k].y*y%mod)%mod;
 	t[p].s2=((long long)f2[1+t[p].now-k].x*x%mod+(long long)f2[1+t[p].now-k].y*y%mod)%mod;
 }
 t[p].now=k;
}

void change(int p,int l,int r,int k)
{if(l==r)
 {	if((t[p].ss==1 && y==-1) || (t[p].ss==0 && y==1))
	{	if(y==-1)
			t[p].s1=t[p].s2=t[p].size=t[p].now=0;
		else
		{	t[p].now=k+1;
			t[p].s1=(long long)sa[l]*f[k+1]%mod;
			t[p].s2=(long long)sa[l]*f[k+2]%mod;
			t[p].size=1;
		}
	}
	t[p].ss+=y;
 	return;
 }
 if(x<=(l+r)/2)
 	change(p*2,l,(l+r)/2,k);
 else
 	change(p*2+1,(l+r)/2+1,r,k+t[p*2].size);
 t[p].size=t[p*2].size+t[p*2+1].size;
 update(k+1,p*2);
 update(k+t[p*2].size+1,p*2+1);
 t[p].now=k+1;
 t[p].s1=(t[p*2].s1+t[p*2+1].s1)%mod;
 t[p].s2=(t[p*2].s2+t[p*2+1].s2)%mod;
}

void trans(int l,int r)
{int i;
 if(nl>r || nr<l)
 {	for(i=nl;i<=nr;i++)
 	{	x=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 		y=-1;
 		change(1,1,n,0);
 	}
 	for(i=l;i<=r;i++)
 	{	x=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 		y=1;
 		change(1,1,n,0);
 	}
 	nl=l;
 	nr=r;
 	return;
 }
 while(nr<r)
 {	x=lower_bound(sa+1,sa+tot+1,a[nr+1])-sa;
 	y=1;
 	change(1,1,n,0);
 	nr++;
 }
 while(nr>r)
 {	x=lower_bound(sa+1,sa+tot+1,a[nr])-sa;
 	y=-1;
 	change(1,1,n,0);
 	nr--;
 }
 while(nl<l)
 {	x=lower_bound(sa+1,sa+tot+1,a[nl])-sa;
 	y=-1;
 	change(1,1,n,0);
 	nl++;
 }
 while(nl>l)
 {	x=lower_bound(sa+1,sa+tot+1,a[nl-1])-sa;
 	y=1;
 	change(1,1,n,0);
 	nl--;
 }
}

int main()
{int i,j;

 scanf("%d%d",&n,&mod);
 lim=(int)sqrt(n);
 for(i=1;i<=n;i++)
 {	scanf("%d",&a[i]);
 	sa[++tot]=a[i];
 }
 sort(sa+1,sa+tot+1);
 scanf("%d",&nq);
 for(i=1;i<=nq;i++)
 {	scanf("%d%d",&q[i].l,&q[i].r);
 	q[i].num=i;
 }
 sort(q+1,q+nq+1,cmp);
 f1[1]=(fib){1,0};
 f1[2]=(fib){0,1};
 for(i=3;i<=n;i++) f1[i]=f1[i-1]+f1[i-2];
 f2[1]=(fib){0,1};
 f2[2]=(fib){1,0};
 for(i=3;i<=n;i++) f2[i]=f2[i-2]-f2[i-1];
 f[1]=f[2]=1;
 for(i=3;i<=n;i++) f[i]=(f[i-1]+f[i-2])%mod;
 for(i=q[1].l;i<=q[1].r;i++)
 {	x=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 	y=1;
 	change(1,1,n,0);
 }
 nl=q[1].l;
 nr=q[1].r;
 b[q[1].num]=t[1].s1;
 for(i=2;i<=nq;i++)
 {	trans(q[i].l,q[i].r);
 	b[q[i].num]=t[1].s1;
 }
 for(i=1;i<=nq;i++)
 	printf("%d\n",b[i]);
 return 0;
}


#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

struct unit
{int l,r,num;
};
struct unit q[60010];

struct fib
{int x,y;
};
struct fib f1[60010],f2[60010];

struct node
{int now,size,ss,s1,s2;
};
struct node t[300010];

int n,nq,mod,lim,x,y,nl,nr,tot;
int a[60010],b[60010],sa[60010],f[60010];

fib operator +(fib A,fib B){return (fib){(A.x+B.x)%mod,(A.y+B.y)%mod};}
fib operator -(fib A,fib B){return (fib){(A.x-B.x)%mod,(A.y-B.y)%mod};}

bool cmp(unit A,unit B)
{if(A.l/lim==B.l/lim) return A.r<B.r;
 return A.l<B.l;
}

void update(int k,int p)
{int x,y;
 if(t[p].now==k) return;
 x=t[p].s1;
 y=t[p].s2;
 if(t[p].now<k)
 {	t[p].s1=((long long)f1[k-t[p].now+1].x*x%mod+(long long)f1[k-t[p].now+1].y*y%mod)%mod;
 	t[p].s2=((long long)f1[k+2-t[p].now].x*x%mod+(long long)f1[k+2-t[p].now].y*y%mod)%mod;
 }
 else
 {	t[p].s1=((long long)f2[2+t[p].now-k].x*x%mod+(long long)f2[2+t[p].now-k].y*y%mod)%mod;
 	t[p].s2=((long long)f2[1+t[p].now-k].x*x%mod+(long long)f2[1+t[p].now-k].y*y%mod)%mod;
 }
 t[p].now=k;
}

void change(int p,int l,int r,int k)
{if(l==r)
 {	if((t[p].ss==1 && y==-1) || (t[p].ss==0 && y==1))
	{	if(y==-1)
			t[p].s1=t[p].s2=t[p].size=t[p].now=0;
		else
		{	t[p].now=k+1;
			t[p].s1=(long long)sa[l]*f[k+1]%mod;
			t[p].s2=(long long)sa[l]*f[k+2]%mod;
			t[p].size=1;
		}
	}
	t[p].ss+=y;
 	return;
 }
 if(x<=(l+r)/2)
 	change(p*2,l,(l+r)/2,k);
 else
 	change(p*2+1,(l+r)/2+1,r,k+t[p*2].size);
 t[p].size=t[p*2].size+t[p*2+1].size;
 update(k+1,p*2);
 update(k+t[p*2].size+1,p*2+1);
 t[p].now=k+1;
 t[p].s1=(t[p*2].s1+t[p*2+1].s1)%mod;
 t[p].s2=(t[p*2].s2+t[p*2+1].s2)%mod;
}

void trans(int l,int r)
{int i;
 if(nl>r || nr<l)
 {	for(i=nl;i<=nr;i++)
 	{	x=a[i],y=-1;
 		change(1,1,n,0);
 	}
 	for(i=l;i<=r;i++)
 	{	x=a[i],y=1;
 		change(1,1,n,0);
 	}
 	nl=l;
 	nr=r;
 	return;
 }
 while(nr<r)
 {	x=a[nr+1],y=1;
 	change(1,1,n,0);
 	nr++;
 }
 while(nr>r)
 {	x=a[nr],y=-1;
 	change(1,1,n,0);
 	nr--;
 }
 while(nl<l)
 {	x=a[nl],y=-1;
 	change(1,1,n,0);
 	nl++;
 }
 while(nl>l)
 {	x=a[nl-1],y=1;
 	change(1,1,n,0);
 	nl--;
 }
}

int main()
{int i,j;

 scanf("%d%d",&n,&mod);
 lim=(int)sqrt(n);
 for(i=1;i<=n;i++)
 {	scanf("%d",&a[i]);
 	sa[++tot]=a[i];
 }
 sort(sa+1,sa+tot+1);
 for(i=1;i<=n;i++) a[i]=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 scanf("%d",&nq);
 for(i=1;i<=nq;i++)
 {	scanf("%d%d",&q[i].l,&q[i].r);
 	q[i].num=i;
 }
 sort(q+1,q+nq+1,cmp);
 f1[1]=(fib){1,0};
 f1[2]=(fib){0,1};
 for(i=3;i<=n;i++) f1[i]=f1[i-1]+f1[i-2];
 f2[1]=(fib){0,1};
 f2[2]=(fib){1,0};
 for(i=3;i<=n;i++) f2[i]=f2[i-2]-f2[i-1];
 f[1]=f[2]=1;
 for(i=3;i<=n;i++) f[i]=(f[i-1]+f[i-2])%mod;
 for(i=q[1].l;i<=q[1].r;i++)
 {	x=a[i];
 	y=1;
 	change(1,1,n,0);
 }
 nl=q[1].l;
 nr=q[1].r;
 b[q[1].num]=t[1].s1;
 for(i=2;i<=nq;i++)
 {	trans(q[i].l,q[i].r);
 	b[q[i].num]=(t[1].s1+mod)%mod;
 }
 for(i=1;i<=nq;i++)
 	printf("%d\n",b[i]);
 return 0;
}


#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

struct unit
{int l,r,num;
};
struct unit q[60010];

struct fib
{int x,y;
};
struct fib f1[60010],f2[60010];

struct node
{int now,size,s1,s2;
};
struct node t[300010];

int n,nq,mod,lim,x,y,nl,nr,tot;
int a[60010],b[60010],w[60010],sa[60010],f[60010];

fib operator +(fib A,fib B){return (fib){(A.x+B.x)%mod,(A.y+B.y)%mod};}
fib operator -(fib A,fib B){return (fib){(A.x-B.x)%mod,(A.y-B.y)%mod};}

bool cmp(unit A,unit B)
{if(A.l/lim!=B.l/lim) return A.l<B.l;
 if((A.l/lim)&1)
 	return A.r>B.r;
 else
 	return A.r<B.r;
}

void update(int k,int p)
{int x,y;
 if(t[p].now==k) return;
 x=t[p].s1;
 y=t[p].s2;
 //cout<<k<<" "<<p<<" "<<t[p].now<<" "<<t[p].s1<<" "<<t[p].s2<<endl;
 if(t[p].now<k)
 {	t[p].s1=((long long)f1[k-t[p].now+1].x*x%mod+(long long)f1[k-t[p].now+1].y*y%mod)%mod;
 	t[p].s2=((long long)f1[k+2-t[p].now].x*x%mod+(long long)f1[k+2-t[p].now].y*y%mod)%mod;
 }
 else
 {	t[p].s1=((long long)f2[2+t[p].now-k].x*x%mod+(long long)f2[2+t[p].now-k].y*y%mod)%mod;
 	t[p].s2=((long long)f2[1+t[p].now-k].x*x%mod+(long long)f2[1+t[p].now-k].y*y%mod)%mod;
 }
 //cout<<t[p].s1<<" "<<t[p].s2<<endl;
 t[p].now=k;
}

void change(int p,int l,int r,int k)
{if(l==r)
 {	if(y==-1)
		t[p].s1=t[p].s2=t[p].size=t[p].now=0;
	else
	{	t[p].now=k+1;
		t[p].s1=(long long)sa[l]*f[k+1]%mod;
		t[p].s2=(long long)sa[l]*f[k+2]%mod;
		t[p].size=1;
	}
 	return;
 }
 if(x<=(l+r)/2)
 {	change(p*2,l,(l+r)/2,k);
 	update(k+t[p*2].size+1,p*2+1);
 }
 else
 {	change(p*2+1,(l+r)/2+1,r,k+t[p*2].size);
 	update(k+1,p*2);
 }
 //update(k+t[p*2].size+1,p*2+1);
 //update(k+1,p*2);
 t[p].size=t[p*2].size+t[p*2+1].size;
 t[p].now=k+1;
 t[p].s1=(t[p*2].s1+t[p*2+1].s1)%mod;
 t[p].s2=(t[p*2].s2+t[p*2+1].s2)%mod;
 //cout<<t[p*2].s1<<" "<<t[p*2].s2<<" "<<t[p*2+1].s1<<" "<<t[p*2+1].s2<<endl;
 //cout<<"## "<<p<<" "<<l<<" "<<r<<" "<<t[p].now<<" "<<t[p].s1<<" "<<t[p].s2<<endl;
}

void trans(int l,int r)
{int i;
 /*
 if(nl>r || nr<l)
 {	for(i=nl;i<=nr;i++)
 	{	x=a[i],y=-1;
 		change(1,1,n,0);
 	}
 	for(i=l;i<=r;i++)
 	{	x=a[i],y=1;
 		change(1,1,n,0);
 	}
 	nl=l;
 	nr=r;
 	return;
 }
 */
 //cout<<l<<" "<<r<<" "<<nl<<" "<<nr<<endl;
 while(nr<r)
 {	x=a[nr+1],y=1;
 	w[a[nr+1]]++;
	if(w[a[nr+1]]==1) change(1,1,n,0);
 	nr++;
 	//cout<<"now I "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
 while(nl>l)
 {	x=a[nl-1],y=1;
 	w[a[nl-1]]++;
 	if(w[a[nl-1]]==1) change(1,1,n,0);
 	nl--;
 	//cout<<"now II "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
 while(nr>r)
 {	x=a[nr],y=-1;
 	w[a[nr]]--;
 	if(w[a[nr]]==0) change(1,1,n,0);
 	nr--;
 	//cout<<"now III "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
 while(nl<l)
 {	x=a[nl],y=-1;
 	w[a[nl]]--;
 	if(w[a[nl]]==0) change(1,1,n,0);
 	nl++;
 	//cout<<"now IIII "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
}

int main()
{int i,j;

 scanf("%d%d",&n,&mod);
 lim=(int)sqrt(n);
 for(i=1;i<=n;i++)
 {	scanf("%d",&a[i]);
 	sa[++tot]=a[i];
 }
 sort(sa+1,sa+tot+1);
 for(i=1;i<=n;i++) a[i]=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 scanf("%d",&nq);
 for(i=1;i<=nq;i++)
 {	scanf("%d%d",&q[i].l,&q[i].r);
 	q[i].num=i;
 }
 sort(q+1,q+nq+1,cmp);
 f1[1]=(fib){1,0};
 f1[2]=(fib){0,1};
 for(i=3;i<=n+5;i++) f1[i]=f1[i-1]+f1[i-2];
 f2[1]=(fib){0,1};
 f2[2]=(fib){1,0};
 for(i=3;i<=n+5;i++) f2[i]=f2[i-2]-f2[i-1];
 f[1]=f[2]=1;
 for(i=3;i<=n+5;i++) f[i]=(f[i-1]+f[i-2])%mod;
 for(i=q[1].l;i<=q[1].r;i++)
 {	x=a[i],y=1;
 	w[a[i]]++;
 	if(w[a[i]]==1) change(1,1,n,0);
 }
 nl=q[1].l;
 nr=q[1].r;
 b[q[1].num]=t[1].s1;
 for(i=2;i<=nq;i++)
 {	trans(q[i].l,q[i].r);
 	b[q[i].num]=(t[1].s1+mod)%mod;
 }
 for(i=1;i<=nq;i++)
 	printf("%d\n",b[i]);
 return 0;
}


#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

struct unit
{int l,r,num;
};
struct unit q[60010];

struct fib
{int x,y;
};
struct fib f1[60010],f2[60010];

struct node
{int now,size,ss,s1,s2;
};
struct node t[300010];

int n,nq,mod,lim,x,y,nl,nr,tot;
int a[60010],b[60010],sa[60010],f[60010];

fib operator +(fib A,fib B){return (fib){(A.x+B.x)%mod,(A.y+B.y)%mod};}
fib operator -(fib A,fib B){return (fib){(A.x-B.x)%mod,(A.y-B.y)%mod};}

bool cmp(unit A,unit B)
{if(A.l/lim!=B.l/lim) return A.l<B.l;
 if((A.l/lim)&1)
 	return A.r>B.r;
 else
 	return A.r<B.r;
}

void update(int k,int p)
{int x,y;
 if(t[p].now==k) return;
 x=t[p].s1;
 y=t[p].s2;
 //cout<<k<<" "<<p<<" "<<t[p].now<<" "<<t[p].s1<<" "<<t[p].s2<<endl;
 if(t[p].now<k)
 {	t[p].s1=((long long)f1[k-t[p].now+1].x*x%mod+(long long)f1[k-t[p].now+1].y*y%mod)%mod;
 	t[p].s2=((long long)f1[k+2-t[p].now].x*x%mod+(long long)f1[k+2-t[p].now].y*y%mod)%mod;
 }
 else
 {	t[p].s1=((long long)f2[2+t[p].now-k].x*x%mod+(long long)f2[2+t[p].now-k].y*y%mod)%mod;
 	t[p].s2=((long long)f2[1+t[p].now-k].x*x%mod+(long long)f2[1+t[p].now-k].y*y%mod)%mod;
 }
 //cout<<t[p].s1<<" "<<t[p].s2<<endl;
 t[p].now=k;
}

void change(int p,int l,int r,int k)
{if(l==r)
 {	if((t[p].ss==1 && y==-1) || (t[p].ss==0 && y==1))
	{	if(y==-1)
			t[p].s1=t[p].s2=t[p].size=t[p].now=0;
		else
		{	t[p].now=k+1;
			t[p].s1=(long long)sa[l]*f[k+1]%mod;
			t[p].s2=(long long)sa[l]*f[k+2]%mod;
			t[p].size=1;
		}
	}
	t[p].ss+=y;
 	return;
 }
 if(x<=(l+r)/2)
 {	change(p*2,l,(l+r)/2,k);
 	//update(k+t[p*2].size+1,p*2+1);
 }
 else
 {	change(p*2+1,(l+r)/2+1,r,k+t[p*2].size);
 	//update(k+1,p*2);
 }
 update(k+t[p*2].size+1,p*2+1);
 update(k+1,p*2);
 t[p].size=t[p*2].size+t[p*2+1].size;
 t[p].now=k+1;
 t[p].s1=(t[p*2].s1+t[p*2+1].s1)%mod;
 t[p].s2=(t[p*2].s2+t[p*2+1].s2)%mod;
 //cout<<t[p*2].s1<<" "<<t[p*2].s2<<" "<<t[p*2+1].s1<<" "<<t[p*2+1].s2<<endl;
 //cout<<"## "<<p<<" "<<l<<" "<<r<<" "<<t[p].now<<" "<<t[p].s1<<" "<<t[p].s2<<endl;
}

void trans(int l,int r)
{int i;
 /*
 if(nl>r || nr<l)
 {	for(i=nl;i<=nr;i++)
 	{	x=a[i],y=-1;
 		change(1,1,n,0);
 	}
 	for(i=l;i<=r;i++)
 	{	x=a[i],y=1;
 		change(1,1,n,0);
 	}
 	nl=l;
 	nr=r;
 	return;
 }
 */
 //cout<<l<<" "<<r<<" "<<nl<<" "<<nr<<endl;
 while(nr<r)
 {	x=a[nr+1],y=1;
 	change(1,1,n,0);
 	nr++;
 	//cout<<"now I "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
 while(nl>l)
 {	x=a[nl-1],y=1;
 	change(1,1,n,0);
 	nl--;
 	//cout<<"now II "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
 while(nr>r)
 {	x=a[nr],y=-1;
 	change(1,1,n,0);
 	nr--;
 	//cout<<"now III "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
 while(nl<l)
 {	x=a[nl],y=-1;
 	change(1,1,n,0);
 	nl++;
 	//cout<<"now IIII "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
}

int main()
{int i,j;

 scanf("%d%d",&n,&mod);
 lim=(int)sqrt(n);
 for(i=1;i<=n;i++)
 {	scanf("%d",&a[i]);
 	sa[++tot]=a[i];
 }
 sort(sa+1,sa+tot+1);
 for(i=1;i<=n;i++) a[i]=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 scanf("%d",&nq);
 for(i=1;i<=nq;i++)
 {	scanf("%d%d",&q[i].l,&q[i].r);
 	q[i].num=i;
 }
 sort(q+1,q+nq+1,cmp);
 f1[1]=(fib){1,0};
 f1[2]=(fib){0,1};
 for(i=3;i<=n+5;i++) f1[i]=f1[i-1]+f1[i-2];
 f2[1]=(fib){0,1};
 f2[2]=(fib){1,0};
 for(i=3;i<=n+5;i++) f2[i]=f2[i-2]-f2[i-1];
 f[1]=f[2]=1;
 for(i=3;i<=n+5;i++) f[i]=(f[i-1]+f[i-2])%mod;
 for(i=q[1].l;i<=q[1].r;i++)
 {	x=a[i];
 	y=1;
 	change(1,1,n,0);
 }
 nl=q[1].l;
 nr=q[1].r;
 b[q[1].num]=t[1].s1;
 for(i=2;i<=nq;i++)
 {	trans(q[i].l,q[i].r);
 	b[q[i].num]=(t[1].s1+mod)%mod;
 }
 for(i=1;i<=nq;i++)
 	printf("%d\n",b[i]);
 return 0;
}


#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

struct unit
{int l,r,num;
};
struct unit q[60010];

struct fib
{int x,y;
};
struct fib f1[60010],f2[60010];

struct node
{int now,size,s1,s2;
};
struct node t[300010];

int n,nq,mod,lim,x,y,nl,nr,tot;
int a[60010],b[60010],w[60010],sa[60010],f[60010];

fib operator +(fib A,fib B){return (fib){(A.x+B.x)%mod,(A.y+B.y)%mod};}
fib operator -(fib A,fib B){return (fib){(A.x-B.x)%mod,(A.y-B.y)%mod};}

bool cmp(unit A,unit B)
{if(A.l/lim!=B.l/lim) return A.l<B.l;
 if((A.l/lim)&1)
 	return A.r>B.r;
 else
 	return A.r<B.r;
}

void update(int k,int p)
{int x,y;
 if(t[p].now==k) return;
 x=t[p].s1;
 y=t[p].s2;
 //cout<<k<<" "<<p<<" "<<t[p].now<<" "<<t[p].s1<<" "<<t[p].s2<<endl;
 if(t[p].now<k)
 {	t[p].s1=((long long)f1[k-t[p].now+1].x*x%mod+(long long)f1[k-t[p].now+1].y*y%mod)%mod;
 	t[p].s2=((long long)f1[k+2-t[p].now].x*x%mod+(long long)f1[k+2-t[p].now].y*y%mod)%mod;
 }
 else
 {	t[p].s1=((long long)f2[2+t[p].now-k].x*x%mod+(long long)f2[2+t[p].now-k].y*y%mod)%mod;
 	t[p].s2=((long long)f2[1+t[p].now-k].x*x%mod+(long long)f2[1+t[p].now-k].y*y%mod)%mod;
 }
 //cout<<t[p].s1<<" "<<t[p].s2<<endl;
 t[p].now=k;
}

void change(int p,int l,int r,int k)
{if(l==r)
 {	if(y==-1)
		t[p].s1=t[p].s2=t[p].size=t[p].now=0;
	else
	{	t[p].now=k+1;
		t[p].s1=(long long)sa[l]*f[k+1]%mod;
		t[p].s2=(long long)sa[l]*f[k+2]%mod;
		t[p].size=1;
	}
 	return;
 }
 if(x<=(l+r)/2)
 {	change(p*2,l,(l+r)/2,k);
 	//update(k+t[p*2].size+1,p*2+1);
 }
 else
 {	change(p*2+1,(l+r)/2+1,r,k+t[p*2].size);
 	//update(k+1,p*2);
 }
 update(k+t[p*2].size+1,p*2+1);
 update(k+1,p*2);
 t[p].size=t[p*2].size+t[p*2+1].size;
 t[p].now=k+1;
 t[p].s1=(t[p*2].s1+t[p*2+1].s1)%mod;
 t[p].s2=(t[p*2].s2+t[p*2+1].s2)%mod;
 //cout<<t[p*2].s1<<" "<<t[p*2].s2<<" "<<t[p*2+1].s1<<" "<<t[p*2+1].s2<<endl;
 //cout<<"## "<<p<<" "<<l<<" "<<r<<" "<<t[p].now<<" "<<t[p].s1<<" "<<t[p].s2<<endl;
}

void trans(int l,int r)
{int i;
 /*
 if(nl>r || nr<l)
 {	for(i=nl;i<=nr;i++)
 	{	x=a[i],y=-1;
 		change(1,1,n,0);
 	}
 	for(i=l;i<=r;i++)
 	{	x=a[i],y=1;
 		change(1,1,n,0);
 	}
 	nl=l;
 	nr=r;
 	return;
 }
 */
 //cout<<l<<" "<<r<<" "<<nl<<" "<<nr<<endl;
 while(nr<r)
 {	x=a[nr+1],y=1;
 	w[a[nr+1]]++;
	if(w[a[nr+1]]==1) change(1,1,n,0);
 	nr++;
 	//cout<<"now I "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
 while(nl>l)
 {	x=a[nl-1],y=1;
 	w[a[nl-1]]++;
 	if(w[a[nl-1]]==1) change(1,1,n,0);
 	nl--;
 	//cout<<"now II "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
 while(nr>r)
 {	x=a[nr],y=-1;
 	w[a[nr]]--;
 	if(w[a[nr]]==0) change(1,1,n,0);
 	nr--;
 	//cout<<"now III "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
 while(nl<l)
 {	x=a[nl],y=-1;
 	w[a[nl]]--;
 	if(w[a[nl]]==0) change(1,1,n,0);
 	nl++;
 	//cout<<"now IIII "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
}

int main()
{int i,j;

 scanf("%d%d",&n,&mod);
 lim=(int)sqrt(n);
 for(i=1;i<=n;i++)
 {	scanf("%d",&a[i]);
 	sa[++tot]=a[i];
 }
 sort(sa+1,sa+tot+1);
 for(i=1;i<=n;i++) a[i]=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 scanf("%d",&nq);
 for(i=1;i<=nq;i++)
 {	scanf("%d%d",&q[i].l,&q[i].r);
 	q[i].num=i;
 }
 sort(q+1,q+nq+1,cmp);
 f1[1]=(fib){1,0};
 f1[2]=(fib){0,1};
 for(i=3;i<=n+5;i++) f1[i]=f1[i-1]+f1[i-2];
 f2[1]=(fib){0,1};
 f2[2]=(fib){1,0};
 for(i=3;i<=n+5;i++) f2[i]=f2[i-2]-f2[i-1];
 f[1]=f[2]=1;
 for(i=3;i<=n+5;i++) f[i]=(f[i-1]+f[i-2])%mod;
 for(i=q[1].l;i<=q[1].r;i++)
 {	x=a[i],y=1;
 	w[a[i]]++;
 	if(w[a[i]]==1) change(1,1,n,0);
 }
 nl=q[1].l;
 nr=q[1].r;
 b[q[1].num]=t[1].s1;
 for(i=2;i<=nq;i++)
 {	trans(q[i].l,q[i].r);
 	b[q[i].num]=(t[1].s1+mod)%mod;
 }
 for(i=1;i<=nq;i++)
 	printf("%d\n",b[i]);
 return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define M 30030

const int B = 100;
struct query {
	int x, y, id;
	query(int x = 0, int y = 0, int id = 0) : x(x), y(y), id(id) {};
	inline bool operator<(const query &a) const {
		return x / B != a.x / B ? x / B < a.x / B : y < a.y;
	}
} q[M];

int n, m, Q, a[M], b[M], cnt, f[M], Ans[M];
multiset <int> S;
multiset <int> :: iterator it;

void init() {
	f[1] = 1 % m;
	for (int i = 2; i <= n; i++) {
		f[i] = (f[i-1] + f[i-2]) % m;
	}
}

int main() {
	//freopen("H.in", "r", stdin);
	scanf("%d %d", &n, &m);
	init();
	//B = sqrt(n + 1);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		b[i] = a[i];
	}
	sort(b, b + n);
	cnt = unique(b, b + n) - b;
	for (int i = 0; i < n; i++) {
		a[i] = lower_bound(b, b + n, a[i]) - b;
	}
	for (int i = 0; i < n; i++) {
		b[i] %= m;
	}

	scanf("%d", &Q);
    for (int i = 0; i < Q; i++) {
		scanf("%d %d", &q[i].x, &q[i].y);
		q[i].x--, q[i].y--;
		q[i].id = i;
    }
    sort(q, q + Q);

	int cur, pos;
	for (int i = 0; i < Q; i++) {
		if (i == 0 || q[i].x / B != q[i-1].x / B) {
			pos = cur = q[i].x / B * B;
			S.clear();
		}
		for (; pos <= q[i].y; pos++) {
			S.insert(a[pos]);
		}
		for (int j = cur; j < q[i].x; j++) {
			S.erase(S.find(a[j]));
		}
		int k = -1, cc = 0, ans = 0;
		for (it = S.begin(); it != S.end(); it++) {
			if (k != *it) {
				ans += b[*it] * f[++cc] % m;
			}
			k = *it;
		}
		for (int j = cur; j < q[i].x; j++) {
			S.insert(a[j]);
		}
		Ans[q[i].id] = ans % m;
	}

    for (int i = 0; i < Q; i++) {
		printf("%d\n", Ans[i]);
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define M 30030

const int B = 80;
struct query {
	int x, y, id;
	query(int x = 0, int y = 0, int id = 0) : x(x), y(y), id(id) {};
	inline bool operator<(const query &a) const {
		return x / B != a.x / B ? x / B < a.x / B : y < a.y;
	}
} q[M];

int n, m, Q, a[M], b[M], cnt, f[M], Ans[M];
multiset <int> S;
multiset <int> :: iterator it;

void init() {
	f[1] = 1 % m;
	for (int i = 2; i <= n; i++) {
		f[i] = (f[i-1] + f[i-2]) % m;
	}
}

int main() {
	//freopen("H.in", "r", stdin);
	scanf("%d %d", &n, &m);
	init();
	//B = sqrt(n + 1);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		b[i] = a[i];
	}
	sort(b, b + n);
	cnt = unique(b, b + n) - b;
	for (int i = 0; i < n; i++) {
		a[i] = lower_bound(b, b + n, a[i]) - b;
	}
	for (int i = 0; i < n; i++) {
		b[i] %= m;
	}

	scanf("%d", &Q);
    for (int i = 0; i < Q; i++) {
		scanf("%d %d", &q[i].x, &q[i].y);
		q[i].x--, q[i].y--;
		q[i].id = i;
    }
    sort(q, q + Q);

	int cur, pos;
	for (int i = 0; i < Q; i++) {
		if (i == 0 || q[i].x / B != q[i-1].x / B) {
			pos = cur = q[i].x / B * B;
			S.clear();
		}
		for (; pos <= q[i].y; pos++) {
			S.insert(a[pos]);
		}
		for (int j = cur; j < q[i].x; j++) {
			S.erase(S.find(a[j]));
		}
		int k = -1, cc = 0, ans = 0;
		for (it = S.begin(); it != S.end(); it++) {
			if (k != *it) {
				ans += b[*it] * f[++cc] % m;
			}
			k = *it;
		}
		for (int j = cur; j < q[i].x; j++) {
			S.insert(a[j]);
		}
		Ans[q[i].id] = ans % m;
	}

    for (int i = 0; i < Q; i++) {
		printf("%d\n", Ans[i]);
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define M 30030

int B;
struct query {
	int x, y, id;
	query(int x = 0, int y = 0, int id = 0) : x(x), y(y), id(id) {};
	inline bool operator<(const query &a) const {
		return x / B != a.x / B ? x / B < a.x / B : y < a.y;
	}
} q[M];

int n, m, Q, a[M], b[M], cnt, f[M], Ans[M];
multiset <int> S;
multiset <int> :: iterator it;

void init() {
	f[1] = 1 % m;
	for (int i = 2; i <= n; i++) {
		f[i] = (f[i-1] + f[i-2]) % m;
	}
}

int main() {
	//freopen("H.in", "r", stdin);
	scanf("%d %d", &n, &m);
	init();
	B = sqrt(n + 1);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		b[i] = a[i];
	}
	sort(b, b + n);
	cnt = unique(b, b + n) - b;
	for (int i = 0; i < n; i++) {
		a[i] = lower_bound(b, b + n, a[i]) - b;
	}
	for (int i = 0; i < n; i++) {
		b[i] %= m;
	}

	scanf("%d", &Q);
    for (int i = 0; i < Q; i++) {
		scanf("%d %d", &q[i].x, &q[i].y);
		q[i].x--, q[i].y--;
		q[i].id = i;
    }
    sort(q, q + Q);

	int cur, pos;
	for (int i = 0; i < Q; i++) {
		if (i == 0 || q[i].x / B != q[i-1].x / B) {
			pos = cur = q[i].x / B * B;
			S.clear();
		}
		for (; pos <= q[i].y; pos++) {
			S.insert(a[pos]);
		}
		for (int j = cur; j < q[i].x; j++) {
			S.erase(S.find(a[j]));
		}
		int k = -1, cc = 0, ans = 0;
		for (it = S.begin(); it != S.end(); it++) {
			if (k != *it) {
				ans += b[*it] * f[++cc] % m;
			}
			k = *it;
		}
		for (int j = cur; j < q[i].x; j++) {
			S.insert(a[j]);
		}
		Ans[q[i].id] = ans % m;
	}

    for (int i = 0; i < Q; i++) {
		printf("%d\n", Ans[i]);
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define M 30030

int F[M];
int n;
int mod;
int a[M], id[M];
int nxt[M];
bool cmp(int i, int j) {
    return a[i] < a[j];
}

int m;

int calc(int l, int r) {
    int s = 0, m = 0;
    for (int j, i = 1; i <= n; i++) {
        j = id[i];
        if (j >= l && j <= r) {
            s = (s + F[++m] * a[j]) % mod;
            i = nxt[i];
        }
    }
    return s;
}

int main() {
    scanf("%d %d", &n, &mod);
    F[1] = F[2] = 1 % mod;
    for (int i = 3; i <= n; i++) {
        F[i] = (F[i-1] + F[i-2]) % mod;
    }
    for (int i = 1; i <= n; i++) {
        scanf("%d", a + i);
        id[i] = i;
    }
    sort(id + 1, id + n + 1, cmp);
    nxt[n] = n;
    for (int j = n - 1; j >= 1; j--) {
        if (a[id[j]] == a[id[j+1]]) {
            nxt[j] = nxt[j+1];
        } else {
            nxt[j] = j;
        }
    }
    for (int j = 1; j <= n; j++) {
        a[j] %= mod;
    }
    scanf("%d", &m);
	for (int l, r, i = 1; i <= m; i++) {
		scanf("%d %d", &l, &r);
		printf("%d\n", calc(l, r));
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define M 30030

int B;
struct query {
	int x, y, id;
	query(int x = 0, int y = 0, int id = 0) : x(x), y(y), id(id) {};
	inline bool operator<(const query &a) const {
		return x / B != a.x / B ? x / B < a.x / B : y < a.y;
	}
} q[M];

int n, m, Q, a[M], b[M], cnt, f[M], Ans[M];
multiset <int> S;
multiset <int> :: iterator it;

void init() {
	f[1] = 1 % m;
	for (int i = 2; i <= n; i++) {
		f[i] = (f[i-1] + f[i-2]) % m;
	}
}

int main() {
	//freopen("H.in", "r", stdin);
	scanf("%d %d", &n, &m);
	init();
	B = sqrt(n + 1);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		b[i] = a[i];
	}
	sort(b, b + n);
	cnt = unique(b, b + n) - b;
	for (int i = 0; i < n; i++) {
		a[i] = lower_bound(b, b + n, a[i]) - b;
	}
	for (int i = 0; i < n; i++) {
		b[i] %= m;
	}

	scanf("%d", &Q);
    for (int i = 0; i < Q; i++) {
		scanf("%d %d", &q[i].x, &q[i].y);
		q[i].x--, q[i].y--;
		q[i].id = i;
    }
    sort(q, q + Q);

	int cur, pos;
	for (int i = 0; i < Q; i++) {
		if (i == 0 || q[i].x / B != q[i-1].x / B) {
			pos = cur = q[i].x / B * B;
			S.clear();
		}
		for (; pos <= q[i].y; pos++) {
			S.insert(a[pos]);
		}
		for (int j = cur; j < q[i].x; j++) {
			S.erase(S.find(a[j]));
		}
		int k = -1, cc = 0, ans = 0;
		for (it = S.begin(); it != S.end(); it++) {
			if (k != *it) {
				ans += b[*it] * f[++cc] % m;
			}
			k = *it;
		}
		Ans[q[i].id] = ans % m;
	}

    for (int i = 0; i < Q; i++) {
		printf("%d\n", Ans[i]);
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define M 30030

const int B = 300;
struct query {
	int x, y, id;
	query(int x = 0, int y = 0, int id = 0) : x(x), y(y), id(id) {};
	inline bool operator<(const query &a) const {
		return x / B != a.x / B ? x / B < a.x / B : y < a.y;
	}
} q[M];

int n, m, Q, a[M], b[M], cnt, f[M], Ans[M];
multiset <int> S;
multiset <int> :: iterator it;

void init() {
	f[1] = 1 % m;
	for (int i = 2; i <= n; i++) {
		f[i] = (f[i-1] + f[i-2]) % m;
	}
}

int main() {
	//freopen("H.in", "r", stdin);
	scanf("%d %d", &n, &m);
	init();
	//B = sqrt(n + 1);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		b[i] = a[i];
	}
	sort(b, b + n);
	cnt = unique(b, b + n) - b;
	for (int i = 0; i < n; i++) {
		a[i] = lower_bound(b, b + n, a[i]) - b;
	}
	for (int i = 0; i < n; i++) {
		b[i] %= m;
	}

	scanf("%d", &Q);
    for (int i = 0; i < Q; i++) {
		scanf("%d %d", &q[i].x, &q[i].y);
		q[i].x--, q[i].y--;
		q[i].id = i;
    }
    sort(q, q + Q);

	int cur, pos;
	for (int i = 0; i < Q; i++) {
		if (i == 0 || q[i].x / B != q[i-1].x / B) {
			pos = cur = q[i].x / B * B;
			S.clear();
		}
		for (; pos <= q[i].y; pos++) {
			S.insert(a[pos]);
		}
		for (int j = cur; j < q[i].x; j++) {
			S.erase(S.find(a[j]));
		}
		int k = -1, cc = 0, ans = 0;
		for (it = S.begin(); it != S.end(); it++) {
			if (k != *it) {
				ans += b[*it] * f[++cc] % m;
			}
			k = *it;
		}
		for (int j = cur; j < q[i].x; j++) {
			S.insert(a[j]);
		}
		Ans[q[i].id] = ans % m;
	}

    for (int i = 0; i < Q; i++) {
		printf("%d\n", Ans[i]);
    }
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <stdio.h>
using namespace std;
int q, a[1000], s[1000], d[1000];
int bp(int w) {
  int c = 0, v;
  for (v = 1<<9; v; v >>= 1)
    if ((c + v < q) && (a[c + v] <= w))
      c += v;
  if (a[c] != w)
    return -1;
  return c;
}
int main(){
  #ifdef Vlad_kv
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
  #endif
  int w, e, r, t, ans = 1, c, v;
  scanf("%d", &q);
  for (w = 0; w < q; w++)
    scanf("%d", &a[w]);
  for (w = 0; w < q; w++)
    for (e = w + 1; e < q; e++)
      if (a[w] > a[e]) {
        t = a[w];
        a[w] = a[e];
        a[e] = t;
      }
  t = 1;
  s[0] = 1;
  for (w = 1; w < q; w++) {
    if (a[w] != a[t - 1]) {
      a[t] = a[w];
      s[t] = 1;
      t++;
    }else
      s[t - 1]++;
  }
  q = t;
  for (e = 0; e < q; e++)
    for (r = 0; r < q; r++) {
      s[e]--;
      s[r]--;
      d[0] = e;
      d[1] = r;
      t = 2;
      if (s[e] >= 0) {
        while (1) {
          c = a[d[t - 1]] + a[d[t - 2]];
          v = bp(c);
          if ((v < 0) || (s[v] == 0))
            break;
          s[v]--;
          d[t] = v;
          t++;
        }
        ans = max(ans, t);
      }
      for (t--; t>=0; t--) {
        s[d[t]]++;
      }
    }
  printf("%d", ans);
  return 0;
}


#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define pb push_back
#define mp make_pair
#define x first
#define y second

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;

template <class T>
void read(T &x)
{
	char ch;
	for (ch=getchar();(ch<'0'||ch>'9')&&ch!='-';) ch=getchar();
	x=0;int t=1;if (ch=='-') {ch=getchar();t=-1;}
	for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
	x*=t;
}

const int N=30010;
const int p=175;
int n,m,cnt,a[N],b[N][2],c[N],F[N],s[N],ans[N];

struct node
{
	int s1,s2,size;
	node operator +(node a)const
	{
		node b;
		b.size=size+a.size;
		b.s1=(s1+F[size+1]*a.s1+F[size]*a.s2)%m;
		if (size) b.s2=(s2+F[size]*a.s1+F[size-1]*a.s2)%m; else b.s2=a.s2;
		return b;
	}
};

struct ques
{
	int x,y,z;
	bool operator <(ques a)const {return y<a.y;}
}q[N];

struct segtree
{
	int lnum,rnum,lch,rch;
	node data;
}tree[2*N];

void build(int k,int l,int r)
{
	tree[k].lnum=l;tree[k].rnum=r;tree[k].data.s1=tree[k].data.s2=tree[k].data.size=0;
	if (l==r) return;
	int mid=(l+r)>>1;
	tree[k].lch=++cnt;build(cnt,l,mid);
	tree[k].rch=++cnt;build(cnt,mid+1,r);
}

void clear(int k)
{
	tree[k].data.s1=tree[k].data.s2=tree[k].data.size=0;
	if (tree[k].lnum==tree[k].rnum) return;
	clear(tree[k].lch);clear(tree[k].rch);
}

void modify(int k,int p,int d)
{
	if (tree[k].lnum==tree[k].rnum)
	{
		tree[k].data.s2=0;
		if (d)
		{
			tree[k].data.size=1;tree[k].data.s1=c[p]%m;
		}
		else
		{
			tree[k].data.size=0;tree[k].data.s1=0;
		}
		return;
	}
	if (p<tree[tree[k].rch].lnum) modify(tree[k].lch,p,d); else modify(tree[k].rch,p,d);
	tree[k].data=tree[tree[k].lch].data+tree[tree[k].rch].data;
}

void add(int k)
{
	if (!s[k]) modify(0,k,1);
	s[k]++;
}

void del(int k)
{
	s[k]--;
	if (!s[k]) modify(0,k,0);
}

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
	read(n);read(m);
	F[1]=1;F[2]=1;
	for (int i=3;i<=n+1;i++) F[i]=(F[i-1]+F[i-2])%m;
	for (int i=1;i<=n;i++) {read(a[i]);c[i]=a[i];}
	sort(c+1,c+n+1);
	int l=1;for (int i=2;i<=n;i++) if (c[i]!=c[i-1]) c[++l]=c[i];
	for (int i=1;i<=n;i++) a[i]=lower_bound(c+1,c+l+1,a[i])-c;
	int Q;read(Q);
	for (int i=1;i<=Q;i++) scanf("%d%d",&b[i][0],&b[i][1]);
	cnt=0;build(0,1,l);
	for (int i=0;i<=n/p;i++)
	{
		clear(0);
		int mm=0;
		for (int j=1;j<=Q;j++)
			if (i*p<=b[j][0]&&b[j][0]<(i+1)*p)
			{
				q[++mm].x=b[j][0];
				q[mm].y=b[j][1];q[mm].z=j;
			}
		if (!mm) continue;
		sort(q+1,q+mm+1);
		memset(s+1,0,l*sizeof(int));
		for (int j=q[1].x;j<=q[1].y;j++) add(a[j]);
		ans[q[1].z]=tree[0].data.s1;
		for (int j=2;j<=mm;j++)
		{
			if (q[j-1].x<q[j].x)
				for (int k=q[j-1].x;k<q[j].x;k++) del(a[k]);
			else
				for (int k=q[j].x;k<q[j-1].x;k++) add(a[k]);
			for (int k=q[j].y;k>q[j-1].y;k--) add(a[k]);
			ans[q[j].z]=tree[0].data.s1;
		}
	}
	for (int i=1;i<=Q;i++) printf("%d\n",ans[i]);
	return 0;
}


#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define pb push_back
#define mp make_pair
#define x first
#define y second

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;

template <class T>
void read(T &x)
{
	char ch;
	for (ch=getchar();(ch<'0'||ch>'9')&&ch!='-';) ch=getchar();
	x=0;int t=1;if (ch=='-') {ch=getchar();t=-1;}
	for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
	x*=t;
}

const int N=30010;
const int p=175;
int n,m,cnt,a[N],b[N][2],c[N],F[N],s[N],ans[N];

struct node
{
	int s1,s2,size;
	node operator +(node a)const
	{
		node b;
		b.size=size+a.size;
		b.s1=(s1+F[size+1]*a.s1+F[size]*a.s2)%m;
		if (size) b.s2=(s2+F[size]*a.s1+F[size-1]*a.s2)%m; else b.s2=a.s2;
		return b;
	}
};

struct ques
{
	int x,y,z;
	bool operator <(ques a)const {return y<a.y;}
}q[N];

struct segtree
{
	int lnum,rnum,lch,rch;
	node data;
}tree[2*N];

void build(int k,int l,int r)
{
	tree[k].lnum=l;tree[k].rnum=r;tree[k].data.s1=tree[k].data.s2=tree[k].data.size=0;
	if (l==r) return;
	int mid=(l+r)>>1;
	tree[k].lch=++cnt;build(cnt,l,mid);
	tree[k].rch=++cnt;build(cnt,mid+1,r);
}

void clear(int k)
{
	tree[k].data.s1=tree[k].data.s2=tree[k].data.size=0;
	if (tree[k].lnum==tree[k].rnum) return;
	clear(tree[k].lch);clear(tree[k].rch);
}

void modify(int k,int p,int d)
{
	if (tree[k].lnum==tree[k].rnum)
	{
		tree[k].data.s2=0;
		if (d)
		{
			tree[k].data.size=1;tree[k].data.s1=c[p]%m;
		}
		else
		{
			tree[k].data.size=0;tree[k].data.s1=0;
		}
		return;
	}
	if (p<tree[tree[k].rch].lnum) modify(tree[k].lch,p,d); else modify(tree[k].rch,p,d);
	tree[k].data=tree[tree[k].lch].data+tree[tree[k].rch].data;
}

void add(int k)
{
	if (!s[k]) modify(0,k,1);
	s[k]++;
}

void del(int k)
{
	s[k]--;
	if (!s[k]) modify(0,k,0);
}

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
	read(n);read(m);
	F[1]=1;F[2]=1;
	for (int i=3;i<=n+1;i++) F[i]=(F[i-1]+F[i-2])%m;
	for (int i=1;i<=n;i++) {read(a[i]);c[i]=a[i];}
	sort(c+1,c+n+1);
	int l=1;for (int i=2;i<=n;i++) if (c[i]!=c[i-1]) c[++l]=c[i];
	for (int i=1;i<=n;i++) a[i]=lower_bound(c+1,c+l+1,a[i])-c;
	int Q;read(Q);
	for (int i=1;i<=Q;i++) scanf("%d%d",&b[i][0],&b[i][1]);
	cnt=0;build(0,1,l);
	for (int i=0;i<=n/p;i++)
	{
		clear(0);
		int mm=0;
		for (int j=1;j<=Q;j++)
			if (i*p<=b[j][0]&&b[j][1]<(i+1)*p)
			{
				q[++mm].x=b[j][0];
				q[mm].y=b[j][1];q[mm].z=j;
			}
		if (!mm) continue;
		sort(q+1,q+mm+1);
		memset(s+1,0,l*sizeof(int));
		for (int j=q[1].x;j<=q[1].y;j++) add(a[j]);
		ans[q[1].z]=tree[0].data.s1;
		for (int j=2;j<=mm;j++)
		{
			if (q[j-1].x<q[j].x)
				for (int k=q[j-1].x;k<q[j].x;k++) del(a[k]);
			else
				for (int k=q[j].x;k<q[j-1].x;k++) add(a[k]);
			for (int k=q[j].y;k>q[j-1].y;k--) add(a[k]);
			ans[q[j].z]=tree[0].data.s1;
		}
	}
	for (int i=1;i<=Q;i++) 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 = 2e5 + 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 FE(int k, int x) {
	if(!k) return 1;
	int temp = FE(k / 2, x);
	temp = (ll) temp * temp % mod;
	if(k & 1) temp = temp * (ll) x % mod;
	return temp;
}

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;
	if(tt < 0) return (t1 * (ll) F[-tt-1] % mod + F[-tt] * (ll) t0 % 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) F[t] % mod;
        ST[k].sum0 = wh[a[bas]] * (ll) 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(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 = 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].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 + 5;
const int sq = 250;

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;

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

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;

int sum1[N << 2], sum0[N << 2], L[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(int k, int l, int r) {
	t = L[k];
	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 assing(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 ;
	}
	assing(sol, bas, orta, x, op); assing(sag, orta + 1, son, x, op);
	merge(k, sol, 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) {
		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, sol, sag);
		return ;
	}
	update(sol, bas, orta, x, y, t); update(sag, orta + 1, son, x, y, t);
	merge(k, sol, sag);
}

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

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) 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 = 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];
		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]);
			assert(last == it->st.st);
			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 = 1e4 + 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;

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

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

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) {
			ST[k].sum1 = w[a[bas]] * (ll) F[ST[k].L] % mod;
			ST[k].sum0 = w[a[bas]] * (ll) F[max(ST[k].L - 1, 0)] % mod;
			ST[k].cnt = !!ST[k].sum1;
			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]++) {
		int bef = query(1, 1, s, 1, x - 1) + 1;
		assing(1, 1, n, x, bef);
		update(1, 1, n, x + 1, n, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		int bef = query(1, 1, s, 1, x - 1) + 1;
		assing(1, 1, n, x, 0);
		update(1, 1, n, x + 1, n, -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] + 1;
		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 = 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 t1, int t0, 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) F[t] % mod;
        ST[k].sum0 = wh[a[bas]] * (ll) 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 = 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 t1, int t0, 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 t0;
}

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 = 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[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];
		FOR(j, 1, (N << 2) - 1) ST[j].cnt = ST[j].sum0 = ST[j].sum1 = ST[j].sum0 = 0;
		foreach(it, all[i]) {
			while(last < it->st.st) add(a[++last]);
			assert(last == it->st.st);
			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 = 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;

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

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

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) {
			ST[k].sum1 = w[a[bas]] * (ll) F[ST[k].L] % mod;
			ST[k].sum0 = w[a[bas]] * (ll) F[max(ST[k].L - 1, 0)] % mod;
			ST[k].cnt = !!ST[k].sum1;
			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]++) {
		int bef = query(1, 1, s, 1, x - 1) + 1;
		assing(1, 1, s, x, bef);
		update(1, 1, s, x + 1, s, +1);
	}
}

void del(int x) {
	if(!--cnt[x]) {
		int bef = query(1, 1, s, 1, x - 1) + 1;
		assing(1, 1, s, x, 0);
		update(1, 1, s, x + 1, 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 = 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) * (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 = 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) * (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];

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) F[t] % mod;
        ST[k].sum0 = wh[a[bas]] * (ll) 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 = 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 t1, int t0, 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 t0;
}

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) F[t] % mod;
        ST[k].sum0 = wh[a[bas]] * (ll) 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;
}