// array: push_back in log n, by index in log n
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <utility>
using namespace std;

int random ()
{
	return (rand () << 15) ^ rand ();
}

struct Node;
typedef Node * PNode;
struct Node
{
	int value;
	int y;
	int size;
	PNode left;
	PNode right;

	Node (int value_)
	{
		value = value_;
		y = random ();
		size = 1;
		left = nullptr;
		right = nullptr;
	}

	void recalc ()
	{
		size = 1;
		if (left != nullptr) size += left -> size;
		if (right != nullptr) size += right -> size;
	}
};

// 40 = 15 left + 1 root + 24 right
// k = 20
// -> right, k = 20 - 15 - 1 = 4
pair <PNode, PNode> tsplitK (PNode t, int k)
{
	if (t == nullptr) return {nullptr, nullptr};
	int leftSize = (t -> left == nullptr) ? 0 : t -> left -> size;
	if (k - leftSize - 1 >= 0)
	{ // leftSize = 10, k = 80: right, k - leftSize - 1
		auto temp = tsplitK (t -> right, k - leftSize - 1);
		t -> right = temp.first;
		t -> recalc ();
		return {t, temp.second};
	}
	else
	{ // leftSize = 50, k = 25: left, k
		auto temp = tsplitK (t -> left, k);
		t -> left = temp.second;
		t -> recalc ();
		return {temp.first, t};
	}
}

PNode tmerge (PNode l, PNode r)
{
	if (l == nullptr) return r;
	if (r == nullptr) return l;
	if (l -> y > r -> y)
	{
		l -> right = tmerge (l -> right, r);
		l -> recalc ();
		return l;
	}
	else
	{
		r -> left = tmerge (l, r -> left);
		r -> recalc ();
		return r;
	}
}

void toutputrecur (PNode t)
{
	if (t == nullptr) return;
	cout << "(";
	toutputrecur (t -> left);
	cout << " " << t -> value << " ";
	toutputrecur (t -> right);
	cout << ")";
}

void toutput (PNode t)
{
	toutputrecur (t);
	cout << endl;
}

int kth (PNode & t, int k)
{
	auto temp = tsplitK (t, k);
	auto r = tsplitK (temp.second, 1);
	// temp.first | r.first | r.second
	int res = r.first -> value;
	temp.second = tmerge (r.first, r.second);
	t = tmerge (temp.first, temp.second);
	return res;
}

int main ()
{
/*
	int n = 10;
	PNode root = nullptr;
	for (int i = 1; i <= n; i++)
	{
		int value = (i * 7) % n;
		root = tmerge (root, new Node (value));
		toutput (root);
	}

	for (int i = n - 1; i >= 0; i--)
	{
		cout << kth (root, i) << endl;
	}
*/

	int n = 100000;
	PNode root = nullptr;
	for (int i = 1; i <= n; i++)
	{
		int value = (i * 317) % n;
		root = tmerge (root, new Node (value));
	}

	for (int i = n - 1; i >= 0; i--)
	{
		cout << kth (root, i) << endl;
	}

	return 0;
}
