#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 LimitedMemorySeries1 { public:
	long long getSum(int n, int x0, int a, int b, const vector <int>& query)
	{
		LL sum = 0;
		multiset<int> qq(query.begin(), query.end());

		const int BACKET = min(n, 250000);
		int prev_turn_largest = -1;
		int ptl_count = 0;

		int* pq = new int[BACKET+1];
		int pql = 0;

		for(int s=0; s<n; s+=BACKET) {
			pql = 0; // PQ keeping min BACKET elements

			int v = x0;
			for(int i=0; i<n; ++i) {
				if(v < prev_turn_largest) {}
				else if(v == prev_turn_largest && ptl_count>0) {ptl_count--;}
				else {
					pq[pql++]=v; push_heap(pq+0, pq+pql);
					if(pql>BACKET)
						pop_heap(pq+0, pq+pql), pql--; // remove largest
				}
				v = int((LL(v) * a + b) % 1000000007);
			}
			int cur_largest = -1;
			int cl_count = 0;

			for(int rank=s+pql-1; rank>=s; --rank) {
				int v = pq[0];
				pop_heap(pq+0, pq+pql), pql--;

				if(cur_largest < v) cur_largest = v, cl_count = 1;
				else if(cur_largest == v) cl_count++;

				if(qq.count(rank)) {
					sum += LL(v) * qq.count(rank);
					qq.erase(rank);
				}
			}

			prev_turn_largest = cur_largest;
			ptl_count = cl_count;
		}
		delete[] pq;
		return sum;
	}
};

// 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(_, LimitedMemorySeries1().getSum(n, x0, a, b, query));}
int main(){

CASE(0)
	int n = 5; 
	int x0 = 100; 
	int a = 1; 
	int b = 5; 
	int query_[] = {0,3};
	  vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); 
	long long _ = 215LL; 
END
CASE(1)
	int n = 5; 
	int x0 = 123456789; 
	int a = 987654321; 
	int b = 1000000006; 
	int query_[] = {0,1,2,3};
	  vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); 
	long long _ = 733028692LL; 
END
CASE(2)
	int n = 5000000; 
	int x0 = 482995837; 
	int a = 512849030; 
	int b = 120583724; 
	int query_[] = {4854010,3139503,1855546,219904,846357,2624639,891260,217467,4940091,4802760,2424821,424076,
 3848272,2062765,2922407,4850301,1279972,4929307,2035456,3562859,1749594,4089499,3797495,1013980,
 1650805,1245213,3020379,4661668,427170,1176815,292944,2435328,420809,4170355,2635197,3940607,
 4311718,2098572,4868054,30319,4588969,1460677,1335028,3921495,3621970,4459335,966000,3686977,
 2316560,1634961,2280624,1940395,3321546,3168811,1856547,3859093,4340475,1382782,3482928,2517843,
 185008,1135373,2821050,3260484,4821220,1700954,3243343,2183615,394339,2630121,1811267,1060542,
 3898314,892312,2015580,11161,4965095,2128470,2320933,1095881,3938450,1887098,975426,2098073,3300937,
 1145560,2894728,1026181,3259403,4509345,3610224,3584456,1877483,2665752,2243671,616205,504849,3068426,
 1471925,4144568};
	  vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); 
	long long _ = 49684994148LL; 
END
CASE(2)
	int n = 5000000; 
	int x0 = 482995837; 
	int a = 512849030; 
	int b = 120583724; 
	int query_[] = {5000000,4999999,4999998,4999997,4999996,4999995,4999994,4999993,4999992,4999991,4999990,4999989,4999988,4999987,4999986,4999985,4999984,4999983,4999982,4999981,4999980,4999979,4999978,4999977,4999976,4999975,4999974,4999973,4999972,4999971,4999970,4999969,4999968,4999967,4999966,4999965,4999964,4999963,4999962,4999961,4999960,4999959,4999958,4999957,4999956,4999955,4999954,4999953,4999952,4999951,4999950,4999949,4999948,4999947,4999946,4999945,4999944,4999943,4999942,4999941,4999940,4999939,4999938,4999937,4999936,4999935,4999934,4999933,4999932,4999931,4999930,4999929,4999928,4999927,4999926,4999925,4999924,4999923,4999922,4999921,4999920,4999919,4999918,4999917,4999916,4999915,4999914,4999913,4999912,4999911,4999910,4999909,4999908,4999907,4999906,4999905,4999904,4999903,4999902,4999901};
	  vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); 
	long long _ = -1LL; 
END
/*
CASE(3)
	int n = ; 
	int x0 = ; 
	int a = ; 
	int b = ; 
	int query_[] = ;
	  vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); 
	long long _ = LL; 
END
CASE(4)
	int n = ; 
	int x0 = ; 
	int a = ; 
	int b = ; 
	int query_[] = ;
	  vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); 
	long long _ = LL; 
END
*/
}
// END CUT HERE
