所以说这种题目出出来到底有什么意义???搞笑的吗。QAQ
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define fo(i,a,b) for(ll i=a;i<=b;i++)
#define fd(i,a,b) for(ll i=a;i>=b;i--)
using namespace std;
typedef long long ll;
ll a,b,c,d,e,f,g,h;
ll m,n;
struct node
{
ll u,w;
}q[2000000];
bool cmp(node x,node y)
{
if (x.u==y.u)return x.w<y.w;
else return x.u>y.u;
}
int main()
{
scanf("%lld%lld%lld%lld%lld%lld%lld%lld%lld%lld",&n,&a,&b,&c,&d,&e,&f,&g,&h,&m);
fo(i,0,3*n-1)
{
ll w1=i%d;
ll w2=(w1*w1)%d;
ll w5=((w2*w2)%d*w1)%d;
q[i].w=(a*w5+b*w2+c)%d;
ll u1=i%h;
ll u3=((u1*u1)%h*u1%h);
ll u5=((u3*u1)%h*u1%h);
q[i].u=(e*u5+f*u3+g)%h;
}
sort(q,q+3*n,cmp);
ll sum=0;
fo(i,0,n-1)sum=(sum+q[i].w)%m;
printf("%lld\n",sum);
}