#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 <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class XorSequence { public:
	long long getmax(int N, int sz, int A0, int A1, int P, int Q, int R)
	{
		vector<int> A(sz);
		A[0] = A0;
		A[1] = A1;
		for(int i=2; i<sz; i++)
			A[i] = int((A[i-2]*LL(P) + A[i-1]*LL(Q) + R) % N);

		int LN = 1;
		while((1<<LN) != N) ++LN;

		return solve(LN, A);
	}

	LL solve(int LN, const vector<int>& A)
	{
		LL total = 0;

		vector<vector<int>> Sec(1, A);
		for(int Bit=LN-1; Bit>=0; --Bit) {
			vector<vector<int>> Next_Sec;

			LL p01=0, p10=0;
			for(vector<int>& A: Sec) {
				vector<int> A0, A1;
				LL n0=0, n1=0;
				for(int i=0; i<A.size(); ++i) {
					if((1<<Bit)&A[i]) {
						p01 += n0;
						++n1;
						A1.push_back(A[i]);
					} else {
						p10 += n1;
						++n0;
						A0.push_back(A[i]);
					}
				}
				if(Bit) {
					if(!A0.empty())Next_Sec.emplace_back(std::move(A0));
					if(!A1.empty())Next_Sec.emplace_back(std::move(A1));
				}
			}
			Sec.swap(Next_Sec);

			total += max(p01, p10);
		}
		return total;
	}
};

// 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(_, XorSequence().getmax(N, sz, A0, A1, P, Q, R));}
int main(){

CASE(0)
	int N = 4; 
	int sz = 6; 
	int A0 = 3; 
	int A1 = 2; 
	int P = 0; 
	int Q = 1; 
	int R = 3; 
	long long _ = 8LL; 
END
CASE(1)
	int N = 8; 
	int sz = 8; 
	int A0 = 2; 
	int A1 = 5; 
	int P = 3; 
	int Q = 1; 
	int R = 4; 
	long long _ = 13LL; 
END
CASE(2)
	int N = 8; 
	int sz = 7; 
	int A0 = 3; 
	int A1 = 0; 
	int P = 1; 
	int Q = 2; 
	int R = 4; 
	long long _ = 12LL; 
END
CASE(3)
	int N = 32; 
	int sz = 15; 
	int A0 = 7; 
	int A1 = 9; 
	int P = 11; 
	int Q = 2; 
	int R = 1; 
	long long _ = 60LL; 
END
CASE(4)
	int N = 131072; 
	int sz = 131072; 
	int A0 = 7; 
	int A1 = 7; 
	int P = 1; 
	int Q = 0; 
	int R = 0; 
	long long _ = 0LL; 
END
CASE(5)
	int N = 131072; 
	int sz = 131070; 
	int A0 = 411; 
	int A1 = 415; 
	int P = 398; 
	int Q = 463; 
	int R = 9191; 
	long long _ = 4302679760LL; 
END
/*
CASE(6)
	int N = ; 
	int sz = ; 
	int A0 = ; 
	int A1 = ; 
	int P = ; 
	int Q = ; 
	int R = ; 
	long long _ = LL; 
END
CASE(7)
	int N = ; 
	int sz = ; 
	int A0 = ; 
	int A1 = ; 
	int P = ; 
	int Q = ; 
	int R = ; 
	long long _ = LL; 
END
*/
}
// END CUT HERE
