#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 TreeColoring { public:
	long long color(int N, int Q, int startSeed, int threshold, int maxDist)
	{
		vector<int> parent(N-1), distance(N-1), queryType(Q), queryNode(Q);
		{
			int curValue = startSeed;
			function<int()> genNextRandom = [&]() {
				curValue = (curValue * 1999 + 17) % 1000003;
				return curValue;
			};
			for(int i=0; i<N-1; i++) {
				distance[i] = genNextRandom() % maxDist;
				parent[i] = genNextRandom();
				if (parent[i] < threshold)
					parent[i] = i;
				else
					parent[i] = parent[i] % (i + 1);
			}
			for (int i=0; i<Q; i++) {
				queryType[i] = genNextRandom() % 2 + 1;
				queryNode[i] = genNextRandom() % N;
			}
		}

		// Doubled parents for LCA and distance to the root
		const int INVALID = -1;
		vector<vector<int>> p(N, vector<int>(17, INVALID));
		vector<int> d(N);
		for(int v=1; v<N; ++v) {
			p[v][1] = parent[v-1];
			d[v] = d[p[v][1]] + distance[v-1];
		}
		for(int d=2,k=2; k<17; d*=2,k++)
			for(int v=0; v<N; ++v) {
				int u = p[v][k-1];
				if(u != INVALID)
					p[v][k] = p[u][k-1];
			}

		return -1;
	}
};

// 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(_, TreeColoring().color(N, Q, startSeed, threshold, maxDist));}
int main(){

CASE(0)
	int N = 4; 
	int Q = 6; 
	int startSeed = 15; 
	int threshold = 2; 
	int maxDist = 5; 
	long long _ = 7LL; 
END
CASE(1)
	int N = 4; 
	int Q = 5; 
	int startSeed = 2; 
	int threshold = 9; 
	int maxDist = 10; 
	long long _ = 30LL; 
END
CASE(2)
	int N = 8; 
	int Q = 8; 
	int startSeed = 3; 
	int threshold = 5; 
	int maxDist = 7; 
	long long _ = 6LL; 
END
CASE(3)
	int N = 14750; 
	int Q = 50; 
	int startSeed = 29750; 
	int threshold = 1157; 
	int maxDist = 21610; 
	long long _ = 2537640LL; 
END
CASE(4)
	int N = 100000; 
	int Q = 100000; 
	int startSeed = 123456; 
	int threshold = 500000; 
	int maxDist = 474747; 
	long long _ = 726915029831LL; 
END
CASE(5)
	int N = 100000; 
	int Q = 100000; 
	int startSeed = 654321; 
	int threshold = 1000003; 
	int maxDist = 1000003; 
	long long _ = 562600687570528LL; 
END
/*
CASE(6)
	int N = ; 
	int Q = ; 
	int startSeed = ; 
	int threshold = ; 
	int maxDist = ; 
	long long _ = LL; 
END
CASE(7)
	int N = ; 
	int Q = ; 
	int startSeed = ; 
	int threshold = ; 
	int maxDist = ; 
	long long _ = LL; 
END
*/
}
// END CUT HERE
