Once Vasya and Petya assembled a figure of m cubes, each of them is associated with a number between 0 and m - 1 (inclusive, each number appeared exactly once). Let's consider a coordinate system such that the OX is the ground, and the OY is directed upwards. Each cube is associated with the coordinates of its lower left corner, these coordinates are integers for each cube.

The figure turned out to be stable. This means that for any cube that is not on the ground, there is at least one cube under it such that those two cubes touch by a side or a corner. More formally, this means that for the cube with coordinates (x, y) either y = 0, or there is a cube with coordinates (x - 1, y - 1), (x, y - 1) or (x + 1, y - 1).

Now the boys want to disassemble the figure and put all the cubes in a row. In one step the cube is removed from the figure and being put to the right of the blocks that have already been laid. The guys remove the cubes in such order that the figure remains stable. To make the process more interesting, the guys decided to play the following game. The guys take out the cubes from the figure in turns. It is easy to see that after the figure is disassembled, the integers written on the cubes form a number, written in the m-ary positional numerical system (possibly, with a leading zero). Vasya wants the resulting number to be maximum possible, and Petya, on the contrary, tries to make it as small as possible. Vasya starts the game.

Your task is to determine what number is formed after the figure is disassembled, if the boys play optimally. Determine the remainder of the answer modulo 10^9 + 9.


-----Input-----

The first line contains number m (2 ≤ m ≤ 10^5).

The following m lines contain the coordinates of the cubes x_{i}, y_{i} ( - 10^9 ≤ x_{i} ≤ 10^9, 0 ≤ y_{i} ≤ 10^9) in ascending order of numbers written on them. It is guaranteed that the original figure is stable.

No two cubes occupy the same place.


-----Output-----

In the only line print the answer to the problem.


-----Examples-----
Input
3
2 1
1 0
0 1

Output
19

Input
5
0 0
0 1
0 2
0 3
0 4

Output
2930

Source: codeforces | Difficulty: HARD
Tags: Data structures

Implementation

Greedy algorithms


Solutions:
import heapq

def coor_neighbor(coor, dxs, dys):
	(x, y) = coor
	for dx in dxs:
		for dy in dys:
			yield (x + dx, y + dy)

def coor_bottoms(coor):
	return coor_neighbor(coor, (-1, 0, 1), (-1,))

def coor_tops(coor):
	return coor_neighbor(coor, (-1, 0, 1), (1,))

def coor_sibs(coor):
	return coor_neighbor(coor, (-2, -1, 1, 2), (0,))

class Figure:

	def __init__(self, coors):
		self._coors = dict()
		self._stables_min = []
		self._stables_max = []
		self._pushed = set()
		self._dropped = set()
		cubes = dict()
		self._bots = dict()
		self._tops = dict()
		for (idx, coor) in enumerate(coors):
			cubes[coor] = idx
			self._coors[idx] = coor
			self._bots[idx] = set()
			self._tops[idx] = set()
		coor_set = set(coors)
		for (idx, coor) in enumerate(coors):
			for bottom in coor_bottoms(coor):
				if bottom in coor_set:
					self._bots[idx].add(cubes[bottom])
			for top in coor_tops(coor):
				if top in coor_set:
					self._tops[idx].add(cubes[top])
		for idx in self._coors:
			if self.isdroppable(idx):
				self.push(idx)

	def sibs(self, idx):
		for top_idx in self._tops[idx]:
			for sib_idx in self._bots[top_idx]:
				if sib_idx not in self._dropped:
					yield sib_idx

	def bottom_count(self, idx):
		return len(self._bots[idx])

	def isdroppable(self, idx):
		return all((len(self._bots[top_idx]) > 1 for top_idx in self._tops[idx]))

	def push(self, idx):
		if idx not in self._pushed:
			heapq.heappush(self._stables_min, idx)
			heapq.heappush(self._stables_max, -idx)
			self._pushed.add(idx)

	def unpush(self, idx):
		if idx in self._pushed:
			self._pushed.remove(idx)

	def drop(self, idx):
		if idx not in self._pushed:
			return False
		self._pushed.remove(idx)
		self._dropped.add(idx)
		for bot_idx in self._bots[idx]:
			self._tops[bot_idx].remove(idx)
		for top_idx in self._tops[idx]:
			self._bots[top_idx].remove(idx)
		coor = self._coors[idx]
		for bot_idx in self._bots[idx]:
			if self.isdroppable(bot_idx):
				self.push(bot_idx)
		for sib_idx in self.sibs(idx):
			if not self.isdroppable(sib_idx):
				self.unpush(sib_idx)
		return True

	def drop_min(self):
		while True:
			if not self._stables_min:
				return None
			min_idx = heapq.heappop(self._stables_min)
			if self.drop(min_idx):
				return min_idx

	def drop_max(self):
		while True:
			if not self._stables_max:
				return None
			max_idx = -heapq.heappop(self._stables_max)
			if self.drop(max_idx):
				return max_idx

	def __bool__(self):
		return len(self._coors) != len(self._dropped)

def input_tuple():
	return tuple(map(int, input().split()))

def result_add(result, base, num):
	return (result * base + num) % (10 ** 9 + 9)
N = int(input())
coors = [input_tuple() for _ in range(N)]
figure = Figure(coors)
result = 0
while True:
	if not figure:
		break
	result = result_add(result, N, figure.drop_max())
	if not figure:
		break
	result = result_add(result, N, figure.drop_min())
print(result)