由于递推形式是乘法,没法直接矩乘
考虑将每个数表示成
c
a
1
∗
f
1
a
2
∗
f
2
a
3
∗
f
3
a
4
c^{a_1}*f_1^{a_2}*f_2^{a_3}*f_3^{a_4}
ca1∗f1a2∗f2a3∗f3a4的形式
然后对指数就是加法递推了
随便矩乘搞一下就完了
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
inline ll readll(){
char ch=gc();
ll res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
int mod;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline int fix(int x){return (x<0)?x+mod:x;}
int m;
struct mat{
int a[5][5];
mat(){memset(a,0,sizeof(a));}
inline void clear(){memset(a,0,sizeof(a));}
friend inline mat operator *(cs mat &a,cs mat &b){
mat c;
for(int i=0;i<m;i++)
for(int k=0;k<m;k++)
for(int j=0;j<m;j++)
Add(c.a[i][j],mul(a.a[i][k],b.a[k][j]));
return c;
}
};
int c,f1,f2,f3;
ll n;
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
#endif
n=readll()-3,f1=read(),f2=read(),f3=read(),c=read();
m=3;int ans=1;
mod=1e9+6;
mat a;a.a[0][0]=1,a.a[0][1]=1,a.a[1][0]=1,a.a[1][2]=1,a.a[2][0]=1;
mat res;res.a[0][0]=1,res.a[1][1]=1,res.a[2][2]=1;
for(ll x=n;x;x>>=1,a=a*a)if(x&1)res=res*a;
mod=1e9+7;
// cout<<res.a[2][0]<<" "<<res.a[1][0]<<" "<<res.a[0][0]<<'\n';
Mul(ans,ksm(f1,res.a[2][0]));
Mul(ans,ksm(f2,res.a[1][0]));
Mul(ans,ksm(f3,res.a[0][0]));
m=5,mod=1e9+6;
a.clear();
a.a[0][0]=1,a.a[1][0]=1,a.a[2][0]=1,a.a[3][0]=2,a.a[4][0]=mod-4,a.a[0][1]=1,a.a[1][2]=1,a.a[3][3]=1,a.a[4][3]=1,a.a[4][4]=1;
res.clear();
res.a[0][0]=1,res.a[1][1]=1,res.a[2][2]=1,res.a[3][3]=1,res.a[4][4]=1;
for(ll x=n;x;x>>=1,a=a*a)if(x&1)res=res*a;
int pw=add(mul(res.a[3][0],3),res.a[4][0]);
// cout<<pw<<'\n';
mod=1e9+7;
Mul(ans,ksm(c,pw));
cout<<ans<<'\n';
}