#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class CellScores { public:
	long long countScores(int N, int M, int K, int X0, int A, int B, int Y0, int C, int D) 
	{
		vector<int> X(M+K,X0%N), Y(M+K,Y0%N);
		for(int i=1; i<M+K; ++i)
			X[i] = (X[i-1]*A+B)%N,
			Y[i] = (Y[i-1]*C+D)%N;

		vector< vector<bool> > black(N, vector<bool>(N, false));
		for(int i=0; i<M; ++i)
			black[Y[i]][X[i]] = true;

		// how to compute them????
		vector< vector<int> > score_b(N, vector<int>(N, 0)); // # of gd rects around(Y,X) whose bottom line is y=Y+1
		vector< vector<int> > score_t(N, vector<int>(N, 0)); // top line is y=Y
		vector< vector<int> > score_l(N, vector<int>(N, 0)); // ...
		vector< vector<int> > score_r(N, vector<int>(N, 0));

		// O(1) score computation
		vector< vector<int> > score(N, vector<int>(N, 0));
		score[0][0] = score_t[0][0];
		for(int x=1; x<N; ++x)
			score[0][x] = score[0][x-1] - score_r[0][x-x] + score_l[0][x];
		for(int y=1; y<N; ++y)
			for(int x=0; x<N; ++x)
				score[y][x] = score[y-1][x] - score_b[y-1][x] + score_t[y][x];

		LL ss = 0;
		for(int i=M; i<M+K; ++i)
			ss += score[Y[i]][X[i]];
		return ss;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const long long& Expected, const long long& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, CellScores().countScores(N, M, K, X0, A, B, Y0, C, D));}
int main(){

CASE(0)
	int N = 10; 
	int M = 0; 
	int K = 1; 
	int X0 = 0; 
	int A = 1; 
	int B = 1; 
	int Y0 = 0; 
	int C = 1; 
	int D = 1; 
	long long _ = 100LL; 
END
CASE(1)
	int N = 10; 
	int M = 1; 
	int K = 1; 
	int X0 = 5; 
	int A = 1; 
	int B = 5; 
	int Y0 = 5; 
	int C = 1; 
	int D = 5; 
	long long _ = 75LL; 
END
CASE(2)
	int N = 7; 
	int M = 4; 
	int K = 3; 
	int X0 = 0; 
	int A = 1; 
	int B = 1; 
	int Y0 = 0; 
	int C = 1; 
	int D = 1; 
	long long _ = 194LL; 
END
CASE(3)
	int N = 23; 
	int M = 10; 
	int K = 30; 
	int X0 = 26; 
	int A = 48; 
	int B = 76; 
	int Y0 = 231; 
	int C = 463; 
	int D = 707; 
	long long _ = 8088LL; 
END
CASE(4)
	int N = 211; 
	int M = 30; 
	int K = 12; 
	int X0 = 3; 
	int A = 35; 
	int B = 82; 
	int Y0 = 0; 
	int C = 43; 
	int D = 15; 
	long long _ = 18196443LL; 
END
CASE(5)
	int N = 3; 
	int M = 0; 
	int K = 100; 
	int X0 = 0; 
	int A = 0; 
	int B = 0; 
	int Y0 = 0; 
	int C = 0; 
	int D = 0; 
	long long _ = 900LL; 
END

}
// END CUT HERE
