1 /**************************************************************
2 Problem: 3613
3 User: Tunix
4 Language: C++
5 Result: Accepted
6 Time:6136 ms
7 Memory:1272 kb
8 ****************************************************************/
9
10 //Huce #6 D
11 #include<vector>
12 #include<cstdio>
13 #include<cstdlib>
14 #include<cstring>
15 #include<iostream>
16 #include<algorithm>
17 #define rep(i,n) for(int i=0;i<n;++i)
18 #define F(i,j,n) for(int i=j;i<=n;++i)
19 #define D(i,j,n) for(int i=j;i>=n;--i)
20 using namespace std;
21
22 int getint(){
23 int v=0,sign=1; char ch=getchar();
24 while(ch<‘0‘||ch>‘9‘) {if (ch==‘-‘) sign=-1; ch=getchar();}
25 while(ch>=‘0‘&&ch<=‘9‘) {v=v*10+ch-‘0‘; ch=getchar();}
26 return v*sign;
27 }
28 typedef long long LL;
29 const int N=5000010,INF=~0u>>2;
30 /*******************tamplate********************/
31 LL n,sa,sb,sc,sd,a[3],MOD;
32 inline LL Fx(int x){
33 return (sa*x%MOD *x%MOD*x%MOD+sb*x%MOD*x%MOD+sc*x%MOD+sd)%MOD;
34 }
35
36 int main(){
37 #ifndef ONLINE_JUDGE
38 freopen("D.in","r",stdin);
39 // freopen("output.txt","w",stdout);
40 #endif
41 n=getint(); sa=getint(); sb=getint(); sc=getint();
42 sd=getint(); a[1]=getint(); MOD=getint();
43 LL mx=a[1],ans=0;
44 F(i,2,n){
45 a[i%3]=(Fx(a[(i-1)%3])+Fx(a[(i-2)%3]))%MOD;
46 ans=max(mx-a[i%3],ans);
47 mx=max(mx,a[i%3]);
48 }
49 printf("%d\n",(ans+1)/2);
50 return 0;
51 }