1 #include<cstdio>
2 #include<iostream>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 using namespace std;
7 char ch; bool ok;
8 void read(int &x){
9 for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch==‘-‘) ok=1;
10 for (x=0;isdigit(ch);x=x*10+ch-‘0‘,ch=getchar());
11 if (ok) x=-x;
12 }
13 const int maxn=100005;
14 const int inf=0x3f3f3f3f;
15 const int MOD=19921228;
16 int add,first,mod,prod,tmp[4][maxn];
17 int n,idx,ans;
18 struct Data{
19 int x,id,op;
20 }list[maxn*3];
21 bool operator<(const Data &a,const Data &b){
22 if (a.x!=b.x) return a.x<b.x;
23 return a.op<b.op;
24 }
25 struct Splay{
26 #define ls son[x][0]
27 #define rs son[x][1]
28 int root,fa[maxn],son[maxn][2],siz[maxn],val[maxn];
29 void init(){
30 root=n+1,siz[n+1]=2,val[n+1]=inf,son[n+1][1]=n+2;
31 siz[n+2]=1,val[n+2]=-inf,fa[n+2]=n+1;
32 }
33 int which(int x){return son[fa[x]][1]==x;}
34 void updata(int x){siz[x]=siz[ls]+1+siz[rs];}
35 void rotate(int x){
36 int y=fa[x],z=fa[y],d=which(x),dd=which(y);
37 fa[son[x][d^1]]=y,son[y][d]=son[x][d^1],fa[x]=z;
38 if (z) son[z][dd]=x;
39 son[x][d^1]=y,fa[y]=x,updata(y);
40 }
41 void splay(int x){
42 while (fa[x]){
43 if (!fa[fa[x]]) rotate(x);
44 else if (which(fa[x])==which(x)) rotate(fa[x]),rotate(x);
45 else rotate(x),rotate(x);
46 }
47 updata(x),root=x;
48 }
49 void insert(int x,int v){
50 fa[x]=son[x][0]=son[x][1]=0,siz[x]=1,val[x]=v;
51 int f,t;
52 for (f=t=root;t;f=t,t=son[t][v<val[t]]);
53 fa[x]=f,son[f][v<val[f]]=x,splay(x);
54 }
55 int find_left(int x){for (;son[x][0];x=son[x][0]);return x;}
56 void _delete(int x){
57 splay(x);
58 int y=find_left(son[x][1]);
59 fa[ls]=fa[rs]=0,splay(y),fa[ls]=y,son[y][0]=ls,updata(y);
60 }
61 int find_kth(int x,int k){
62 if (!x) return 0;
63 if (siz[ls]>=k) return find_kth(ls,k);
64 if (siz[ls]+1==k) return x;
65 return find_kth(rs,k-siz[ls]-1);
66 }
67 int query(int k){
68 int x=find_kth(root,k+1);
69 if (1<=x&&x<=n) return val[x];
70 return 0;
71 }
72 }T;
73 int main(){
74 read(n);
75 for (int op=0;op<4;op++){
76 read(add),read(first),read(mod),read(prod);
77 tmp[op][1]=first%mod;
78 for (int i=2;i<=n;i++) tmp[op][i]=(1LL*tmp[op][i-1]*prod+add+i)%mod;
79 }
80 for (int i=1;i<=n;i++) if (tmp[2][i]>tmp[3][i]) swap(tmp[2][i],tmp[3][i]);
81 for (int i=1;i<=n;i++) list[++idx]=(Data){tmp[2][i],i,0};
82 for (int i=1;i<=n;i++) list[++idx]=(Data){tmp[0][i],i,1};
83 for (int i=1;i<=n;i++) list[++idx]=(Data){tmp[3][i],i,2};
84 sort(list+1,list+idx+1),T.init();
85 for (int i=1;i<=idx;i++){
86 int id=list[i].id,op=list[i].op;
87 if (op==0) T.insert(id,tmp[1][id]);
88 else if (op==1) ans+=T.query(id),ans%=MOD;
89 else if (op==2) T._delete(id);
90 }
91 printf("%d\n",ans);
92 return 0;
93 }