#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 FindingKids { public:
	long long getSum(int n, int q, int A_, const int B, const int C)
	{
		set<int> pos_set;
		vector<int> pos(n), dir(n);

		const int M = 1000000007;
		LL A = A_;
		for(int i=0; i<n; ++i) {
		    A = (A * B + C) % M;
			int p = int(A % (M - n + i + 1));
		    if(pos_set.count(p))
				p = M - n + i;
		    pos[i] = p;
			pos_set.insert(p);
		    if(p%2 == 0)
		        dir[i] = +1;
		    else
			    dir[i] = -1;
		}

		vector<int> kid(q), time(q);
		for(int i=0; i<q; ++i) {
		    A = (A * B + C) % M;
		    kid[i] = int(A%n);
		    A = (A * B + C) % M;
			time[i] = int(A);
		}

		return solve(n, pos, dir, q, kid, time);
	}

	LL solve(
		const int N, const vector<int>& P, const vector<int>& D,
		const int Q, const vector<int>& K, const vector<int>& T) {

		vector<int> kid_id_to_pos_order(N); {
			vector<int> kids_sorted_by_pos(N);
			for(int i=0; i<N; ++i) kids_sorted_by_pos[i] = i;
			sort(kids_sorted_by_pos.begin(), kids_sorted_by_pos.end(), [&](int a, int b){
				return P[a] < P[b];
			});
			for(int i=0; i<N; ++i)
				kid_id_to_pos_order[kids_sorted_by_pos[i]] = i;
		}

		vector<LL> move_kids, stay_kids;
		for(int i=0; i<N; ++i)
			(D[i] == +1 ? move_kids : stay_kids).emplace_back(P[i]);

		sort(move_kids.begin(), move_kids.end());
		sort(stay_kids.begin(), stay_kids.end());

		LL total = 0;
		for(int i=0; i<Q; ++i) {
			LL p = find_kth_left(kid_id_to_pos_order[K[i]], stay_kids, move_kids, 2*T[i]);
			total += abs(p - T[i]);
		}
		return total;
	}

	// Find the $k$-th left kid in {stay_kids} U {move_kids += t}.
	LL find_kth_left(const int k, const vector<LL>& stay_kids, const vector<LL>& move_kids, const LL t) {
		if(stay_kids.empty()) return move_kids[k]+t;
		if(move_kids.empty()) return stay_kids[k];
		if(stay_kids.back() < move_kids.front()+t) {
			if(k < stay_kids.size())
				return stay_kids[k];
			else
				return move_kids[k-stay_kids.size()]+t;
		}
		if(move_kids.back()+t < stay_kids.front()) {
			if(k < move_kids.size())
				return move_kids[k]+t;
			else
				return stay_kids[k-move_kids.size()]+t;
		}

		int L=0, R=stay_kids.size();
		while(R-L>1) {
			int C = (L+R)/2;
			int left_in_stay = C;
			int left_in_move = lower_bound(move_kids.begin(), move_kids.end(), stay_kids[C]-t) - move_kids.begin();
			int left = left_in_stay + left_in_move;
			if(left == k)
				return stay_kids[C];
			(left >= k ? R : L) = C;
		}

		L=0, R=move_kids.size();
		while(R-L>1) {
			int C = (L+R)/2;
			int left_in_move = C;
			int left_in_stay = lower_bound(stay_kids.begin(), stay_kids.end(), move_kids[C]+t) - stay_kids.begin();
			int left = left_in_stay + left_in_move;
			if(left == k)
				return move_kids[C]+t;
			(left >= k ? R : L) = C;
		}

		vector<LL> now = stay_kids;
		for(LL p: move_kids) now.push_back(p + t);
		sort(now.begin(), now.end());
		return now[k];
	}
};

// 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(_, FindingKids().getSum(n, q, A, B, C));}
int main(){

CASE(0)
	int n = 5; 
	int q = 2; 
	int A = 0; 
	int B = 1; 
	int C = 1; 
	long long _ = 15LL; 
END
CASE(1)
	int n = 5; 
	int q = 4; 
	int A = 3; 
	int B = 2; 
	int C = 1; 
	long long _ = 43376LL; 
END
CASE(2)
	int n = 200000; 
	int q = 200000; 
	int A = 12345; 
	int B = 67890; 
	int C = 111213141; 
	long long _ = 133378408428237LL; 
END
/*
CASE(3)
	int n = ; 
	int q = ; 
	int A = ; 
	int B = ; 
	int C = ; 
	long long _ = LL; 
END
CASE(4)
	int n = ; 
	int q = ; 
	int A = ; 
	int B = ; 
	int C = ; 
	long long _ = LL; 
END
*/
}
// END CUT HERE
