#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i ) #define rep(i,n) REP(i,0,n) using ll = long long; const int inf=1e9+7; const ll longinf=1LL<<60 ; int main(){ int m,n,mx,ax,my,ay,mod; cin>>m>>n>>mx>>ax>>my>>ay>>mod; int B = mod-1; vector x(m), y(m); int nn = (1<<24)+1; vector cnt(nn); rep(i,m){ if(i>x[i]; else x[i]=(x[i-1]*mx+ax)&B; } rep(i,m){ if(i>y[i]; else y[i]=(y[i-1]*my+ay)&B; } rep(i,m){ cnt[x[i]]+=y[i]; } ll cx = x[m-1], cy = y[m-1]; rep(i,n-m){ cx=(cx*mx+ax)&B; cy=(cy*my+ay)&B; cnt[cx]+=cy; } x.clear();y.clear(); vectorisprime(nn,1); vector prime; for(int i=2;i0;--i){ cnt[i]+=cnt[i*p]; } } prime.clear(); isprime.clear(); return 0; vectora(n),b(n); rep(i,n){ if(i>a[i]; else a[i]=((a[i-1]*mx+ax+B)&B)+1; } rep(i,n){ if(i>b[i]; else b[i]=((b[i-1]*my+ay+B)&B)+1; } ll ans = 0; rep(i,n){ ll res = cnt[a[i]]; if((ll)a[i]*b[i]<=mod)res-=cnt[a[i]*b[i]]; if(i