#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<set>
using namespace std;
long long read(){
    long long xx=0,ff=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
    return xx*ff;
}
const int maxn=100010;
long long N,M,w[maxn];
int mod[maxn];
int phi(int x){
	int re=x;
	for(int i=2;i*i<=x;i++)
		if(x%i==0){
			re=re/i*(i-1);
			while(x%i==0)
				x/=i;
		}
	if(x!=1)
		re=re/x*(x-1);
	return re;
}
inline long long fmod(long long x,int md){
	if(x>md)
		return x%md+md;
	return x;
}
long long mypow(long long b,long long p,int md){
	long long re=1;
	while(p){
		if(p&1)
			re=fmod(1LL*re*b,md);
		p>>=1;
		b=fmod(1LL*b*b,md);
	}
	return re;
}
int dfs(int L,int R,int mk){
	int md=mod[mk];
	if(L==R||md==1)
		return fmod(w[L],md);
	return mypow(w[L],dfs(L+1,R,mk+1),md);
}
int main(){
	//freopen("in","r",stdin);
	N=read(),M=read();
	mod[0]=M;
	for(int i=1;i<=N;i++)
		mod[i]=phi(mod[i-1]);
	for(int i=1;i<=N;i++)
		w[i]=read();
	for(int Q=read();Q;Q--){
		int t1=read(),t2=read();
		printf("%d\n",dfs(t1,t2,0)%M);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<set>
using namespace std;
int read(){
    int xx=0,ff=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
    return xx*ff;
}
const int maxn=100010;
int N,M,w[maxn];
int mod[maxn];
int phi(int x){
	int re=x;
	for(int i=2;i*i<=x;i++)
		if(x%i==0){
			re=re/i*(i-1);
			while(x%i==0)
				x/=i;
		}
	if(x!=1)
		re=re/x*(x-1);
	return re;
}
inline int fmod(long long x,int md){
	if(x>md)
		return x%md+md;
	return x;
}
int mypow(int b,int p,int md){
	int re=1;
	while(p){
		if(p&1)
			re=fmod(1LL*re*b,md);
		p>>=1;
		b=fmod(1LL*b*b,md);
	}
	return re;
}
int dfs(int L,int R,int mk){
	int md=mod[mk];
	if(L==R||md==1)
		return fmod(1LL*w[L],md);
	return mypow(w[L],dfs(L+1,R,mk+1),md);
}
int main(){
	//freopen("in","r",stdin);
	N=read(),M=read();
	mod[0]=M;
	for(int i=1;i<=N;i++)
		mod[i]=phi(mod[i-1]);
	for(int i=1;i<=N;i++)
		w[i]=read();
	for(int Q=read();Q;Q--){
		int t1=read(),t2=read();
		printf("%d\n",dfs(t1,t2,0)%M);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<set>
using namespace std;
int read(){
    int xx=0,ff=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
    return xx*ff;
}
const int maxn=100010;
int N,M,w[maxn];
int mod[maxn];
int phi(int x){
	int re=x;
	for(int i=2;i*i<=x;i++)
		if(x%i==0){
			re=re/i*(i-1);
			while(x%i==0)
				x/=i;
		}
	if(x!=1)
		re=re/x*(x-1);
	return re;
}
inline int fmod(long long x,int md){
	if(x>md)
		return x%md+md;
	return x;
}
int mypow(int b,int p,int md){
	int re=1;
	while(p){
		if(p&1)
			re=fmod(1LL*re*b,md);
		p>>=1;
		b=fmod(1LL*b*b,md);
	}
	return re;
}
int dfs(int L,int R,int mk){
	int md=mod[mk];
	if(L==R||md==1)
		return fmod(w[L],md);
	return mypow(w[L],dfs(L+1,R,mk+1),md);
}
int main(){
	//freopen("in","r",stdin);
	N=read(),M=read();
	mod[0]=M;
	for(int i=1;i<=N;i++)
		mod[i]=phi(mod[i-1]);
	for(int i=1;i<=N;i++)
		w[i]=read();
	for(int Q=read();Q;Q--){
		int t1=read(),t2=read();
		printf("%d\n",dfs(t1,t2,0)%M);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<set>
using namespace std;
int read(){
    int xx=0,ff=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
    return xx*ff;
}
const int maxn=100010;
long long N,M,w[maxn];
int mod[maxn];
int phi(int x){
	int re=x;
	for(int i=2;i*i<=x;i++)
		if(x%i==0){
			re=re/i*(i-1);
			while(x%i==0)
				x/=i;
		}
	if(x!=1)
		re=re/x*(x-1);
	return re;
}
inline int fmod(long long x,int md){
	if(x>md)
		return x%md+md;
	return x;
}
int mypow(int b,int p,int md){
	int re=1;
	while(p){
		if(p&1)
			re=fmod(1LL*re*b,md);
		p>>=1;
		b=fmod(1LL*b*b,md);
	}
	return re;
}
int dfs(int L,int R,int mk){
	int md=mod[mk];
	if(L==R||md==1)
		return fmod(w[L],md);
	return mypow(w[L],dfs(L+1,R,mk+1),md);
}
int main(){
	//freopen("in","r",stdin);
	N=read(),M=read();
	mod[0]=M;
	for(int i=1;i<=N;i++)
		mod[i]=phi(mod[i-1]);
	for(int i=1;i<=N;i++)
		w[i]=read();
	for(int Q=read();Q;Q--){
		int t1=read(),t2=read();
		printf("%d\n",dfs(t1,t2,0)%M);
	}
	return 0;
}


#include<bits/stdc++.h>
#define Mod(a,b) a<b?a:a%b+b						//重定义取模，按照欧拉定理的条件
#define LL long long
#define N 100010
using namespace std;

LL n,q,mod,a[N];
map<LL,LL> mp;

LL qpow(LL x,LL n,LL mod)
{
    LL res=1;
    while(n)
    {
        if (n&1) res=Mod(res*x,mod),n--;
        x=Mod(x*x,mod); n>>=1;
    }
    return res;
}

LL phi(LL k)
{
    LL i,s=k,x=k;
    if (mp.count(k)) return mp[x];					//记忆化存储
    for(i = 2;i * i <= k; i++)
    {
        if(k % i == 0) s = s / i * (i - 1);
        while(k % i == 0) k /= i;
    }
    if(k > 1) s = s / k * (k - 1);
    mp[x]=s; return s;
}

LL solve(LL l,LL r,LL mod)
{
    if (l==r||mod==1) return Mod(a[l],mod);					//如果到右端点或者φ值等于1，那么直接返回当前数字
    return qpow(a[l],solve(l+1,r,phi(mod)),mod);			//否则指数为[l+1,r]区间的结果
}

int main()
{
    scanf("%I64d%I64d",&n,&mod);
    for(int i=1;i<=n;i++)
        scanf("%I64d",&a[i]);
    scanf("%I64d",&q);
    while(q--)
    {
        int L,R;
        scanf("%d%d",&L,&R);
        printf("%I64d\n",solve(L,R,mod)%mod);				//对mod取模，因为qpow内部是用Mod(a,b)取模
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<set>
using namespace std;
long long read(){
    long long xx=0,ff=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
    return xx*ff;
}
const int maxn=100010;
long long N,M,w[maxn];
int mod[maxn];
int phi(int x){
	int re=x;
	for(int i=2;i*i<=x;i++)
		if(x%i==0){
			re=re/i*(i-1);
			while(x%i==0)
				x/=i;
		}
	if(x!=1)
		re=re/x*(x-1);
	return re;
}
inline long long fmod(long long x,int md){
	if(x>md)
		return x%md+md;
	return x;
}
long long mypow(long long b,long long p,int md){
	long long re=1;
	while(p){
		if(p&1)
			re=fmod(1LL*re*b,md);
		p>>=1;
		b=fmod(1LL*b*b,md);
	}
	return re;
}
long long dfs(int L,int R,int mk){
	int md=mod[mk];
	if(L==R||md==1)
		return fmod(w[L],md);
	return mypow(w[L],dfs(L+1,R,mk+1),md);
}
int main(){
	//freopen("in","r",stdin);
	N=read(),M=read();
	mod[0]=M;
	for(int i=1;i<=N;i++)
		mod[i]=phi(mod[i-1]);
	for(int i=1;i<=N;i++)
		w[i]=read();
	for(int Q=read();Q;Q--){
		int t1=read(),t2=read();
		printf("%I64d\n",dfs(t1,t2,0)%M);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<set>
using namespace std;
int read(){
    int xx=0,ff=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
    return xx*ff;
}
const int maxn=100010;
int N,M,w[maxn];
int mod[maxn];
int phi(int x){
	int re=x;
	for(int i=2;i*i<=x;i++)
		if(x%i==0){
			re=re/i*(i-1);
			while(x%i==0)
				x/=i;
		}
	if(x!=1)
		re=re/x*(x-1);
	return re;
}
inline int fmod(long long x,int md){
	if(x>md)
		return x%md+md;
	return x;
}
int mypow(long long b,long long p,int md){
	int re=1;
	b=fmod(b,md);
	while(p){
		if(p&1)
			re=fmod(1LL*re*b,md);
		p>>=1;
		b=fmod(1LL*b*b,md);
	}
	return re;
}
int dfs(int L,int R,int mk){
	int md=mod[mk];
	if(L==R)
		return fmod(w[L],md);
	if(md==1)
		return 0;
	return fmod(mypow(w[L],dfs(L+1,R,mk+1),md),md);
}
int main(){
	//freopen("in","r",stdin);
	N=read(),M=read();
	mod[0]=M;
	for(int i=1;i<=N;i++)
		mod[i]=phi(mod[i-1]);
	for(int i=1;i<=N;i++)
		w[i]=read();
	for(int Q=read();Q;Q--){
		int t1=read(),t2=read();
		printf("%d\n",dfs(t1,t2,0)%M);
	}
	return 0;
}


#include<iostream>
using namespace std;
long long n,mod;
long long m;
long long l,r;
long long a[500005];
long long phi[500005];
long long varphi(long long n)
{
	long long ans=n;
	for(int i=2;1ll*i*i<=n;i++)
	{
		if(n%i==0)
		{
			ans=ans/i*(i-1);
			while(!(n%i))
			{
				n/=i;
			}
		}
	}
	if(n>1)
		ans=ans/n*(n-1);
	return ans;
}
long long qkpow(long long a,long long b,long long mod)
{
	if(b==0)
		return 1;
	if(b==1)
		return a;
	long long t=qkpow(a,b/2,mod);
	t=(t*t)%mod;
	if(b%2==1)
		t=(t*a)%mod;
	return t;
}
long long dfs(long long l,long long r,long long k)
{
	if(l==r||phi[k]==1)
	{
		if(a[l]>=phi[k])
			return a[l]%phi[k]+phi[k];
		else
			return a[l];
	}
	return qkpow(a[l],dfs(l+1,r,k+1),phi[k]);
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>mod;
	phi[0]=mod;
	for(int i=1;i<=n;i++)
		phi[i]=varphi(phi[i-1]);
	for(int i=1;i<=n;i++)
		cin>>a[i];
	cin>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>l>>r;
		cout<<dfs(l,r,0)%mod<<'\n';
	}
	return 0;
}


#include<iostream>
using namespace std;
long long n,mod;
long long m;
long long l,r;
long long a[500005];
long long phi[500005];
long long varphi(long long n)
{
	long long ans=n;
	for(int i=2;1ll*i*i<=n;i++)
	{
		if(n%i==0)
		{
			ans=ans/i*(i-1);
			while(!(n%i))
			{
				n/=i;
			}
		}
	}
	if(n>1)
		ans=ans/n*(n-1);
	return ans;
}
long long qkpow(long long a,long long b,long long mod)
{
	if(b==0)
		return 1;
	if(b==1)
		return a;
	long long t=qkpow(a,b/2,mod);
	t=(t*t)%mod;
	if(b%2==1)
		t=(t*a)%mod;
	return t;
}
long long dfs(long long l,long long r,long long k)
{
	if(l==r||phi[k]==1)
	{
		if(a[l]>=phi[k])
			return a[l]=a[l]%phi[k]+phi[k];
		else
			return a[l];
	}
	return qkpow(a[l],dfs(l+1,r,k+1),phi[k]);
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>mod;
	phi[0]=mod;
	for(int i=1;i<=n;i++)
		phi[i]=varphi(phi[i-1]);
	for(int i=1;i<=n;i++)
		cin>>a[i];
	cin>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>l>>r;
		cout<<dfs(l,r,0)%mod<<'\n';
	}
	return 0;
}


#include<iostream>
using namespace std;
long long n,mod;
long long m;
long long l,r;
long long a[500005];
long long phi[500005];
long long varphi(long long n)
{
	long long ans=n;
	for(int i=2;1ll*i*i<=n;i++)
	{
		if(n%i==0)
		{
			ans=ans/i*(i-1);
			while(!(n%i))
			{
				n/=i;
			}
		}
	}
	if(n>1)
		ans=ans/n*(n-1);
	return ans;
}
long long qkpow(long long a,long long b,long long _mod)
{
	if(b==0)
		return 1;
	if(b==1)
		return a;
	long long t=qkpow(a,b/2,_mod);
	t=(t*t)%_mod;
	if(b%2==1)
		t=(t*a)%_mod;
	return t;
}
long long dfs(long long l,long long r,long long k)
{
	if(l==r||phi[k]==1)
	{
		if(a[l]>=phi[k])
			return a[l]%phi[k]+phi[k];
		else
			return a[l];
	}
	return qkpow(a[l],dfs(l+1,r,k+1),phi[k]);
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>mod;
	phi[0]=mod;
	for(int i=1;i<=n;i++)
		phi[i]=varphi(phi[i-1]);
	for(int i=1;i<=n;i++)
		cin>>a[i];
	cin>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>l>>r;
		cout<<dfs(l,r,0)%mod<<'\n';
	}
	return 0;
}


#include<iostream>
using namespace std;
long long n,mod;
long long m;
long long l,r;
long long a[500005];
long long phi[500005];
long long varphi(long long n)
{
	long long ans=n;
	for(int i=2;1ll*i*i<=n;i++)
	{
		if(n%i==0)
		{
			ans=ans/i*(i-1);
			while(!(n%i))
			{
				n/=i;
			}
		}
	}
	if(n>1)
		ans=ans/n*(n-1);
	return ans;
}
long long ex_oular(long long x,long long mod)
{
	if(x>=mod)
		return x=x%mod+mod;
	else
		return x;
}
long long qkpow(long long a,long long b,long long mod)
{
	if(b==0)
		return 1;
	if(b==1)
		return a;
	long long t=qkpow(a,b/2,mod);
	t=ex_oular(t*t,mod);
	if(b%2==1)
		t=ex_oular(t*a,mod);
	return t;
}
long long dfs(long long l,long long r,long long k)
{
	if(l==r||phi[k]==1)
	{
		return ex_oular(a[l],phi[k]);
	}
	return qkpow(a[l],dfs(l+1,r,k+1),phi[k]);
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>mod;
	phi[0]=mod;
	for(int i=1;i<=n;i++)
		phi[i]=varphi(phi[i-1]);
	for(int i=1;i<=n;i++)
		cin>>a[i];
	cin>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>l>>r;
		cout<<dfs(l,r,0)%mod<<'\n';
	}
	return 0;
}


#include<iostream>
using namespace std;
long long n,mod;
long long m;
long long l,r;
long long a[500005];
long long phi[500005];
long long varphi(long long n)
{
	long long ans=n;
	for(int i=2;1ll*i*i<=n;i++)
	{
		if(n%i==0)
		{
			ans=ans/i*(i-1);
			while(!(n%i))
			{
				n/=i;
			}
		}
	}
	if(n>1)
		ans=ans/n*(n-1);
	return ans;
}
long long qkpow(long long a,long long b,long long mod)
{
	if(b==0)
		return 1;
	if(b==1)
		return a;
	long long t=qkpow(a,b/2,mod);
	t=(t*t)%mod;
	if(b%2==1)
		t=(t*a)%mod;
	return t;
}
long long dfs(long long l,long long r,long long k)
{
	if(l==r||phi[k]==1)
	{
		if(a[l]>=phi[k])
			a[l]=a[l]%phi[k]+phi[k];
		else
			return a[l];
	}
	return qkpow(a[l],dfs(l+1,r,k+1),phi[k]);
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>mod;
	phi[0]=mod;
	for(int i=1;i<=n;i++)
		phi[i]=varphi(phi[i-1]);
	for(int i=1;i<=n;i++)
		cin>>a[i];
	cin>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>l>>r;
		cout<<dfs(l,r,0)%mod<<'\n';
	}
	return 0;
}


#include<bits/stdc++.h>
#define Mod(a,b) a<b?a:a%b+b						//重定义取模，按照欧拉定理的条件
#define LL long long
#define N 100010
using namespace std;

LL n,q;
map<LL,LL> mp;

LL qpow(LL x,LL n,LL mod)
{
    LL res=1;
    while(n)
    {
        if (n&1) res=Mod(res*x,mod);
        x=Mod(x*x,mod); n>>=1;
    }
    return res;
}

LL phi(LL k)
{
    LL i,s=k,x=k ;
	if (mp.count(k)) return mp[x];					//记忆化存储
    for(i = 2;i * i <= k; i++)
    {
        if(k % i == 0) s = s / i * (i - 1);
        while(k % i == 0) k /= i;
    }
    if(k > 1) s = s / k * (k - 1);
    return s;
}
LL a,b,mod;
LL solve(LL l,LL r,LL mod)
{
    if (l==r||mod==1) return Mod(a,mod);					//如果到右端点或者φ值等于1，那么直接返回当前数字
    return qpow(a,solve(l+1,r,phi(mod)),mod);			//否则指数为[l+1,r]区间的结果
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		scanf("%lld%lld%lld",&a,&b,&mod);
		if(b==0) printf("%lld\n",1%mod);
		else printf("%lld\n",solve(1,b,mod)%mod);
	}
    return 0;
}


#include<bits/stdc++.h>
#define Mod(a,b) a<b?a:a%b+b						//重定义取模，按照欧拉定理的条件
#define LL long long
#define N 100010
using namespace std;

LL n,q,mod,a[N];
map<LL,LL> mp;

LL qpow(LL x,LL n,LL mod)
{
    LL res=1;
    while(n)
    {
        if (n&1) res=Mod(res*x,mod),n--;
        x=Mod(x*x,mod); n>>=1;
    }
    return res;
}

LL phi(LL k)
{
    LL i,s=k,x=k;
    //if (mp.count(k)) return mp[x];					//记忆化存储
    for(i = 2;i * i <= k; i++)
    {
        if(k % i == 0) s = s / i * (i - 1);
        while(k % i == 0) k /= i;
    }
    if(k > 1) s = s / k * (k - 1);
    mp[x]=s; return s;
}

LL solve(LL l,LL r,LL mod)
{
    if (l==r||mod==1) return Mod(a[l],mod);					//如果到右端点或者φ值等于1，那么直接返回当前数字
    return qpow(a[l],solve(l+1,r,phi(mod)),mod);			//否则指数为[l+1,r]区间的结果
}

int main()
{
    scanf("%lld%lld",&n,&mod);
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    scanf("%lld",&q);
    while(q--)
    {
        int L,R;
        scanf("%d%d",&L,&R);
        printf("%lld\n",solve(L,R,mod)%mod);				//对mod取模，因为qpow内部是用Mod(a,b)取模
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+100;
ll n,mod;
int a[N];
ll phi(ll x)
{
	ll ans=x;
	for(int i=2;i*i<=x;i++)
	{
		if(x%i==0)
		{
			ans=ans*(i-1)/i;
			while(x%i==0) x/=i;
		}
	}
	if(x>1) ans=ans*(x-1)/x;
	return ans;
}
ll qpow(ll a,ll b,ll mod)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
ll solve(ll l,ll r,ll mod)
{
	if(mod==1)return 0;
	if(l==r)return 1;
	int p=phi(mod);
	ll x=solve(l+1,r,p);
	if(x<=p&&x) return qpow(a[l],x,mod);
	else return qpow(a[l],x%p+p,mod);
}
int main()
{
	cin>>n>>mod;
	for(int i=1;i<=n;i++) cin>>a[i];
	int m;
	cin>>m;
	while(m--)
	{
		int l,r;
		cin>>l>>r;
		cout<<solve(l,r+1,mod)%mod<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define Mod(a,b) a<b?a:a%b+b						//重定义取模，按照欧拉定理的条件
#define LL long long
#define N 100010
using namespace std;

LL n,q,mod,a[N];
map<LL,LL> mp;

LL qpow(LL x,LL n,LL mod)
{
    LL res=1;
    while(n)
    {
        if (n&1) res=Mod(res*x,mod),n--;
        x=Mod(x*x,mod); n>>=1;
    }
    return res;
}

LL phi(LL k)
{
    LL i,s=k,x=k;
    if (mp.count(k)) return mp[x];					//记忆化存储
    for(i = 2;i * i <= k; i++)
    {
        if(k % i == 0) s = s / i * (i - 1);
        while(k % i == 0) k /= i;
    }
    if(k > 1) s = s / k * (k - 1);
    mp[x]=s; return s;
}

LL solve(LL l,LL r,LL mod)
{
    if (l==r||mod==1) return Mod(a[l],mod);					//如果到右端点或者φ值等于1，那么直接返回当前数字
    return qpow(a[l],solve(l+1,r,phi(mod)),mod);			//否则指数为[l+1,r]区间的结果
}

int main()
{
    scanf("%lld%lld",&n,&mod);
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    scanf("%lld",&q);
    while(q--)
    {
        int L,R;
        scanf("%d%d",&L,&R);
        printf("%lld\n",solve(L,R,mod)%mod);				//对mod取模，因为qpow内部是用Mod(a,b)取模
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+100;
ll n,mod;
ll a[N];
#define Mod(a,b) a<b?a:a%b+b
ll phi(ll x)
{
	ll ans=x;
	for(ll i=2;i*i<=x;i++)
	{
		if(x%i==0)
		{
			ans=ans/i*(i-1);
			while(x%i==0) x/=i;
		}
	}
	if(x>1) ans=ans/x*(x-1);
	return ans;
}
ll qpow(ll a,ll b,ll mod)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans=Mod(ans*a,mod);
		a=Mod(a*a,mod);
		b>>=1;
	}
	return ans;
}
ll solve(ll l,ll r,ll mod)
{
	if(mod==1) return 0;
	if(l==r) return 1;
	ll p=phi(mod);
	ll x=solve(l+1,r,p);
	if(x<=p&&x) return qpow(a[l],x,mod);
	else return qpow(a[l],x%p+p,mod);
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>mod;
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);;
	int t;
	cin>>t;
	while(t--)
	{
		ll L,R;
        scanf("%lld%lld",&L,&R);
		cout<<solve(L,R+1,mod)%mod<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+100;
ll n,mod;
ll a[N];
#define Mod(a,b) a<b?a:a%b+b
ll phi(ll x)
{
	ll ans=x;
	for(ll i=2;i*i<=x;i++)
	{
		if(x%i==0)
		{
			ans=ans/i*(i-1);
			while(x%i==0) x/=i;
		}
	}
	if(x>1) ans=ans/x*(x-1);
	return ans;
}
ll qpow(ll a,ll b,ll mod)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans=Mod(ans*a,mod);
		a=Mod(a*a,mod);
		b>>=1;
	}
	return ans;
}
ll solve(ll l,ll r,ll mod)
{
	if(mod==1) return 0;
	if(l==r) return 1;
	ll p=phi(mod);
	ll x=solve(l+1,r,p);
	if(x<=p&&x) return qpow(a[l],x,mod);
	else return qpow(a[l],x%p+p,mod);
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>mod;
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);;
	int t;
	cin>>t;
	while(t--)
	{
		int L,R;
        scanf("%d%d",&L,&R);
		cout<<solve(L,R+1,mod)%mod<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+100;
ll n,mod;
ll a[N];
#define Mod(a,b) a<b?a:a%b+b
ll phi(ll x)
{
	ll ans=x;
	for(ll i=2;i*i<=x;i++)
	{
		if(x%i==0)
		{
			ans=ans/i*(i-1);
			while(x%i==0) x/=i;
		}
	}
	if(x>1) ans=ans/x*(x-1);
	return ans;
}
ll qpow(ll a,ll b,ll mod)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans=Mod(ans*a,mod);
		a=Mod(a*a,mod);
		b>>=1;
	}
	return ans;
}
ll solve(ll l,ll r,ll mod)
{
	if(mod==1) return 0;
	if(l==r) return 1;
	ll p=phi(mod);
	ll x=solve(l+1,r,p);
	return qpow(a[l],x,mod);
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>mod;
	for(int i=1;i<=n;i++) cin>>a[i];
	int t;
	cin>>t;
	while(t--)
	{
		ll l,r;
		cin>>l>>r;
		cout<<solve(l,r+1,mod)%mod<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define Mod(a,b) a<b?a:a%b+b						//重定义取模，按照欧拉定理的条件
#define LL long long
#define N 100010
using namespace std;

LL n,q,mod,a[N];
map<LL,LL> mp;

LL qpow(LL x,LL n,LL mod)
{
    LL res=1;
    while(n)
    {
        if (n&1) res=Mod(res*x,mod),n--;
        x=Mod(x*x,mod); n>>=1;
    }
    return res;
}

LL phi(LL k)
{
    LL i,s=k,x=k;
    //if (mp.count(k)) return mp[x];					//记忆化存储
    for(i = 2;i * i <= k; i++)
    {
        if(k % i == 0) s = s / i * (i - 1);
        while(k % i == 0) k /= i;
    }
    //if(k > 1) s = s / k * (k - 1);
    mp[x]=s; return s;
}

LL solve(LL l,LL r,LL mod)
{
    if (l==r||mod==1) return Mod(a[l],mod);					//如果到右端点或者φ值等于1，那么直接返回当前数字
    return qpow(a[l],solve(l+1,r,phi(mod)),mod);			//否则指数为[l+1,r]区间的结果
}

int main()
{
    scanf("%lld%lld",&n,&mod);
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    scanf("%lld",&q);
    while(q--)
    {
        int L,R;
        scanf("%d%d",&L,&R);
        printf("%lld\n",solve(L,R,mod)%mod);				//对mod取模，因为qpow内部是用Mod(a,b)取模
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+100;
ll n,mod;
ll a[N];
#define Mod(a,b) a<b?a:a%b+b
ll phi(ll x)
{
	ll ans=x;
	for(ll i=2;i*i<=x;i++)
	{
		if(x%i==0)
		{
			ans=ans/i*(i-1);
			while(x%i==0) x/=i;
		}
	}
	if(x>1) ans=ans/x*(x-1);
	return ans;
}
ll qpow(ll a,ll b,ll mod)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans=Mod(ans*a,mod);
		a=Mod(a*a,mod);
		b>>=1;
	}
	return ans;
}
ll solve(ll l,ll r,ll mod)
{
	if(mod==1) return 0;
	if(l==r) return 1;
	ll p=phi(mod);
	//cout<<p<<endl;
	ll x=solve(l+1,r,p);
	if(x<=p&&x) return qpow(a[l],x,mod);
	else return qpow(a[l],x%p+p,mod);
}
int main()
{
	cin>>n>>mod;
	for(int i=1;i<=n;i++) cin>>a[i];
	int t;
	cin>>t;
	while(t--)
	{
		ll l,r;
		cin>>l>>r;
		cout<<solve(l,r+1,mod)%mod<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+100;
ll n,mod;
ll a[N];
#define Mod(a,b) a<b?a:a%b+b
ll phi(ll x)
{
	ll ans=x;
	for(ll i=2;i*i<=x;i++)
	{
		if(x%i==0)
		{
			ans=ans/i*(i-1);
			while(x%i==0) x/=i;
		}
	}
	if(x>1) ans=ans/x*(x-1);
	return ans;
}
ll qpow(ll a,ll b,ll mod)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans=Mod(ans*a,mod);
		a=Mod(a*a,mod);
		b>>=1;
	}
	return ans;
}
ll solve(ll l,ll r,ll mod)
{
	if(mod==1) return 0;
	if(l==r) return 1;
	ll p=phi(mod);
	//cout<<p<<endl;
	ll x=solve(l+1,r,p);
	if(x<=p&&x) return qpow(a[l],x,mod);
	else return qpow(a[l],x%p+p,mod);
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>mod;
	for(int i=1;i<=n;i++) cin>>a[i];
	int t;
	cin>>t;
	while(t--)
	{
		ll l,r;
		cin>>l>>r;
		cout<<solve(l,r+1,mod)%mod<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define REP(i,st,ed) for(int i=(int)(st),i##end=(int)(ed);i<=i##end;++i)
#define DREP(i,st,ed) for(int i=(int)(st),i##end=(int)(ed);i>=i##end;--i)
template<typename T>bool chkmin(T &x,const T &y){return x>y?x=y,1:0;}
template<typename T>bool chkmax(T &x,const T &y){return x<y?x=y,1:0;}
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
template<typename T>T read(){
	T x=0,f=1;
	char c=getchar();
	while((c<'0')||(c>'9')){if(c=='-')f=-1;c=getchar();}
	while((c>='0')&&(c<='9'))x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
#define read() read<int>()
template<typename T>void write(T x,char c){
	static char t[25];
	static int tlen;
	t[tlen=1]=c;
	if(x<0)putchar('-'),x=-x;
	do t[++tlen]=(x%10)^48;
	while(x/=10);
	while(tlen)putchar(t[tlen--]);
}
#define pb push_back
typedef long long ll;
typedef double lf;
const int maxn=100005;
int phi(int x){
	int res=x;
	for(int i=2;i*i<=x;++i)
		if(x%i==0){
			res-=res/i;
			while(x%i==0)x/=i;
		}
	if(x>1)res-=res/x;
	return res;
}
int n,m,q_cnt,a[maxn];
int p[105],tot;
int ksm(int x,int y,int mod){
	int res=1;
	while(y){
		if(y&1)res=1ll*res*x%mod;
		x=1ll*x*x%mod,y>>=1;
	}return res;
}
struct data{
	int val,flag;
};
data solve(int l,int r,int k){
	if((l==r)||(k==tot))return (data){a[l]%p[k],a[l]>=p[k]};
	else if(a[l]==1)return (data){1,0};
	int cnt=0,tmp=1;
	while(1ll*tmp*a[l]<p[k])tmp*=a[l],++cnt;
	data t=solve(l+1,r,k+1);
	return (data){ksm(a[l],t.val+t.flag*p[k+1],p[k]),t.val+t.flag*p[k+1]>cnt};
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	n=read(),m=read();
	REP(i,1,n)a[i]=read();
	while(m!=1)p[++tot]=m,m=phi(m);
	q_cnt=read();
	REP(i,1,q_cnt){
		int l=read(),r=read();
		write(solve(l,r,1).val,'\n');
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define REP(i,st,ed) for(int i=(int)(st),i##end=(int)(ed);i<=i##end;++i)
#define DREP(i,st,ed) for(int i=(int)(st),i##end=(int)(ed);i>=i##end;--i)
template<typename T>bool chkmin(T &x,const T &y){return x>y?x=y,1:0;}
template<typename T>bool chkmax(T &x,const T &y){return x<y?x=y,1:0;}
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
template<typename T>T read(){
	T x=0,f=1;
	char c=getchar();
	while((c<'0')||(c>'9')){if(c=='-')f=-1;c=getchar();}
	while((c>='0')&&(c<='9'))x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
#define read() read<int>()
template<typename T>void write(T x,char c){
	static char t[25];
	static int tlen;
	t[tlen=1]=c;
	if(x<0)putchar('-'),x=-x;
	do t[++tlen]=(x%10)^48;
	while(x/=10);
	while(tlen)putchar(t[tlen--]);
}
#define pb push_back
typedef long long ll;
typedef double lf;
const int maxn=100005;
int phi(int x){
	int res=x-1;
	for(int i=2;i*i<=x;++i)
		if(x%i==0)res-=res/i;
	return res;
}
int n,m,q_cnt,a[maxn];
int p[105],tot;
int ksm(int x,int y,int mod){
	int res=1;
	while(y){
		if(y&1)res=1ll*res*x%mod;
		x=1ll*x*x%mod,y>>=1;
	}return res;
}
int solve(int l,int r,int k){
	if((l==r)||(k==tot))return a[l];
	int t=solve(l+1,r,k+1);
	return ksm(a[l],t%p[k+1]+(t>p[k+1])*p[k+1],p[k]);
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	n=read(),m=read();
	REP(i,1,n)a[i]=read();
	while(m)p[++tot]=m,m=phi(m);
	q_cnt=read();
	REP(i,1,q_cnt){
		int l=read(),r=read();
		write(solve(l,r,1)%p[1],'\n');
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define REP(i,st,ed) for(int i=(int)(st),i##end=(int)(ed);i<=i##end;++i)
#define DREP(i,st,ed) for(int i=(int)(st),i##end=(int)(ed);i>=i##end;--i)
template<typename T>bool chkmin(T &x,const T &y){return x>y?x=y,1:0;}
template<typename T>bool chkmax(T &x,const T &y){return x<y?x=y,1:0;}
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
template<typename T>T read(){
	T x=0,f=1;
	char c=getchar();
	while((c<'0')||(c>'9')){if(c=='-')f=-1;c=getchar();}
	while((c>='0')&&(c<='9'))x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
#define read() read<int>()
template<typename T>void write(T x,char c){
	static char t[25];
	static int tlen;
	t[tlen=1]=c;
	if(x<0)putchar('-'),x=-x;
	do t[++tlen]=(x%10)^48;
	while(x/=10);
	while(tlen)putchar(t[tlen--]);
}
#define pb push_back
typedef long long ll;
typedef double lf;
const int maxn=100005;
int phi(int x){
	int res=x-1;
	for(int i=2;i*i<=x;++i)
		if(x%i==0)res-=res/i;
	return res;
}
int n,m,q_cnt,a[maxn];
int p[105],tot;
int ksm(int x,int y,int mod){
	int res=1;
	while(y){
		if(y&1)res=1ll*res*x%mod;
		x=1ll*x*x%mod,y>>=1;
	}return res;
}
int solve(int l,int r,int k){
	if((l==r)||(k==tot))return a[l];
	int t=solve(l+1,r,k+1);
	return ksm(a[l],t%p[k+1]+(t>p[k+1])*p[k+1],p[k]);
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	n=read(),m=read();
	REP(i,1,n)a[i]=read();
	while(m)p[++tot]=m,m=phi(m);
	q_cnt=read();
	REP(i,1,q_cnt){
		int l=read(),r=read();
		write(solve(l,r,1),'\n');
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define REP(i,st,ed) for(int i=(int)(st),i##end=(int)(ed);i<=i##end;++i)
#define DREP(i,st,ed) for(int i=(int)(st),i##end=(int)(ed);i>=i##end;--i)
template<typename T>bool chkmin(T &x,const T &y){return x>y?x=y,1:0;}
template<typename T>bool chkmax(T &x,const T &y){return x<y?x=y,1:0;}
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
template<typename T>T read(){
	T x=0,f=1;
	char c=getchar();
	while((c<'0')||(c>'9')){if(c=='-')f=-1;c=getchar();}
	while((c>='0')&&(c<='9'))x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
#define read() read<int>()
template<typename T>void write(T x,char c){
	static char t[25];
	static int tlen;
	t[tlen=1]=c;
	if(x<0)putchar('-'),x=-x;
	do t[++tlen]=(x%10)^48;
	while(x/=10);
	while(tlen)putchar(t[tlen--]);
}
#define pb push_back
typedef long long ll;
typedef double lf;
const int maxn=100005;
int phi(int x){
	int res=x-1;
	for(int i=2;i*i<=x;++i)
		if(x%i==0)res-=res/i;
	return res;
}
int n,m,q_cnt,a[maxn];
int p[105],tot;
int ksm(int x,int y,int mod){
	int res=1;
	while(y){
		if(y&1)res=1ll*res*x%mod;
		x=1ll*x*x%mod,y>>=1;
	}return res;
}
struct data{
	int val,flag;
};
data solve(int l,int r,int k){
	if((l==r)||(k==tot))return (data){a[l]%p[k],a[l]>=p[k]};
	else if(a[l]==1)return (data){1,0};
	int cnt=0,tmp=1;
	while(1ll*tmp*a[l]<p[k])tmp*=a[l],++cnt;
	data t=solve(l+1,r,k+1);
	return (data){ksm(a[l],t.val+t.flag*p[k+1],p[k]),t.val+t.flag*p[k+1]>cnt};
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	n=read(),m=read();
	REP(i,1,n)a[i]=read();
	while(m)p[++tot]=m,m=phi(m);
	q_cnt=read();
	REP(i,1,q_cnt){
		int l=read(),r=read();
		write(solve(l,r,1).val,'\n');
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define REP(i,st,ed) for(int i=(int)(st),i##end=(int)(ed);i<=i##end;++i)
#define DREP(i,st,ed) for(int i=(int)(st),i##end=(int)(ed);i>=i##end;--i)
template<typename T>bool chkmin(T &x,const T &y){return x>y?x=y,1:0;}
template<typename T>bool chkmax(T &x,const T &y){return x<y?x=y,1:0;}
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
template<typename T>T read(){
	T x=0,f=1;
	char c=getchar();
	while((c<'0')||(c>'9')){if(c=='-')f=-1;c=getchar();}
	while((c>='0')&&(c<='9'))x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
#define read() read<int>()
template<typename T>void write(T x,char c){
	static char t[25];
	static int tlen;
	t[tlen=1]=c;
	if(x<0)putchar('-'),x=-x;
	do t[++tlen]=(x%10)^48;
	while(x/=10);
	while(tlen)putchar(t[tlen--]);
}
#define pb push_back
typedef long long ll;
typedef double lf;
const int maxn=100005;
int phi(int x){
	int res=x;
	for(int i=2;i*i<=x;++i)
		if(x%i==0){
			res-=res/i;
			while(x%i==0)x/=i;
		}
	if(x>1)res-=res/x;
	return res;
}
int n,m,q_cnt,a[maxn];
int p[105],tot;
int ksm(int x,int y,int mod){
	int res=1;
	while(y){
		if(y&1)res=1ll*res*x%mod;
		x=1ll*x*x%mod,y>>=1;
	}return res;
}
struct data{
	int val,flag;
};
data solve(int l,int r,int k){
	if((l==r)||(k==tot))return (data){a[l]%p[k],a[l]>=p[k]};
	else if(a[l]==1)return (data){1,0};
	int cnt=0,tmp=1;
	while(1ll*tmp*a[l]<p[k])tmp*=a[l],++cnt;
	data t=solve(l+1,r,k+1);
	return (data){ksm(a[l],t.val+t.flag*p[k+1],p[k]),t.val+t.flag*p[k+1]>cnt};
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	n=read(),m=read();
	REP(i,1,n)a[i]=read();
	while(m!=1)p[++tot]=m,m=phi(m);
	p[++tot]=1;
	q_cnt=read();
	REP(i,1,q_cnt){
		int l=read(),r=read();
		write(solve(l,r,1).val,'\n');
	}
	return 0;
}


#include <map>
#include <cstdio>
using namespace std;

const int MAXN = 1e5;
int n , q , p , a[ MAXN + 5 ];

map< int , int > phi;
int Phi( int x ) {
    if( phi[ x ] ) return phi[ x ];
    int Ans = x;
    for( int i = 2 ; i * i <= x ; i ++ )
        if( x % i == 0 ) {
            Ans = Ans / i * ( i - 1 );
            while( x % i == 0 ) x /= i;
        }
    if( x > 1 ) Ans = Ans / x * ( x - 1 );
    return phi[ x ] = Ans;
}

int chk( int x , int Mod ) {
    return x < Mod ? x : x % Mod + Mod;
}
int Quick_pow( int x , int po , int Mod ) {
    po = chk( po , Mod );

    int Ans = 1;
    for( ; po ; x = 1ll * x * x % Mod , po >>= 1 )
        if( po & 1 ) Ans = 1ll * Ans * x % Mod;
    return Ans;
}
int Solve( int l , int r , int Mod ) {
    if( l == r || Mod == 1 ) return chk( a[ l ] , Mod );
    return Quick_pow( a[ l ] , Solve( l + 1 , r , Phi( Mod ) ) , Mod );
}

int main( ) {
    scanf("%d %d",&n,&p);
    for( int i = 1 ; i <= n ; i ++ ) scanf("%d",&a[ i ]);
    scanf("%d",&q);
    for( int i = 1 , l , r ; i <= q ; i ++ ) {
        scanf("%d %d",&l,&r);
        printf("%d\n", Solve( l , r , p ) );
    }
    return 0;
}


#include <map>
#include <cstdio>
using namespace std;

const int MAXN = 1e5;
int n , q , p , a[ MAXN + 5 ];

map< int , int > phi;
int Phi( int x ) {
    if( phi[ x ] ) return phi[ x ];
    int Ans = x;
    for( int i = 2 ; i * i <= x ; i ++ )
        if( x % i == 0 ) {
            Ans = Ans / i * ( i - 1 );
            while( x % i == 0 ) x /= i;
        }
    if( x > 1 ) Ans = Ans / x * ( x - 1 );
    return phi[ x ] = Ans;
}

int chk( long long x , int Mod ) {
    return x < Mod ? x : x % Mod + Mod;
}
int Quick_pow( int x , int po , int Mod ) {
    po = chk( po , Mod );

    int Ans = 1;
    for( ; po ; x = chk( 1ll * x * x , Mod ) , po >>= 1 )
        if( po & 1 ) Ans = chk( 1ll * Ans * x , Mod );
    return Ans;
}
int Solve( int l , int r , int Mod ) {
    if( l == r || Mod == 1 ) return chk( a[ l ] , Mod );
    return Quick_pow( a[ l ] , Solve( l + 1 , r , Phi( Mod ) ) , Mod );
}

int main( ) {
    scanf("%d %d",&n,&p);
    for( int i = 1 ; i <= n ; i ++ ) scanf("%d",&a[ i ]);
    scanf("%d",&q);
    for( int i = 1 , l , r ; i <= q ; i ++ ) {
        scanf("%d %d",&l,&r);
        printf("%d\n", Solve( l , r , p ) % p );
    }
    return 0;
}


#include <map>
#include <cstdio>
using namespace std;

const int MAXN = 1e5;
int n , q , p , a[ MAXN + 5 ];

map< int , int > phi;
int Phi( int x ) {
    if( phi[ x ] ) return phi[ x ];
    int Ans = x;
    for( int i = 2 ; i * i <= x ; i ++ )
        if( x % i == 0 ) {
            Ans = Ans / i * ( i - 1 );
            while( x % i == 0 ) x /= i;
        }
    if( x > 1 ) Ans = Ans / x * ( x - 1 );
    return phi[ x ] = Ans;
}

int Quick_pow( int x , int po , int Mod ) {
    // if( po > Mod ) po = po % Mod + Mod;

    int Ans = 1;
    for( ; po ; x = 1ll * x * x % Mod , po >>= 1 )
        if( po & 1 ) Ans = 1ll * Ans * x % Mod;
    return Ans;
}
int Solve( int l , int r , int Mod ) {
    if( Mod == 1 ) return 0;
    if( l == r ) return a[ l ] % Mod;
    return Quick_pow( a[ l ] , Solve( l + 1 , r , Phi( Mod ) ) , Mod );
}

int main( ) {
    scanf("%d %d",&n,&p);
    for( int i = 1 ; i <= n ; i ++ ) scanf("%d",&a[ i ]);
    scanf("%d",&q);
    for( int i = 1 , l , r ; i <= q ; i ++ ) {
        scanf("%d %d",&l,&r);
        printf("%d\n", Solve( l , r , p ) );
    }
    return 0;
}


#include <map>
#include <cstdio>
using namespace std;

const int MAXN = 1e5;
int n , q , p , a[ MAXN + 5 ];

map< int , int > phi;
int Phi( int x ) {
    if( phi[ x ] ) return phi[ x ];
    int Ans = x;
    for( int i = 2 ; i * i <= x ; i ++ )
        if( x % i == 0 ) {
            Ans = Ans / i * ( i - 1 );
            while( x % i == 0 ) x /= i;
        }
    if( x > 1 ) Ans = Ans / x * ( x - 1 );
    return phi[ x ] = Ans;
}

int Quick_pow( int x , int po , int Mod ) {
    if( po > Mod ) po = po % Mod + Mod;

    int Ans = 1;
    for( ; po ; x = 1ll * x * x % Mod , po >>= 1 )
        if( po & 1 ) Ans = 1ll * Ans * x % Mod;
    return Ans;
}
int Solve( int l , int r , int Mod ) {
    if( Mod == 1 ) return 1;
    if( l == r ) return a[ l ] % Mod;
    return Quick_pow( a[ l ] , Solve( l + 1 , r , Phi( Mod ) ) , Mod );
}

int main( ) {
    scanf("%d %d",&n,&p);
    for( int i = 1 ; i <= n ; i ++ ) scanf("%d",&a[ i ]);
    scanf("%d",&q);
    for( int i = 1 , l , r ; i <= q ; i ++ ) {
        scanf("%d %d",&l,&r);
        printf("%d\n", Solve( l , r , p ) );
    }
    return 0;
}


#include <map>
#include <cstdio>
using namespace std;

const int MAXN = 1e5;
int n , q , p , a[ MAXN + 5 ];

map< int , int > phi;
int Phi( int x ) {
    if( phi[ x ] ) return phi[ x ];
    int Ans = x;
    for( int i = 2 ; i * i <= x ; i ++ )
        if( x % i == 0 ) {
            Ans = Ans / i * ( i - 1 );
            while( x % i == 0 ) x /= i;
        }
    if( x > 1 ) Ans = Ans / x * ( x - 1 );
    return phi[ x ] = Ans;
}

int chk( long long x , int Mod ) {
    return x < Mod ? x : x % Mod + Mod;
}
int Quick_pow( int x , int po , int Mod ) {
    po = chk( po , Mod );

    int Ans = 1;
    for( ; po ; x = chk( 1ll * x * x , Mod ) , po >>= 1 )
        if( po & 1 ) Ans = chk( 1ll * Ans * x , Mod );
    return Ans;
}
int Solve( int l , int r , int Mod ) {
    if( l == r || Mod == 1 ) return chk( a[ l ] , Mod );
    return Quick_pow( a[ l ] , Solve( l + 1 , r , Phi( Mod ) ) , Mod );
}

int main( ) {
    scanf("%d %d",&n,&p);
    for( int i = 1 ; i <= n ; i ++ ) scanf("%d",&a[ i ]);
    scanf("%d",&q);
    for( int i = 1 , l , r ; i <= q ; i ++ ) {
        scanf("%d %d",&l,&r);
        printf("%d\n", Solve( l , r , p ) );
    }
    return 0;
}


#include <map>
#include <cstdio>
using namespace std;

const int MAXN = 1e5;
int n , q , p , a[ MAXN + 5 ];

map< int , int > phi;
int Phi( int x ) {
    if( phi[ x ] ) return phi[ x ];
    int Ans = x;
    for( int i = 2 ; i * i <= x ; i ++ )
        if( x % i == 0 ) {
            Ans = Ans / i * ( i - 1 );
            while( x % i == 0 ) x /= i;
        }
    if( x > 1 ) Ans = Ans / x * ( x - 1 );
    return phi[ x ] = Ans;
}

int Quick_pow( int x , int po , int Mod ) {
    if( po > Mod ) po = po % Mod + Mod;

    int Ans = 1;
    for( ; po ; x = 1ll * x * x % Mod , po >>= 1 )
        if( po & 1 ) Ans = 1ll * Ans * x % Mod;
    return Ans;
}
int Solve( int l , int r , int Mod ) {
    if( Mod == 1 ) return 0;
    if( l == r ) return a[ l ] % Mod;
    return Quick_pow( a[ l ] , Solve( l + 1 , r , Phi( Mod ) ) , Mod );
}

int main( ) {
    scanf("%d %d",&n,&p);
    for( int i = 1 ; i <= n ; i ++ ) scanf("%d",&a[ i ]);
    scanf("%d",&q);
    for( int i = 1 , l , r ; i <= q ; i ++ ) {
        scanf("%d %d",&l,&r);
        printf("%d\n", Solve( l , r , p ) );
    }
    return 0;
}


#include <map>
#include <cstdio>
using namespace std;

const int MAXN = 1e5;
int n , q , p , a[ MAXN + 5 ];

map< int , int > phi;
int Phi( int x ) {
    if( phi[ x ] ) return phi[ x ];
    int Ans = x;
    for( int i = 2 ; i * i <= x ; i ++ )
        if( x % i == 0 ) {
            Ans = Ans / i * ( i - 1 );
            while( x % i == 0 ) x /= i;
        }
    if( x > 1 ) Ans = Ans / x * ( x - 1 );
    return phi[ x ] = Ans;
}

int chk( int x , int Mod ) {
    return x < Mod ? x : x % Mod + Mod;
}
int Quick_pow( int x , int po , int Mod ) {
    po = chk( po , Mod );

    int Ans = 1;
    for( ; po ; x = chk( 1ll * x * x % Mod , Mod ) , po >>= 1 )
        if( po & 1 ) Ans = chk( 1ll * Ans * x % Mod , Mod );
    return Ans;
}
int Solve( int l , int r , int Mod ) {
    if( l == r || Mod == 1 ) return chk( a[ l ] , Mod );
    return Quick_pow( a[ l ] , Solve( l + 1 , r , Phi( Mod ) ) , Mod );
}

int main( ) {
    scanf("%d %d",&n,&p);
    for( int i = 1 ; i <= n ; i ++ ) scanf("%d",&a[ i ]);
    scanf("%d",&q);
    for( int i = 1 , l , r ; i <= q ; i ++ ) {
        scanf("%d %d",&l,&r);
        printf("%d\n", Solve( l , r , p ) );
    }
    return 0;
}


#include <map>
#include <cstdio>
using namespace std;

const int MAXN = 1e5;
int n , q , p , a[ MAXN + 5 ];

map< int , int > phi;
int Phi( int x ) {
    if( phi[ x ] ) return phi[ x ];
    int Ans = x;
    for( int i = 2 ; i * i <= x ; i ++ )
        if( x % i == 0 ) {
            Ans = Ans / i * ( i - 1 );
            while( x % i == 0 ) x /= i;
        }
    if( x > 1 ) Ans = Ans / x * ( x - 1 );
    return phi[ x ] = Ans;
}

int chk( long long x , int Mod ) {
    return x < Mod ? x : x % Mod + Mod;
}
int Quick_pow( int x , int po , int Mod ) {
    po = chk( po , Mod );

    int Ans = 1;
    for( ; po ; x = chk( 1ll * x * x , Mod ) , po >>= 1 )
        if( po & 1 ) Ans = chk( 1ll * Ans * x , Mod );
    return Ans;
}
int Solve( int l , int r , int Mod ) {
    if( l == r || Mod == 1 ) return chk( a[ l ] , Mod );
    return Quick_pow( a[ l ] , Solve( l + 1 , r , Phi( Mod ) ) , Mod );
}

int main( ) {
    scanf("%d %d",&n,&p);
    for( int i = 1 ; i <= n ; i ++ ) scanf("%d",&a[ i ]);
    scanf("%d",&q);
    for( int i = 1 , l , r ; i <= q ; i ++ ) {
        scanf("%d %d",&l,&r);
        printf("%d\n", Solve( l , r , p ) % Mod );
    }
    return 0;
}


#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;

const int N=100010;
int n,Q,MOD,a[N];
map<int,int> phi;

int Phi(int x)
{
	if (phi[x]) return phi[x];
	int p=x;
	for (int i=2;i*i<=x;i++)
		if (x%i==0)
		{
			p=p/i*(i-1);
			while (x%i==0) x/=i;
		}
	if (x>1) p=p/x*(x-1);
	return p;
}

ll fpow(ll x,ll k,int mod)
{
	ll ans=1;
	for (;k;k>>=1,x=x*x%mod)
		if (k&1) ans=ans*x%mod;
	return ans;
}

ll solve(int x,int y,int p)
{
	if (p==1 || x>y) return 1;
	return fpow(a[x],solve(x+1,y,Phi(p)),p);
}

int main()
{
	scanf("%d%d",&n,&MOD);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	scanf("%d",&Q);
	while (Q--)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		printf("%lld\n",solve(l,r,MOD));
	}
	return 0;
}


#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;

const int N=100010;
int n,Q,MOD,a[N];
map<int,int> phi;

int Gcd(int x,int y)
{
	if (!y) return x;
	return Gcd(y,x%y);
}

int Phi(int x)
{
	if (phi[x]) return phi[x];
	int p=x,y=x;
	for (int i=2;i*i<=x;i++)
		if (x%i==0)
		{
			p=p/i*(i-1);
			while (x%i==0) x/=i;
		}
	if (x>1) p=p/x*(x-1);
	return phi[y]=p;
}

ll fpow(ll x,ll k,int mod)
{
	ll ans=1;
	for (;k;k>>=1,x=x*x)
	{
		if (x>mod) x=x%mod+mod;
		if (k&1) ans=ans*x%mod;
	}
	return ans;
}

ll solve(int x,int y,int p)
{
	if (p==1 || x>y) return 1;
	return fpow(a[x],solve(x+1,y,Phi(p)),p);
}

int main()
{
	scanf("%d%d",&n,&MOD);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	scanf("%d",&Q);
	while (Q--)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		printf("%I64d\n",solve(l,r,MOD));
	}
	return 0;
}


#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;

const int N=100010;
int n,Q,MOD,a[N];
map<int,int> phi;

int Phi(int x)
{
	if (phi[x]) return phi[x];
	int p=x,y=x;
	for (int i=2;i*i<=x;i++)
		if (x%i==0)
		{
			p=p/i*(i-1);
			while (x%i==0) x/=i;
		}
	if (x>1) p=p/x*(x-1);
	return phi[y]=p;
}

ll fpow(ll x,ll k,int mod)
{
	ll ans=1;
	for (;k;k>>=1,x=x*x%mod)
		if (k&1) ans=ans*x%mod;
	return ans;
}

ll solve(int x,int y,int p)
{
	if (p==1 || x>y) return 1;
	return fpow(a[x],solve(x+1,y,Phi(p)),p);
}

int main()
{
	scanf("%d%d",&n,&MOD);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	scanf("%d",&Q);
	while (Q--)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		printf("%I64d\n",solve(l,r,MOD));
	}
	return 0;
}


#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;

const int N=100010;
int n,Q,MOD,a[N];
map<int,int> phi;

int Phi(int x)
{
	if (phi[x]) return phi[x];
	int p=x,y=x;
	for (int i=2;i*i<=x;i++)
		if (x%i==0)
		{
			p=p/i*(i-1);
			while (x%i==0) x/=i;
		}
	if (x>1) p=p/x*(x-1);
	return phi[y]=p;
}

ll fpow(ll x,ll k,int mod)
{
	ll ans=1;
	for (;k;k>>=1,x=x*x)
	{
		if (x>=mod) x=x%mod+mod;
		if (k&1) ans=ans*x%mod;
	}
	return ans;
}

int solve(int x,int y,int p)
{
	if (p==1 || x>y) return 1;
	return (int)fpow(a[x],solve(x+1,y,Phi(p)),p);
}

int main()
{
	scanf("%d%d",&n,&MOD);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	scanf("%d",&Q);
	while (Q--)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		printf("%d\n",solve(l,r,MOD)%MOD);
	}
	return 0;
}


#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;

const int N=100010;
int n,Q,MOD,a[N];
map<int,int> phi;

int Phi(int x)
{
	if (phi[x]) return phi[x];
	int p=x,y=x;
	for (int i=2;i*i<=x;i++)
		if (x%i==0)
		{
			p=p/i*(i-1);
			while (x%i==0) x/=i;
		}
	if (x>1) p=p/x*(x-1);
	return phi[y]=p;
}

ll fpow(ll x,ll k,int mod)
{
	ll ans=1;
	for (;k;k>>=1,x*=x)
	{
		if (x>=mod) x=x%mod+mod;
		if (k&1) ans=ans*x;
		if (ans>=mod) ans=ans%mod+mod;
	}
	return ans;
}

int solve(int x,int y,int p)
{
	if (p==1 || x>y) return 1;
	return (int)fpow(a[x],solve(x+1,y,Phi(p)),p);
}

int main()
{
	scanf("%d%d",&n,&MOD);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	scanf("%d",&Q);
	while (Q--)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		printf("%d\n",solve(l,r,MOD)%MOD);
	}
	return 0;
}


#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;

const int N=100010;
int n,Q,MOD,a[N];
map<int,int> phi;

int Gcd(int x,int y)
{
	if (!y) return x;
	return Gcd(y,x%y);
}

int Phi(int x)
{
	if (phi[x]) return phi[x];
	int p=x,y=x;
	for (int i=2;i*i<=x;i++)
		if (x%i==0)
		{
			p=p/i*(i-1);
			while (x%i==0) x/=i;
		}
	if (x>1) p=p/x*(x-1);
	return phi[y]=p;
}

ll fpow(ll x,ll k,int mod)
{
	ll ans=1;
	for (;k;k>>=1,x=x*x%mod)
		if (k&1) ans=ans*x%mod;
	return ans;
}

ll solve(int x,int y,int p)
{
	if (p==1 || x>y) return 1;
	int t=(Gcd(a[x],p)==1 || a[x]<p)?0:Phi(p);
	return fpow(a[x],solve(x+1,y,Phi(p))+t,p);
}

int main()
{
	scanf("%d%d",&n,&MOD);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	scanf("%d",&Q);
	while (Q--)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		printf("%I64d\n",solve(l,r,MOD));
	}
	return 0;
}


#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;

const int N=100010;
int n,Q,MOD,a[N];
map<int,int> phi;

int Phi(int x)
{
	if (phi[x]) return phi[x];
	int p=x,y=x;
	for (int i=2;i*i<=x;i++)
		if (x%i==0)
		{
			p=p/i*(i-1);
			while (x%i==0) x/=i;
		}
	if (x>1) p=p/x*(x-1);
	return phi[y]=p;
}

ll fpow(ll x,ll k,int mod)
{
	ll ans=1;
	for (;k;k>>=1)
	{
		if (k&1) ans=ans*x%mod;
		x=x*x;
		if (x>=mod) x=x%mod+mod;
	}
	return ans;
}

int solve(int x,int y,int p)
{
	if (p==1 || x>y) return 1;
	return (int)fpow(a[x],solve(x+1,y,Phi(p)),p);
}

int main()
{
	scanf("%d%d",&n,&MOD);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	scanf("%d",&Q);
	while (Q--)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		printf("%d\n",solve(l,r,MOD)%MOD);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
// typedef __int128 LLL;
typedef pair<int,int>PII;
const int maxn = 1e5+2;
// const LL mod = 998244353;
const long double PI = acos(-1);
const int N=100000,B=33;

int mod[100];

int phi(int x){
	int ret=1;
	for(int i=2;i<=x/i;i++){
		if(x%i==0){
			int tmp=1;
			while(x%i==0){
				x/=i;
				tmp*=i;
			}
			ret*=(tmp/i)*(i-1);
		}
	}
	if(x>1) ret*=(x-1);
	return ret;
}

void init(int m){
	while(m!=1){
		mod[++mod[0]]=m;
		// printf("mod %d\n", m);
		m = phi(m);
	}
	mod[++mod[0]]=1;
}

int a[maxn];
bool flag;
int q_pow(LL base,int t,int mm){
	LL ret=1;
	while(t){
		if(t&1){
			if(ret*base>=mm) flag=1;
			ret=ret*base%mm;
		}
		base = base*base%mm;
		t>>=1;
	}
	return ret;
}

int main(void){
	mod[0]=0;
	int n,m;
	scanf("%d%d",&n,&m);
	init(m);
	for(int i=1;i<=n;i++)
		scanf("%d",a+i);
	int q;
	scanf("%d",&q);
	while(q--){
		int l,r;
		scanf("%d%d",&l,&r);
		r=min(r,l+mod[0]-1);
		int ans=1;
		for(int i=l;i<=r;i++){
			if(a[i]==1){
				r=i;break;
			}
		}
		flag=0;
		for(int i=r;i>=l;i--){
			int t = i-l+1;
			if(flag) ans+=mod[t+1];
			ans=q_pow(a[i],ans,mod[t]);
		}
		printf("%d\n", ans);
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
// typedef __int128 LLL;
typedef pair<int,int>PII;
const int maxn = 1e5+2;
// const LL mod = 998244353;
const long double PI = acos(-1);
const int N=100000,B=33;

int mod[100];

int phi(int x){
	int ret=1;
	for(int i=2;i<=x/i;i++){
		if(x%i==0){
			int tmp=1;
			while(x%i==0){
				x/=i;
				tmp*=i;
			}
			ret*=(tmp/i)*(i-1);
		}
	}
	if(x>1) ret*=(x-1);
	return ret;
}

void init(int m){
	while(m!=1){
		mod[++mod[0]]=m;
		// printf("mod %d\n", m);
		m = phi(m);
	}
	mod[++mod[0]]=1;
}

int a[maxn];
bool flag;
int q_pow(LL base,int t,int mm,int xx=0){
	LL ret=1;
	while(t){
		if(t&1){
			if(ret*base>=xx && mm==1000000007) flag=1;
			ret=ret*base%mm;
		}
		if(base*base>=xx && mm==1000000007) flag=1;
		base = base*base%mm;
		t>>=1;
	}
	return ret;
}

int main(void){
	mod[0]=0;
	int n,m;
	scanf("%d%d",&n,&m);
	init(m);
	for(int i=1;i<=n;i++)
		scanf("%d",a+i);
	int q;
	scanf("%d",&q);
	while(q--){
		int l,r;
		scanf("%d%d",&l,&r);
		if(a[l]==1) printf("%d\n", a[l]%mod[1]);
		else{
			int cpr = r;
			r=min(r,l+mod[0]-1);
			int ans=1;
			for(int i=l;i<=r;i++){
				if(a[i]==1){
					cpr=r=i-1;break;
				}
			}
			flag=0;
			int tmp=1;
			if(cpr!=r) flag=1;
			for(int i=r;i>=l;i--){
				int t = i-l+1;
				if(!flag && i<r){
					tmp = q_pow(a[i+1],tmp,1000000007,mod[t+1]);
				}
				if(flag) ans+=mod[t+1];
				ans=q_pow(a[i],ans,mod[t]);
			}
			printf("%d\n", ans);

		}
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
// typedef __int128 LLL;
typedef pair<int,int>PII;
const int maxn = 1e5+2;
// const LL mod = 998244353;
const long double PI = acos(-1);
const int N=100000,B=33;

int mod[100];

int phi(int x){
	int ret=1;
	for(int i=2;i<=x/i;i++){
		if(x%i==0){
			int tmp=1;
			while(x%i==0){
				x/=i;
				tmp*=i;
			}
			ret*=(tmp/i)*(i-1);
		}
	}
	if(x>1) ret*=(x-1);
	return ret;
}

void init(int m){
	while(m!=1){
		mod[++mod[0]]=m;
		// printf("mod %d\n", m);
		m = phi(m);
	}
	mod[++mod[0]]=1;
}

int a[maxn];

int q_pow(LL base,int t,int mm){
	LL ret=1;
	while(t){
		if(t&1) ret=ret*base%mm;
		base = base*base%mm;
		t>>=1;
	}
	return ret;
}

int main(void){
	mod[0]=0;
	int n,m;
	scanf("%d%d",&n,&m);
	init(m);
	for(int i=1;i<=n;i++)
		scanf("%d",a+i);
	int q;
	scanf("%d",&q);
	while(q--){
		int l,r;
		scanf("%d%d",&l,&r);
		r=min(r,l+mod[0]-1);
		int ans=1;
		for(int i=r;i>=l;i--){
			int t = i-l+1;
			ans=q_pow(a[i],ans+mod[t+1],mod[t]);
		}
		printf("%d\n", ans);
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
// typedef __int128 LLL;
typedef pair<int,int>PII;
const int maxn = 1e5+2;
// const LL mod = 998244353;
const long double PI = acos(-1);
const int N=100000,B=33;

int mod[100];

int phi(int x){
	int ret=1;
	for(int i=2;i<=x/i;i++){
		if(x%i==0){
			int tmp=1;
			while(x%i==0){
				x/=i;
				tmp*=i;
			}
			ret*=(tmp/i)*(i-1);
		}
	}
	if(x>1) ret*=(x-1);
	return ret;
}

void init(int m){
	while(m!=1){
		mod[++mod[0]]=m;
		// printf("mod %d\n", m);
		m = phi(m);
	}
	mod[++mod[0]]=1;
}

int a[maxn];

int q_pow(LL base,int t,int mm){
	LL ret=1;
	while(t){
		if(t&1) ret=ret*base%mm;
		base = base*base%mm;
		t>>=1;
	}
	return ret;
}

int main(void){
	mod[0]=0;
	int n,m;
	scanf("%d%d",&n,&m);
	init(m);
	for(int i=1;i<=n;i++)
		scanf("%d",a+i);
	int q;
	scanf("%d",&q);
	while(q--){
		int l,r;
		scanf("%d%d",&l,&r);
		r=min(r,l+mod[0]-1);
		int ans=1;
		for(int i=r;i>=l;i--){
			int t = i-l+1;
			ans=q_pow(a[i],ans,mod[t]);
		}
		printf("%d\n", ans);
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
// typedef __int128 LLL;
typedef pair<int,int>PII;
const int maxn = 1e5+2;
// const LL mod = 998244353;
const long double PI = acos(-1);
const int N=100000,B=33;

int mod[100];

int phi(int x){
	int ret=1;
	for(int i=2;i<=x/i;i++){
		if(x%i==0){
			int tmp=1;
			while(x%i==0){
				x/=i;
				tmp*=i;
			}
			ret*=(tmp/i)*(i-1);
		}
	}
	if(x>1) ret*=(x-1);
	return ret;
}

void init(int m){
	while(m!=1){
		mod[++mod[0]]=m;
		// printf("mod %d\n", m);
		m = phi(m);
	}
	mod[++mod[0]]=1;
}

int a[maxn];
bool flag;
int q_pow(LL base,int t,int mm){
	LL ret=1;
	while(t){
		if(t&1){
			if(ret*base>=mm) flag=1;
			ret=ret*base%mm;
		}
		base = base*base%mm;
		t>>=1;
	}
	return ret;
}

int main(void){
	mod[0]=0;
	int n,m;
	scanf("%d%d",&n,&m);
	init(m);
	for(int i=1;i<=n;i++)
		scanf("%d",a+i);
	int q;
	scanf("%d",&q);
	while(q--){
		int l,r;
		scanf("%d%d",&l,&r);
		r=min(r,l+mod[0]-1);
		int ans=1;
		flag=0;
		for(int i=r;i>=l;i--){
			int t = i-l+1;
			if(flag) ans+=mod[t+1];
			ans=q_pow(a[i],ans,mod[t]);
		}
		printf("%d\n", ans);
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
// typedef __int128 LLL;
typedef pair<int,int>PII;
const int maxn = 1e5+2;
// const LL mod = 998244353;
const long double PI = acos(-1);
const int N=100000,B=33;

int mod[100];

int phi(int x){
	int ret=1;
	for(int i=2;i<=x/i;i++){
		if(x%i==0){
			int tmp=1;
			while(x%i==0){
				x/=i;
				tmp*=i;
			}
			ret*=(tmp/i)*(i-1);
		}
	}
	if(x>1) ret*=(x-1);
	return ret;
}

void init(int m){
	while(m!=1){
		mod[++mod[0]]=m;
		// printf("mod %d\n", m);
		m = phi(m);
	}
	mod[++mod[0]]=1;
}

int a[maxn];
bool flag;
int q_pow(LL base,int t,int mm){
	LL ret=1;
	while(t){
		if(t&1){
			if(ret*base>=mm && mm==1000000007) flag=1;
			ret=ret*base%mm;
		}
		if(base*base>=mm && mm==1000000007) flag=1;
		base = base*base%mm;
		t>>=1;
	}
	return ret;
}

int main(void){
	mod[0]=0;
	int n,m;
	scanf("%d%d",&n,&m);
	init(m);
	for(int i=1;i<=n;i++)
		scanf("%d",a+i);
	int q;
	scanf("%d",&q);
	while(q--){
		int l,r;
		scanf("%d%d",&l,&r);
		r=min(r,l+mod[0]-1);
		int ans=1;
		for(int i=l;i<=r;i++){
			if(a[i]==1){
				r=i;break;
			}
		}
		flag=0;
		int tmp=1;
		for(int i=r;i>=l;i--){
			int t = i-l+1;
			if(!flag && i<r){
				tmp = q_pow(a[i+1],tmp,1000000007);
			}
			if(flag) ans+=mod[t+1];
			ans=q_pow(a[i],ans,mod[t]);
		}
		printf("%d\n", ans);
	}
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>

inline int read() {
	int x = 0, f = 1; char ch = getchar();
	while(ch > '9' || ch < '0') { if(ch == '-') f = -1; ch = getchar(); }
	do x = x * 10 + ch - 48, ch = getchar(); while(ch >= '0' && ch <= '9');
	return x * f;
}

typedef long long ll;
const int MAXN = 100001;

int n,mod;
ll w[MAXN];
std::map <int,int> map;

int Varphi(int n) {
	if(map.find(n) != map.end()) return map[n];
	int res = n;
	for(int i = 2;i * i <= n;i++) {
		if(!(n % i)) res = res - res / i;
		while(!(n % i)) n /= i;
	}
	if(n > 1) res = res - res / n;
	return map[n] = res;
}

std::pair <ll,bool> Quick_Pow(ll a,int b,int mod) {
	ll res = 1; bool flag = false;
	a %= mod;
	while(b) {
		if(b & 1) {
			if(res * a >= mod) flag = true;
			res = res * a % mod;
		}
		a = a * a % mod;
		b >>= 1;
	}
	return std::make_pair(res,flag);
}

std::pair <ll,bool> Query(int i,int r,int p) {
	if(p == 1 || i > r) return std::make_pair(1,0);
	int phi = Varphi(p);
	std::pair <ll,bool> pr = Query(i + 1,r,p);
	int pow = pr.first + (pr.second ? phi : 0);
	return Quick_Pow(w[i],pow,mod);
}

int main() {
	n = read(), mod = read();
	for(int i = 1;i <= n;i++) w[i] = read();
	int q = read(),l,r;
	while(q--) {
		l = read(), r = read();
		std::printf("%lld\n",Query(l,r,mod).first);
	}
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>

inline int read() {
	int x = 0, f = 1; char ch = getchar();
	while(ch > '9' || ch < '0') { if(ch == '-') f = -1; ch = getchar(); }
	do x = x * 10 + ch - 48, ch = getchar(); while(ch >= '0' && ch <= '9');
	return x * f;
}

typedef long long ll;
const int MAXN = 100001;

int n,mod;
ll w[MAXN];
std::map <int,int> map;

int Varphi(int n) {
	if(map.find(n) != map.end()) return map[n];
	int res = n;
	for(int i = 2;i * i <= n;i++) {
		if(!(n % i)) res = res - res / i;
		while(!(n % i)) n /= i;
	}
	if(n > 1) res = res - res / n;
	return map[n] = res;
}

std::pair <ll,bool> Quick_Pow(ll a,int b,int mod) {
	ll res = 1; bool flag = false;
	a %= mod;
	while(b) {
		if(b & 1) {
			if(res * a >= mod) flag = true;
			res = res * a % mod;
		}
		a = a * a % mod;
		b >>= 1;
	}
	return std::make_pair(res,flag);
}

std::pair <ll,bool> Query(int i,int r,int p) {
	if(p == 1 || i == r) return std::make_pair(w[i] % p,w[i] >= p);
	int phi = Varphi(p);
	std::pair <ll,bool> pr = Query(i + 1,r,phi);
	int pow = pr.first + (pr.second ? phi : 0);
	return Quick_Pow(w[i],pow,p);
}

int main() {
	n = read(), mod = read();
	for(int i = 1;i <= n;i++) w[i] = read();
	int q = read(),l,r;
	while(q--) {
		l = read(), r = read();
		std::printf("%lld\n",Query(l,r,mod).first);
	}
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>

inline int read() {
    int x = 0, f = 1; char ch = getchar();
    while(ch > '9' || ch < '0') { if(ch == '-') f = -1; ch = getchar(); }
    do x = x * 10 + ch - 48, ch = getchar(); while(ch >= '0' && ch <= '9');
    return x * f;
}

typedef long long ll;
const int MAXN = 100001;

int n,mod;
ll w[MAXN];
std::map <int,int> map;

int Varphi(int n) {
    if(map.find(n) != map.end()) return map[n];
    int res = n;
    for(int i = 2;i * i <= n;i++) {
        if(!(n % i)) res = res - res / i;
        while(!(n % i)) n /= i;
    }
    if(n > 1) res = res - res / n;
    return map[n] = res;
}

ll Quick_Pow(ll a,int b,int mod) {
    ll res = 1;
    while(b) {
        if(b & 1) res = res * a;
        if(res >= mod) res = res % mod + mod;
        a = a * a % mod;
        if(a >= mod) a = a % mod + mod;
        b >>= 1;
    }
    return res;
}

ll Query(int i,int r,int p) {
    if(p == 1 || i == r) return w[i] < p ? w[i] : w[i] % p + p;
    int phi = Varphi(p);
    ll pow = Query(i + 1,r,phi);
    return Quick_Pow(w[i],pow,p);
}

int main() {
    n = read(), mod = read();
    for(int i = 1;i <= n;i++) w[i] = read();
    int q = read(),l,r;
    while(q--) {
        l = read(), r = read();
        std::printf("%lld\n",Query(l,r,mod) % mod);
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>

inline int read() {
    int x = 0, f = 1; char ch = getchar();
    while(ch > '9' || ch < '0') { if(ch == '-') f = -1; ch = getchar(); }
    do x = x * 10 + ch - 48, ch = getchar(); while(ch >= '0' && ch <= '9');
    return x * f;
}

typedef long long ll;
const int MAXN = 100001;

int n,mod;
ll w[MAXN];
std::map <ll,ll> map;

ll Varphi(ll n) {
    if(map.find(n) != map.end()) return map[n];
    ll res = n;
    for(int i = 2;i * i <= n;i++) {
        if(!(n % i)) res = res - res / i;
        while(!(n % i)) n /= i;
    }
    if(n > 1) res = res - res / n;
    return map[n] = res;
}

ll Mod(ll x,int mod) {
	return x < mod ? x : x % mod + mod;
}

ll Quick_Pow(ll a,ll b,int mod) {
    ll res = 1;
    while(b) {
        if(b & 1) res = Mod(res * a,mod);
        a = Mod(a * a,mod);
        b >>= 1;
    }
    return res;
}

ll Query(int i,int r,int p) {
    if(p == 1 || i == r) return Mod(w[i],p);
    return Quick_Pow(w[i],Query(i + 1,r,Varphi(p)),p);
}

int main() {
    n = read(), mod = read();
    for(int i = 1;i <= n;i++) w[i] = read();
    int q = read(),l,r;
    while(q--) {
        l = read(), r = read();
        std::printf("%lld\n",Query(l,r,mod) % mod);
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>

inline int read() {
    int x = 0, f = 1; char ch = getchar();
    while(ch > '9' || ch < '0') { if(ch == '-') f = -1; ch = getchar(); }
    do x = x * 10 + ch - 48, ch = getchar(); while(ch >= '0' && ch <= '9');
    return x * f;
}

typedef long long ll;
const int MAXN = 100001;

int n,mod;
ll w[MAXN];
std::map <int,int> map;

int Varphi(int n) {
    if(map.find(n) != map.end()) return map[n];
    int res = n;
    for(int i = 2;i * i <= n;i++) {
        if(!(n % i)) res = res - res / i;
        while(!(n % i)) n /= i;
    }
    if(n > 1) res = res - res / n;
    return map[n] = res;
}

ll Quick_Pow(ll a,int b,int mod) {
    ll res = 1;
    a %= mod;
    while(b) {
        if(b & 1) res = res * a;
        if(res >= mod) res = res % mod + mod;
        a = a * a % mod;
        if(a >= mod) a = a % mod + mod;
        b >>= 1;
    }
    return res;
}

ll Query(int i,int r,int p) {
    if(p == 1 || i == r) return w[i] < p ? w[i] : w[i] % p + p;
    int phi = Varphi(p);
    ll pow = Query(i + 1,r,phi);
    return Quick_Pow(w[i],pow,p);
}

int main() {
    n = read(), mod = read();
    for(int i = 1;i <= n;i++) w[i] = read();
    int q = read(),l,r;
    while(q--) {
        l = read(), r = read();
        std::printf("%lld\n",Query(l,r,mod) % mod);
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>

inline int read() {
	int x = 0, f = 1; char ch = getchar();
	while(ch > '9' || ch < '0') { if(ch == '-') f = -1; ch = getchar(); }
	do x = x * 10 + ch - 48, ch = getchar(); while(ch >= '0' && ch <= '9');
	return x * f;
}

typedef long long ll;
const int MAXN = 100001;

int n,mod;
ll w[MAXN];
std::map <int,int> map;

int Varphi(int n) {
	if(map.find(n) != map.end()) return map[n];
	int res = n;
	for(int i = 2;i * i <= n;i++) {
		if(!(n % i)) res = res - res / i;
		while(!(n % i)) n /= i;
	}
	if(n > 1) res = res - res / n;
	return map[n] = res;
}

std::pair <ll,bool> Quick_Pow(ll a,int b,int mod) {
	ll res = 1; bool flag = false;
	a %= mod;
	while(b) {
		if(b & 1) {
			if(res * a >= mod) flag = true;
			res = res * a % mod;
		}
		a = a * a % mod;
		b >>= 1;
	}
	return std::make_pair(res,flag);
}

std::pair <ll,bool> Query(int i,int r,int p) {
	if(p == 1 || i > r) return std::make_pair(1,0);
	int phi = Varphi(p);
	std::pair <ll,bool> pr = Query(i + 1,r,p);
	int pow = pr.first + (pr.second ? phi : 0);
	return Quick_Pow(w[i],pow,p);
}

int main() {
	n = read(), mod = read();
	for(int i = 1;i <= n;i++) w[i] = read();
	int q = read(),l,r;
	while(q--) {
		l = read(), r = read();
		std::printf("%lld\n",Query(l,r,mod).first);
	}
    return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define inf 1000000007
#define mod 1000000007
#define x first
#define y second
#define pi acos(-1.0)
#define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
//#define dprintf(...)
#define hash _hash
//#define dprintf(...) fprintf(outFile,__VA_ARGS__)

#define FOREACH(it,x) for(__typeof(x.begin()) it=x.begin();it!=x.end();it++)
#define ull unsigned long long
#define ll long long
#define N 100010

template <class T,class U>inline void Max(T &a,U b){if(a<b)a=b;}
template <class T,class U>inline void Min(T &a,U b){if(a>b)a=b;}

//FILE* outFile;

inline void add(int &a,int b){a+=b;while(a>=mod)a-=mod;}

/*
int pow(int a,int b){
    int ans=1;
    while(b){
        if(b&1)ans=ans*(ll)a%mod;
        a=(ll)a*a%mod;b>>=1;
    }
    return ans;
}//*/
ll ex_gcd(ll a,ll b,ll &x,ll &y){
    if(b==0){
        x=1,y=0;return a;
    }
    ll x1,y1,d=ex_gcd(b,a%b,x1,y1);
    x=y1;
    y=x1-a/b*x;
    return d;
}
map<int,vector<pii>>h;
int f[N];
vector<pii> cal(int m){
    if(h.count(m))return h[m];
    int n=m;
    if(m==2)return h[n]=vector<pii>();
    vector<pii>v;
    rep(i,2,N){
        if(1LL*i*i>m)break;
        if(m%i==0){
            int k=0;
            while(m%i==0)m/=i,k++;
            v.pb({i,k});
        }
    }
    if(m>1)v.pb({m,1});
    h[n]=v;
    return v;
}
int pow(int a,int b,int c){
    int ans=1;
    while(b){
        if(b&1)ans=1LL*ans*a%c;
        a=1LL*a*a%c;b>>=1;
    }
    return ans;
}
ll pow(int a,int y){
    ll q=1,x=a;
    while(y){
        if(y&1){if(q>inf/x)return inf;q=q*x;}
        if(x>inf/x)x=inf+1;
        else x=x*x;
        y>>=1;
    }
    return q;
}
int g[N][33];
int solve(int l,int r,int m){
    //DBG(m)
    if(l==r){
        return f[l]%m;
    }
    int a1=0,b1=1;
    if(m==2){
        if(f[l]%2==0){
            return 0;
        }
        return 1;
    }
    for(auto &o:cal(m)){
        int q=pow(o.x,o.y),a=0,b=q;
        //cerr<<o.x<<" "<<q<<" "<<f[l]<<"..\n";
        if(f[l]%o.x==0){
            int k=0,z=f[l];
            while(z%o.x==0)k++,z/=o.x;
            int rem=(o.y+k-1)/k;
            k=r-l-1;
            int t;
            if(k>=33)t=inf;
            else t=g[r][k];
            if(t<rem){
                a=pow(f[l],t,q);
            }
        }
        else{
            int t=solve(l+1,r,q-q/o.x);
            a=pow(f[l],t,q);
        }
        ll x,y,c=a1-a;
        //cerr<<a<<" "<<b<<" "<<a1<<" "<<b1<<"\n";
        ll d=ex_gcd(b,b1,x,y);
        //cerr<<x<<" "<<y<<" "<<d<<" ,,\n";
        ll m=c/d,z=b1/d;
        x*=m;x%=z;if(x<0)x+=z;
        ll a2=1LL*b*x+a,b2=b*b1;
        a2%=b2;if(a2<0)a2+=b2;
        //cerr<<a<<" "<<a2%b<<",\t"<<a1<<" "<<a2%b1<<"\n";
        a1=a2,b1=b2;
        //cerr<<a1<<" "<<b1<<"??\n";
    }
    //DBG(a1)
    return a1;
}
int b[N];
int main(){
    //freopen("A.in","r",stdin);
    //freopen("A.out","w",stdout);
    //cout<<setprecision(9)<<fixed;
    int j,i,k,T,ca=0,K=0,n,m=0;
    scanf("%d%d",&n,&m);
    rep(i,0,n)scanf("%d",&f[i]);
    rep(i,0,n){
        g[i][0]=f[i];
        int q=g[i][0];
        per(j,0,i){
            k=i-j;
            if(k>=33)break;
            if(q<inf)q=pow(f[j],q);
            if(q>=inf)g[i][k]=inf;
            else g[i][k]=q;
        }
    }
    int last=n-1;
    per(i,0,n){
        if(f[i]==1)last=i;
        b[i]=last;
    }
    int Q,l,r;
    scanf("%d",&Q);
    while(Q--){
        scanf("%d%d",&l,&r);l--,r--;
        int ans=solve(l,min(r,b[l]),m);
        printf("%d\n",ans);
    }
    return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define inf 1000000007
#define mod 1000000007
#define x first
#define y second
#define pi acos(-1.0)
#define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
//#define dprintf(...)
#define hash _hash
//#define dprintf(...) fprintf(outFile,__VA_ARGS__)

#define FOREACH(it,x) for(__typeof(x.begin()) it=x.begin();it!=x.end();it++)
#define ull unsigned long long
#define ll long long
#define N 100010

template <class T,class U>inline void Max(T &a,U b){if(a<b)a=b;}
template <class T,class U>inline void Min(T &a,U b){if(a>b)a=b;}

//FILE* outFile;

inline void add(int &a,int b){a+=b;while(a>=mod)a-=mod;}

/*
int pow(int a,int b){
    int ans=1;
    while(b){
        if(b&1)ans=ans*(ll)a%mod;
        a=(ll)a*a%mod;b>>=1;
    }
    return ans;
}//*/
ll ex_gcd(ll a,ll b,ll &x,ll &y){
    if(b==0){
        x=1,y=0;return a;
    }
    ll x1,y1,d=ex_gcd(b,a%b,x1,y1);
    x=y1;
    y=x1-a/b*x;
    return d;
}
map<int,vector<pii>>h;
int f[N];
vector<pii> cal(int m){
    if(h.count(m))return h[m];
    int n=m;
    if(m==2)return h[n]=vector<pii>();
    vector<pii>v;
    rep(i,2,N){
        if(1LL*i*i>m)break;
        if(m%i==0){
            int k=0;
            while(m%i==0)m/=i,k++;
            v.pb({i,k});
        }
    }
    if(m>1)v.pb({m,1});
    h[n]=v;
    return v;
}
int pow(int a,int b,int c){
    int ans=1;
    while(b){
        if(b&1)ans=1LL*ans*a%c;
        a=1LL*a*a%c;b>>=1;
    }
    return ans;
}
ll pow(int a,int y){
    ll q=1,x=a;
    while(y){
        if(y&1){if(q*x>=inf)return inf;q=q*x;}
        x=x*x;y>>=1;
    }
    return q;
}
int g[N][33];
int solve(int l,int r,int m){
    //DBG(m)
    if(l==r){
        return f[l]%m;
    }
    int a1=0,b1=1;
    if(m==2){
        if(f[l]%2==0){
            return 0;
        }
        return 1;
    }
    for(auto &o:cal(m)){
        int q=pow(o.x,o.y),a=0,b=q;
        //cerr<<o.x<<" "<<q<<" "<<f[l]<<"..\n";
        if(f[l]%o.x==0){
            int k=0,z=f[l];
            while(z%o.x==0)k++,z/=o.x;
            int rem=(o.y+k-1)/k;
            k=r-l-1;
            int t;
            if(k>=33)t=inf;
            else t=g[r][k];
            if(t<rem){
                a=pow(f[l],t,q);
            }
        }
        else{
            int t=solve(l+1,r,q-q/o.x);
            a=pow(f[l],t,q);
        }
        ll x,y,c=a1-a;
        //cerr<<a<<" "<<b<<" "<<a1<<" "<<b1<<"\n";
        ll d=ex_gcd(b,b1,x,y);
        //cerr<<x<<" "<<y<<" "<<d<<" ,,\n";
        ll m=c/d,z=b1/d;
        x*=m;x%=z;if(x<0)x+=z;
        ll a2=1LL*b*x+a,b2=b*b1;
        a2%=b2;if(a2<0)a2+=b2;
        //cerr<<a<<" "<<a2%b<<",\t"<<a1<<" "<<a2%b1<<"\n";
        a1=a2,b1=b2;
        //cerr<<a1<<" "<<b1<<"??\n";
    }
    //DBG(a1)
    return a1;
}
int b[N];
int main(){
    //freopen("A.in","r",stdin);
    //freopen("A.out","w",stdout);
    //cout<<setprecision(9)<<fixed;
    int j,i,k,T,ca=0,K=0,n,m=0;
    scanf("%d%d",&n,&m);
    rep(i,0,n)scanf("%d",&f[i]);
    rep(i,0,n){
        g[i][0]=f[i];
        int q=g[i][0];
        per(j,0,i){
            k=i-j;
            if(k>=33)break;
            if(q<inf)q=pow(f[j],q);
            if(q>=inf)g[i][k]=inf;
            else g[i][k]=q;
        }
    }
    int last=n-1;
    per(i,0,n){
        if(f[i]==1)last=i;
        b[i]=last;
    }
    int Q,l,r;
    scanf("%d",&Q);
    while(Q--){
        scanf("%d%d",&l,&r);l--,r--;
        int ans=solve(l,min(r,b[l]),m);
        printf("%d\n",ans);
    }
    return 0;
}


//#include <bits/stdc++.h>
#include <stdlib.h>
#include<iomanip>
#include<string.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<string>
#include<fstream>
#include<stdio.h>
#include<map>
#include<set>
#include<vector>
#include<bitset>
#define  PI 3.1415926536
#define Mod 1000000007
using namespace std;
bool check(char x, char y)
{
	if (x - '0' + y - '0' == 9)return 1;
	return 0;
}
int main()
{
	unsigned long long res = 1;
	string s;
	cin >> s;
	int sz = s.length();
	for (int i = 0; i < sz; i++)
	{
		int x = 0;
		if (i == 0){
			if(check(s[i], s[i + 1]))x++;
		}
		else if (i == sz - 1 ){
			if(check(s[i], s[i - 1]))x++;
		}
		else
		{
			if (check(s[i], s[i - 1]))x++;
			if (check(s[i + 1], s[i]))x++;
		}
		if (x>0)res *= x;

		res =res% Mod;
	}
	cout << res << endl;
	return 0;
}


//#include <bits/stdc++.h>
#include <stdlib.h>
#include<iomanip>
#include<string.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<string>
#include<fstream>
#include<stdio.h>
#include<map>
#include<set>
#include<vector>
#include<bitset>
#define  PI 3.1415926536
#define Mod 1000000007
using namespace std;
bool check(char x, char y)
{
	if (x - '0' + y - '0' == 9)return 1;
	return 0;
}
int main()
{
	unsigned long long res = 1;
	string s;
	cin >> s;
	int sz = s.length();
	for (int i = 0; i < sz; i++)
	{
		int x = 0;
		if (i == 0){
			if(check(s[i], s[i + 1]))x++;
		}
		else if (i == sz - 1 ){
			if(check(s[i], s[i - 1]))x++;
		}
		else
		{
			if (check(s[i], s[i - 1]))x++;
			if (check(s[i + 1], s[i]))x++;
		}
		if (x>0)res *= x;

		//res =res% Mod;
	}
	if (res == 0)cout << "1" << endl;
	else cout << res << endl;
	return 0;
}


//#include <bits/stdc++.h>
#include <stdlib.h>
#include<iomanip>
#include<string.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<string>
#include<fstream>
#include<stdio.h>
#include<map>
#include<set>
#include<vector>
#include<bitset>
#define  PI 3.1415926536
using namespace std;
bool check(char x, char y)
{
	if (x - '0' + y - '0' == 9)return 1;
	return 0;
}
int toto(char x)
{
	return x - '0';
}
int main()
{
	unsigned long long res = 1;
	string s;
	cin >> s;
	s += '0';
	int sz = s.length();
	int L = 0, R = 0;
	bool big_flag = 0,small_flag=0;
	int rkmy = 0;
	for (int i = 0; i < sz; i++)
	{
		if (!big_flag)
		{
			L = i;
			big_flag = 1;
			small_flag = 1;
			rkmy = toto(s[i]);
		}
		else
		{
			if (small_flag == 1&&9-toto(s[i])==rkmy)small_flag = 0;
			else if (small_flag == 0&&toto(s[i])==rkmy)small_flag = 1;
			else
			{
				int temp = i - L;
				if (temp & 1)res *= ((temp / 2) + 1);
				big_flag = 0;
				i--;
			}
		}
	}
	 cout << res << endl;
	return 0;
}


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

int main()
{
    char a, b;
    b = '9';
    long long sum = 0, ans = 1;
    while(scanf("%c", &a) && a!='\n')
    {
        if(a - '0' + b - '0' == 9)
        {
            sum++;
 //           printf("%c %c %d\n", a, b, sum);
        }
        else
        {
            if(sum % 2 == 0 && sum > 0)
            {
                ans *= (sum/2+1);
            }
            sum = 0;
        }
        b = a;
    }
    if(sum % 2 == 0 && sum > 0)
        printf("%I64d", ans*(sum/2+1));
    else
        printf("%I64d", ans);
    return 0;
}


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

int main()
{
    char a, b;
    b = '9';
    int sum = 0, ans = 1;
    while(scanf("%c", &a) && a!='\n')
    {
        if(a - '0' + b - '0' == 9)
        {
            sum++;
 //           printf("%c %c %d\n", a, b, sum);
        }
        else
        {
            if(sum % 2 == 0 && sum > 0)
            {
                ans *= (sum/2+1);
            }
            sum = 0;
        }
        b = a;
    }
    if(sum % 2 == 0 && sum > 0)
        printf("%d", (sum/2+1));
    else
        printf("%d", ans);
    return 0;
}


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

int main()
{
    char a, b;
    b = '9';
    long long sum = 0, ans = 1;
    while(scanf("%c", &a) && a!='\n')
    {
        if(a - '0' + b - '0' == 9)
        {
            sum++;
 //           printf("%c %c %d\n", a, b, sum);
        }
        else
        {
            if(sum % 2 == 0 && sum > 0)
            {
                ans *= (sum/2+1);
            }
            sum = 0;
        }
        b = a;
    }
    if(sum % 2 == 0 && sum > 0)
        printf("%I64d", (sum/2+1));
    else
        printf("%I64d", ans);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define trace(x) cerr << #x << ": " << x << endl;
#define trace2(x, y) cerr << #x << ": " << x << " " << #y << ": " << y << endl;
#define trace3(x, y, z) cerr << #x << ": " << x << " " << #y << ": " << y << " " << #z << ": " << z << endl;
#define trace4(x, y, z, a) cerr << #x << ": " << x << " " << #y << ": " << y << " " << #z << ": " << z << " " << #a << ": " << a << endl;

typedef long long lolo;
typedef unsigned long long ulolo;

const int MAXN = 100 + 100;

int main()
{
  std::ios::sync_with_stdio(false);
	string s;
	cin >> s;
	s.push_back('9');
	ulolo slen = s.length() + 1;
	ulolo ans = 1, st = slen;
	for (int i = 1; i < slen; ++i) {
		if (s[i - 1] - '0' + s[i] - '0' == 9) {
			if (st == slen) st = i - 1;
			continue;
		}
		ulolo diff = i - st;
		if (diff & 1)	ans *= diff;
		st = slen;
	}
	cout << ans << "\n";
	return 0;
}

// g++ -W -Wall -Wextra -pedantic -std=c++0x -o p main.cpp


#include <bits/stdc++.h>

using namespace std;

#define trace(x) cerr << #x << ": " << x << endl;
#define trace2(x, y) cerr << #x << ": " << x << " " << #y << ": " << y << endl;
#define trace3(x, y, z) cerr << #x << ": " << x << " " << #y << ": " << y << " " << #z << ": " << z << endl;
#define trace4(x, y, z, a) cerr << #x << ": " << x << " " << #y << ": " << y << " " << #z << ": " << z << " " << #a << ": " << a << endl;

typedef long long lolo;
typedef unsigned long long ulolo;

const int MAXN = 100 + 100;

int main()
{
  std::ios::sync_with_stdio(false);
	string s;
	cin >> s;
	s.push_back('9');
	ulolo slen = s.length() + 1;
	ulolo ans = 1, st = slen;
	for (int i = 1; i < slen; ++i) {
		if (s[i - 1] - '0' + s[i] - '0' == 9) {
			if (st == slen) st = i - 1;
			continue;
		}
		if (st != slen) {
			ulolo diff = i - st;
			if (diff & 1)	ans *= diff - 1;
			st = slen;
		}
	}
	cout << ans << "\n";
	return 0;
}

// g++ -W -Wall -Wextra -pedantic -std=c++0x -o p main.cpp


#include <bits/stdc++.h>

using namespace std;

#define trace(x) cerr << #x << ": " << x << endl;
#define trace2(x, y) cerr << #x << ": " << x << " " << #y << ": " << y << endl;
#define trace3(x, y, z) cerr << #x << ": " << x << " " << #y << ": " << y << " " << #z << ": " << z << endl;
#define trace4(x, y, z, a) cerr << #x << ": " << x << " " << #y << ": " << y << " " << #z << ": " << z << " " << #a << ": " << a << endl;

typedef long long lolo;
typedef unsigned long long ulolo;

const int MAXN = 100 + 100;

int main()
{
  std::ios::sync_with_stdio(false);
	string s;
	cin >> s;
	s.push_back('9');
	ulolo slen = s.length() + 1;
	ulolo ans = 1, st = slen;
	for (int i = 1; i < slen; ++i) {
		if (s[i - 1] - '0' + s[i] - '0' == 9) {
			if (st == slen) st = i - 1;
			continue;
		}
		if (st != slen) {
			ulolo diff = i - st;
			if (diff & 1)	ans *= diff / 2 + 1;
			// trace3(i, st, ans);
			st = slen;
		}
	}
	cout << ans << "\n";
	return 0;
}

// g++ -W -Wall -Wextra -pedantic -std=c++0x -o p main.cpp


#include <bits/stdc++.h>

using namespace std;

#define trace(x) cerr << #x << ": " << x << endl;
#define trace2(x, y) cerr << #x << ": " << x << " " << #y << ": " << y << endl;
#define trace3(x, y, z) cerr << #x << ": " << x << " " << #y << ": " << y << " " << #z << ": " << z << endl;
#define trace4(x, y, z, a) cerr << #x << ": " << x << " " << #y << ": " << y << " " << #z << ": " << z << " " << #a << ": " << a << endl;

typedef long long lolo;
typedef unsigned long long ulolo;

const int MAXN = 100 + 100;

int main()
{
  std::ios::sync_with_stdio(false);
	string s;
	cin >> s;
	s.push_back('9');
	ulolo ans = 1, st  = -1;
	for (int i = 1; i < s.length(); ++i) {
		if (s[i - 1] - '0' + s[i] - '0' == 9) {
			if (st == -1) st = i - 1;
			continue;
		}
		ulolo diff = i - st;
		if (diff & 1)	ans *= diff
		st = -1;
	}
	cout << ans << "\n";
	return 0;
}

// g++ -W -Wall -Wextra -pedantic -std=c++0x -o p main.cpp


#include <iostream>
using namespace std;
int main(){
string s;
cin>>s;
long long x=0;
long long sum=1;
for(int i=0;i<s.length();i++){
    x=0;
    while(s[i]+s[i+1]-'0'-'0'==9&&i+1<s.length()){x++;i++;}
    if(x==2)sum*=x;
}
cout<<sum;

}


#include <iostream>
using namespace std;
int main(){
string s;
cin>>s;
long long x=0;
long long sum=1;
for(int i=0;i<s.length();i++){
    x=0;
    while(s[i]+s[i+1]-'0'-'0'==9&&i+1<s.length()){x++;i++;}
    if(x%2==0&&x>=2){sum*=2;if(x>2)sum*=2;}

}
cout<<sum;

}


#include <iostream>
using namespace std;
int main(){
string s;
cin>>s;
int x=0;
long long sum=1;
for(int i=0;i<s.length();i++){
    x=0;
    while(s[i]+s[i+1]-'0'-'0'==9&&i+1<s.length()){x++;i++;}
    if(x%2==1&&x>2)sum*=2;
}
cout<<sum;

}


#include <iostream>
using namespace std;
int main(){
string s;
cin>>s;
long long x=0;
long long sum=1;
for(int i=0;i<s.length();i++){
    x=0;
    while(s[i]+s[i+1]-'0'-'0'==9&&i+1<s.length()){x++;i++;}
    if(x%2==0&&x>=2){sum*=(x/2+1);}

}
cout<<sum;

}


#include <iostream>
using namespace std;
int main(){
string s;
cin>>s;
int x=0;
long long sum=1;
for(int i=0;i<s.length();i++){
    x=0;
    while(s[i]+s[i+1]-'0'-'0'==9&&i+1<s.length()){x++;i++;}
    if(x%2==0&&x>2)sum*=2;
}
cout<<sum;

}


#include <iostream>
using namespace std;
int main(){
string s;
cin>>s;
int x=0;
long long sum=1;
for(int i=0;i<s.length();i++){
    x=0;
    while(s[i]+s[i+1]-'0'-'0'==9&&i+1<s.length()){x++;i++;}
    if(x%2==1)sum*=2;
}
cout<<sum;

}


#include <iostream>
using namespace std;
int main(){
string s;
cin>>s;
int x=0;
long long sum=1;
for(int i=0;i<s.length();i++){
    x=0;
    while(s[i]+s[i+1]-'0'-'0'==9&&i+1<s.length()){x++;i++;}
    if(x%2==0&&x>=2)sum*=2;
}
cout<<sum;

}


#include <iostream>
using namespace std;
int main(){
string s;
cin>>s;
long long x=0;
long long sum=1;
for(int i=0;i<s.length();i++){
    x=0;
    while(s[i]+s[i+1]-'0'-'0'==9&&i+1<s.length()){x++;i++;}
    if(x%2==0&&x>=2)sum*=2;
    if(x>0)i--;
}
cout<<sum;

}


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

#define N 100010

char s[N];
int  n, k, num;
long long res;

int main() {
	scanf("%s", s);
	n = strlen(s);
	for (int i = 1; i <= n; i ++) {
        if ((s[i] - '0' + s[i - 1] - '0' == 9) && i != n) num ++;
        else {
			if (num % 2 == 0 && num) k ++;
			num = 0;
        }
	}
	res = 1;
	while (k --) res *= (1LL * 2);
	printf("%I64d", res);
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100000

char a[N];
int x, y, z;
ll p, q, r;

int main(){
	scanf("%s", a + 1);
	int len = strlen(a + 1), i;
	for(i = 1; i <= len; i++) a[i] -= '0';
	q = 1; p = 1;
	for(i = 2; i <= len; i++){
		r = 0;
		if(a[i] + a[i - 1] == 9){
			z = max(x + 1, y);
			if(z == y) r += q;
			if(z == x + 1) r += p;
		}
		else z = y, r = q;
		x = y; y = z;
		p = q; q = r;
	}
	cout << q << endl;
}


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

#define N 100010

char s[N];
int  n, k, num[N];
long long res;

int main() {
	scanf("%s", s);
	n = strlen(s);
	for (int i = 1; i < n; i ++) {
        if ((s[i] - '0' + s[i - 1] - '0' == 9)) num[k] ++;
        else k ++;
	}
	res = 1;
	for (int i = 0; i <= k; i ++) if (num[i] % 2 == 0)res *= (1LL * ((num[i] / 2) + 1));
	printf("%I64d", res);
}


#include <bits/stdc++.h>
using namespace std;
#define ll          long long int
#define pb          push_back
#define pll         pair<ll,ll>
#define pq          priority_queue
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mod         1000000007
#define inf         10000000000000000000LL
#define vl          vector<ll>
#define vvl         vector<vector<ll>>
#define F           first
#define S           second
#define db          double
#define sz(x)       (ll)x.size()
#define fix(n)      cout<<fixed<<setprecision(n)
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define all(x)      x.begin(),x.end()
#define mset(x)     memset(x,0,sizeof x)
#define pi          3.14159265

void solve()
{
    string s;
    cin>>s;
    ll ans=1;
    ll p=-1,c=0;
    ll cnt=1;
    for(ll i=1;i<sz(s); i++)
    {
        char x=s[i];
        char y=s[i-1];
        //cout<<x<<" "<<y<<endl;
        c=x-'0';
        p=y-'0';
        if(c+p==9) { cnt++; }
        else
        {
            //cout<<cnt<<" ";
            ans*=max(1LL,(cnt%2==0?1:(cnt+1)/2)); cnt=1;
        }
    }
    //cout<<cnt<<" ";
    ans*=max(1LL,(cnt%2==0?1:(cnt+1)/2));
    cout<<ans;
}

int main()
{
    speed_up
    ll T=1;
    //cin>>T;
    while(T--)
        solve();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll          long long int
#define pb          push_back
#define pll         pair<ll,ll>
#define pq          priority_queue
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mod         1000000007
#define inf         10000000000000000000LL
#define vl          vector<ll>
#define vvl         vector<vector<ll>>
#define F           first
#define S           second
#define db          double
#define sz(x)       (ll)x.size()
#define fix(n)      cout<<fixed<<setprecision(n)
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define all(x)      x.begin(),x.end()
#define mset(x)     memset(x,0,sizeof x)
#define pi          3.14159265

void solve()
{
    string s;
    cin>>s;
    ll ans=1;
    ll p=-1,c=0;
    ll cnt=0;
    for(ll i=1;i<sz(s); i++)
    {
        char x=s[i];
        char y=s[i-1];
        //cout<<x<<" "<<y<<endl;
        c=x-'0';
        p=y-'0';
        if(c+p==9) { cnt++; }
        else
        {
            ans*=max(1LL,(cnt%2==0?1:(cnt+1)/2)); cnt=1;
        }
    }
    ans*=max(1LL,(cnt%2==0?1:(cnt+1)/2));
    cout<<ans;
}

int main()
{
    speed_up
    ll T=1;
    //cin>>T;
    while(T--)
        solve();
    return 0;
}


void solve()
{
    string s;
    cin>>s;
    ll ans=1;
    ll p=-1,c=0;
    ll cnt=0;
    for(ll i=1;i<sz(s); i++)
    {
        char x=s[i];
        char y=s[i-1];
        //cout<<x<<" "<<y<<endl;
        c=x-'0';
        p=y-'0';
        if(c+p==9) { cnt++; }
        else { ans*=max(1LL,(cnt+1)/2); cnt=1; }
    }
    ans*=max(1LL,(cnt+1)/2);
    cout<<ans;
}

int main()
{
    speed_up
    ll T=1;
    //cin>>T;
    while(T--)
        solve();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll          long long int
#define pb          push_back
#define pll         pair<ll,ll>
#define pq          priority_queue
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mod         1000000007
#define inf         10000000000000000000LL
#define vl          vector<ll>
#define vvl         vector<vector<ll>>
#define F           first
#define S           second
#define db          double
#define sz(x)       (ll)x.size()
#define fix(n)      cout<<fixed<<setprecision(n)
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define all(x)      x.begin(),x.end()
#define mset(x)     memset(x,0,sizeof x)
#define pi          3.14159265

void solve()
{
    string s;
    cin>>s;
    ll ans=1;
    ll p=-1,c=0;
    ll cnt=0;
    for(ll i=1;i<sz(s); i++)
    {
        char x=s[i];
        char y=s[i-1];
        //cout<<x<<" "<<y<<endl;
        c=x-'0';
        p=y-'0';
        if(c+p==9) { cnt++; }
        else { ans*=max(1LL,(cnt+1)/2); cnt=1; }
    }
    ans*=max(1LL,(cnt+1)/2);
    cout<<ans;
}

int main()
{
    speed_up
    ll T=1;
    //cin>>T;
    while(T--)
        solve();
    return 0;
}


#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define clr(v, d) memset(v, d, sizeof(v))
#define pb push_back
#define mp make_pair
#define F first
#define S second

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef pair<int, int> ii;
typedef vector<int> vii;

const int OO = (int)1e9+7;
const int MAX = (int)1e5+7;

int main(void){

    string s;

    while(cin>>s){

        int len = s.length();

        ll res = 1;

        for(int i=1; i<len-1; i++)if( (s[i-1]-'0')+(s[i]-'0')==9  &&  (s[i+1]-'0') + (s[i]-'0') == 9 ){

            int can = 1;

            if(i>1 && (s[i-1]-'0')+(s[i-2]-'0')==9)
                can = 0;
            if(i<len-2 && (s[i+1]-'0')+(s[i+2]-'0')==9)
                can = 0;

            if(can)
                res *= 2;

        }

        cout<<res<<"\n";

    }

    return 0;
}


#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define clr(v, d) memset(v, d, sizeof(v))
#define pb push_back
#define mp make_pair
#define F first
#define S second

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef pair<int, int> ii;
typedef vector<int> vii;

const int OO = (int)1e9+7;
const int MAX = (int)1e5+7;

int main(void){

    string s;

    while(cin>>s){

        int len = s.length();

        ll res = 1;
        for(int i=1; i<len-1; i++)if( (s[i-1]-'0')+(s[i]-'0')==9  &&  (s[i+1]-'0') + (s[i]-'0') == 9 )
            res *= 2;

        cout<<res<<"\n";

    }

    return 0;
}


#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define clr(v, d) memset(v, d, sizeof(v))
#define pb push_back
#define mp make_pair
#define F first
#define S second

using namespace std;

typedef unsigned long long ll;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef pair<int, int> ii;
typedef vector<int> vii;

const int OO = (int)1e9+7;
const int MAX = (int)1e5+7;

int main(void){

    string s;

    while(cin>>s){

        int len = s.length();

        ll res = 1;

       for(int i=1; i<len-1; i++) if( (s[i-1]-'0')+(s[i]-'0')==9  &&  (s[i+1]-'0') + (s[i]-'0') == 9 ){

            //cout<<i<<"\n";

            int cnt = 1;

            i++;

            while(i<len-1 && (s[i-1]-'0')+(s[i]-'0')==9  &&  (s[i+1]-'0') + (s[i]-'0') == 9){
                cnt++;
                i++;
            }

            //cout<<"cnt:"<<cnt<<"\n";

            if((cnt+2) % 2)
                res *= (((cnt+2)/2) +1);
        }

        cout<<res<<"\n";

    }

    return 0;
}


#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define clr(v, d) memset(v, d, sizeof(v))
#define pb push_back
#define mp make_pair
#define F first
#define S second

using namespace std;

typedef unsigned long long ll;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef pair<int, int> ii;
typedef vector<int> vii;

const int OO = (int)1e9+7;
const int MAX = (int)1e5+7;

int main(void){

    string s;

    while(cin>>s){

        int len = s.length();

        ll res = 1;

        for(int i=1; i<len-1; i++) if( (s[i-1]-'0')+(s[i]-'0')==9  &&  (s[i+1]-'0') + (s[i]-'0') == 9 ){

            int can = 1;

            if(i>1 && (s[i-1]-'0')+(s[i-2]-'0')==9)
                can = 0;
            if(i<len-2 && (s[i+1]-'0')+(s[i+2]-'0')==9)
                can = 0;

            if(can)
                res *= 2;

        }

        cout<<res<<"\n";

    }

    return 0;
}


#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define clr(v, d) memset(v, d, sizeof(v))
#define pb push_back
#define mp make_pair
#define F first
#define S second

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef pair<int, int> ii;
typedef vector<int> vii;

const int OO = (int)1e9+7;
const int MAX = (int)1e5+7;

int main(void){

    string s;

    while(cin>>s){

        int len = s.length();

        ll res = 1;
        int cnt = 1, f= 0;
        for(int i=1; i<len-1; i++)if( (s[i-1]-'0')+(s[i]-'0')==9  &&  (s[i+1]-'0') + (s[i]-'0') == 9 ){

            res *= 2;

            if(i>1 && (s[i-2]-'0')+(s[i-1]-'0')==9){
                cnt++;
                f = 1;
            }else{
                if(i>1 && f){
                    while(cnt--)
                        res /=2;
                    cnt = 1;
                }
                f = 0;
            }

        }

        //cout<<cnt<<" " << res << "\n";

        while(f && cnt--)
            res /=2;

        cout<<res<<"\n";

    }

    return 0;
}


#include <iostream>
#include <string>

using namespace std;
int main(){
	string s;
	cin>>s;
	if(s.length()<3){cout<<1<<endl; return 0;}
	for(int i=0;i<s.length()-2;i++){
		if((s[i]-'0')+(s[i+1]-'0')==9 && (s[i+1]-'0')+(s[i+2]-'0')==9 ){
			cout<<2<<endl;
			return 0;
		}
	}
	cout<<1<<endl;

}


#include <iostream>
#include <string>

using namespace std;
int main(){
	string s;
	cin>>s;
	if(s.length()<3){cout<<1<<endl; return 0;}
	for(int i=0;i<s.length()-2;i++){
		if((s[i]-'0')+(s[i+1]-'0')==9 && (s[i+1]-'0')+(s[i+2]-'0')==9 && s.length()%2!=0){
			cout<<2<<endl;
			return 0;
		}
	}
	cout<<1<<endl;

}


#include <iostream>
#include <string>

using namespace std;
int main(){
	string s;
	cin>>s;
	long long c=1;
	long long d=1;
	for(long long i=0;i<s.length()-1;i++){
			if((s[i]-'0')+(s[i+1]-'0')==9) d++;

			else{
				c*=(d%2==0?1:(d/2)+1);
				d=1;
			}
	}
	c*=(d%2==0?1:(d/2)+1);
	cout<<c<<endl;

}


#include <iostream>
#include <string>

using namespace std;
int main(){
	string s;
	cin>>s;
	for(int i=0;i<s.length()-2;i++){
		if((s[i]-'0')+(s[i+1]-'0')==9 && (s[i+1]-'0')+(s[i+2]-'0')==9 ){
			cout<<2<<endl;
			return 0;
		}
	}
	cout<<1<<endl;

}


#include <iostream>
#include <string>

using namespace std;
int main(){
	string s;
	cin>>s;
	int c=0;
	if(s.length()<3){cout<<1<<endl; return 0;}
	for(int i=0;i<s.length()-2;i++){
		if((s[i]-'0')+(s[i+1]-'0')==9 && (s[i+1]-'0')+(s[i+2]-'0')==9){
			c++;
		}
	}
	cout<<(c%2==0?1:2)<<endl;

}


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

int arr[200000];

int main()
{

    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    string str;
    cin>>str;
    for(int i=0; i<str.size()-1; i++){
        int x = 9-(str[i]-'0');
        int y = (str[i+1]-'0');
        if(x == y)
            arr[i] = 1;
    }

    long long res = 1;
    for(int i=0; i<str.size(); i++){
        if(arr[i] == 0)
            continue;
        arr[i] += arr[i-1];
    }
    for(int i=0; i<str.size(); i++){
        if(arr[i]%2==0 && arr[i]!=0 && arr[i+1] == 0)
            res*=2;
    }
    cout<<res<<endl;
    return 0;
}


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

int arr[200000];

int main()
{

    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    string str;
    cin>>str;
    for(int i=0; i<str.size()-1; i++){
        int x = 9-(str[i]-'0');
        int y = (str[i+1]-'0');
        if(x == y)
            arr[i] = 1;
    }

    long long res = 1;
    for(int i=0; i<str.size(); i++){
        if(arr[i] == 0)
            continue;
        arr[i] += arr[i-1];
    }
    for(int i=0; i<str.size(); i++){
        if(arr[i]%2==0 && arr[i]!=0 && arr[i+1] == 0)
            res*=((arr[i])/2+1);
    }
    cout<<res<<endl;
    return 0;
}


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

int arr[200000];

int main()
{

    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    string str;
    cin>>str;
    for(int i=0; i<str.size()-1; i++){
        int x = 9-(str[i]-'0');
        int y = (str[i+1]-'0');
        cout<<x<<y<<endl;
        if(x == y)
            arr[i] = 1;
    }

    long long res = 1;
    for(int i=0; i<str.size(); i++){
        if(arr[i] == 0)
            continue;
        arr[i] += arr[i-1];
    }
    for(int i=0; i<str.size(); i++){
        if(arr[i]%2==0 && arr[i]!=0 && arr[i+1] == 0)
            res*=2;
    }
    cout<<res<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define lld long long int
#define faster ios_base::sync_with_stdio(false);cin.tie(0);

using namespace std;

int main(){

  string input;
  cin>>input;

  lld ans = 1;
  lld temp = 0;
  int i=0;
  while(i<input.length()){
    temp = 0;
    if(i<input.length()-1 && (input[i]-'0'+input[i+1]-'0')==9){
      temp = 2;
      i++;
      while(i<input.length()-1 && (input[i]-'0'+input[i+1]-'0')==9){
        temp++;i++;
      }
      if(temp%2==1)ans *= (temp+1)/2;
    } else i++;
  }
  cout<<ans<<endl;

}


#include<bits/stdc++.h>
#define lld long long int
#define faster ios_base::sync_with_stdio(false);cin.tie(0);

using namespace std;

int main(){

  string input;
  cin>>input;

  int ans = 1;
  int temp = 0;
  int i=0;
  while(i<input.length()){
    temp = 0;
    if(i<input.length()-1 && (input[i]-'0'+input[i+1]-'0')==9){
      temp = 2;
      i++;
      while(i<input.length()-1 && (input[i]-'0'+input[i+1]-'0')==9){
        temp++;i++;
      }
      if(temp%2==1)ans *= (temp+1)/2;
    } else i++;
  }
  cout<<ans<<endl;

}


#include<bits/stdc++.h>
#define lld long long int
#define faster ios_base::sync_with_stdio(false);cin.tie(0);

using namespace std;

int main(){

  string input;
  cin>>input;

  int ans = 0;
  int temp = 0;
  int i=0;
  while(i<input.length()){
    temp = 0;
    if(i<input.length()-1 && (input[i]-'0'+input[i+1]-'0')==9){
      temp = 2;
      i++;
      while(i<input.length()-1 && (input[i]-'0'+input[i+1]-'0')==9){
        temp++;i++;
      }
      if(temp%2==1)ans += (temp+1)/2;
    } else i++;
  }
  ans = max(1,ans);
  cout<<ans<<endl;

}


#include <bits/stdc++.h>
typedef long long ll ;
using namespace std;

int main()
{
    string s ;
    cin >> s ;
     vector < bool > b ;
    ll res = 1;
     b.resize(s.size() + 10);
    for(int i = 0 ; i < s.size() - 1 ; i++){
            int x = s[i] - '0';
            int y = s[i + 1] - '0';
            if(x + y == 9)
                  b[i] = true ;
    }
    ll cnt = 0 ;
    for(int i = 0 ; i < s.size() ; i++){
            if(b[i]) cnt++ ;

            else if(cnt % 2 == 0) res = res * ((cnt  / 2) + 1) , cnt = 0 ;

            else cnt = 0 ;
    }

    cout << res << endl ;

    return 0;
}


#include <bits/stdc++.h>
typedef long long ll ;
using namespace std;

/*

Implementation , Greedy , Sorting , Brute_Force

Binary_Search , Tow_Pointer , Prefix_Sum , STL ( set , map , queue , lower_bound )

Graph : DFS , BFS ,  Dijkstra , Floyd

Math : Simple_Math , Modular_Arithmetic , GCD_LCM , Primes

DP : Top_Down , Pick_Or_Leave

*/

int main()
{

   string s ;

   cin >> s;

  if(s.size() == 1){
     cout << 1 << endl ;
     return 0 ;
  }

   ll res = 0 ;

   bool b = 0 ;
   for(int i = 1 ; i < s.size() - 1 ; i++){
          int x = (s[i] - '0') + (s[i + 1] - '0');
          int y = (s[i] - '0') + (s[i - 1] - '0');
          if(x == y && x == 9) res += 2 ;

          else if(x == 9 || y == 9) b = 1 ;
   }
   if(!res && b) res += b ;
   cout << res << endl ;
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll ;
using namespace std;

/*

Implementation , Greedy , Sorting , Brute_Force

Binary_Search , Tow_Pointer , Prefix_Sum , STL ( set , map , queue , lower_bound )

Graph : DFS , BFS ,  Dijkstra , Floyd

Math : Simple_Math , Modular_Arithmetic , GCD_LCM , Primes

DP : Top_Down , Pick_Or_Leave

*/

int main()
{

   string s ;

   cin >> s;

   ll res = 1 ;
   ll ans = 0 ;

   for(int i = 1 ; i < s.size() - 1 ; i++){
      int x = (s[i] - '0') + (s[i -1] - '0') ;
      int y = (s[i] - '0') + (s[i + 1] - '0');

      if(x == y && x == 9) ans += 2;
   }

   if(ans ) cout << ans << endl ;

   else cout  << res << endl ;

    return 0;
}


using namespace std;
#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
#define pb push_back
#define mp make_pair
#define ft first
#define sc second
#define bs binary_search
#define INF 1000000000
#define rc(s) return cout << s,0
const ll mod = 1e9 + 7;
/*----------------------------*/
int binomialCoeff(int n, int k)
{
    int res = 1;
    if ( k > n - k )
        k = n - k;
    for (int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }

    return res;
}
/*----------------------------*/
int main() {
	string s;
	cin>>s;
	vi len;
	ll cnt=1;
	for(int i=0;i<s.size();i++)if(s[i]+s[i+1]-'0'-'0'==9)cnt++;else if(cnt!=1)len.pb(cnt-1),cnt=1;
	ll ans=1;
	len.pb(cnt);
	for(int i=0;i<len.size();i++)ans*=len[i];
	rc(ans);
	//THIS IS THE END
}


using namespace std;
#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
#define pb push_back
#define mp make_pair
#define ft first
#define sc second
#define bs binary_search
#define INF 1000000000
#define rc(s) return cout << s,0
const ll mod = 1e9 + 7;
/*----------------------------*/
int binomialCoeff(int n, int k)
{
    int res = 1;
    if ( k > n - k )
        k = n - k;
    for (int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }

    return res;
}
/*----------------------------*/
int main() {
	string s;
	cin>>s;
	vi len;
	ll cnt=1;
	len.pb(1);
	for(int i=0;i<s.size();i++)if(s[i]+s[i+1]-'0'-'0'==9)cnt++;else if(cnt!=1)len.pb(cnt),cnt=1;
	ll ans=1;
	len.pb(cnt);
	for(int i=0;i<len.size();i++)if(len[i]%2!=0&&len[i]!=1)ans*=len[i]-1;
	rc(ans);
	//THIS IS THE END
}


using namespace std;
#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
#define pb push_back
#define mp make_pair
#define ft first
#define sc second
#define bs binary_search
#define INF 1000000000
#define rc(s) return cout << s,0
const ll mod = 1e9 + 7;
/*----------------------------*/
int binomialCoeff(int n, int k)
{
    int res = 1;
    if ( k > n - k )
        k = n - k;
    for (int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }

    return res;
}
/*----------------------------*/
int main() {
	string s;
	cin>>s;
	vi len;
	ll cnt=1;
	len.pb(1);
	for(int i=0;i<s.size();i++)if(s[i]+s[i+1]-'0'-'0'==9)cnt++;else if(cnt!=1)len.pb(cnt),cnt=1;
	ll ans=1;
	len.pb(cnt);
	for(int i=0;i<len.size();i++)if(len[i]%2!=0)ans*=len[i]/2+1;
	rc(max(ans,(ll)1));
	//THIS IS THE END
}


#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
int main(){
	string x;
	cin>>x;
	long long counter=0,answer=1;
	for(int i=0;i<x.size();i++){
		if((x[i]-'0')+(x[i+1]-'0')==9){
			counter++;
		}else{
			if(!(counter%2) && counter>1){
				answer*=(counter)/2+1;
			}
			counter=0;
		}

	}
	cout<<answer<<endl;

	return 0;
}