#include <bits/stdc++.h>
using namespace std;
const int INF = (1 << 30) - 1;
const long long mod = 1e9 + 9;
map<pair<int, int>, int> mp;
int m;
int v[100010][2];
int vis[100010];
int ans[100010], anscnt;
int up[100010][3];
int down[100010][3];
priority_queue<int> qmax;
priority_queue<int, vector<int>, greater<int> > qmin;
bool check(int a) {
  for (int i = 0; i < 3; ++i)
    if (up[a][i]) {
      int id = up[a][i], cnt = 0;
      for (int j = 0; j < 3; ++j) cnt += !down[id][j];
      if (cnt == 2) return false;
    }
  return true;
}
bool Check(int a, int x) {
  for (int i = 0; i < 3; ++i)
    if (up[a][i]) {
      int id = up[a][i], cnt = 0;
      if (id == x) continue;
      for (int j = 0; j < 3; ++j) cnt += !down[id][j];
      if (cnt == 2) return false;
    }
  return true;
}
void Remove(int a) {
  int now;
  for (int i = 0; i < 3; ++i)
    if (up[a][i]) {
      int id = up[a][i];
      for (int j = 2; j >= 0; --j)
        if (down[id][j]) {
          now = down[id][j];
          if (now == a) down[id][j] = 0;
        }
    }
  for (int i = 0; i < 3; ++i)
    if (down[a][i]) {
      int id = down[a][i];
      for (int j = 2; j >= 0; --j)
        if (up[id][j]) {
          now = up[id][j];
          if (now == a) up[id][j] = 0;
        }
    }
}
void Topo() {
  anscnt = 0;
  for (int i = 1; i <= m; ++i)
    if (check(i)) {
      qmax.push(i);
      qmin.push(i);
    }
  int cur = 1;
  while (1) {
    int x;
    if (cur % 2) {
      if (qmax.empty()) break;
      x = qmax.top();
      qmax.pop();
      if (vis[x]) continue;
    } else {
      if (qmin.empty()) break;
      x = qmin.top();
      qmin.pop();
      if (vis[x]) continue;
    }
    if (check(x) == false) continue;
    cur++;
    vis[x] = 1;
    ans[++anscnt] = x;
    for (int i = 0; i < 3; ++i)
      if (down[x][i]) {
        int id = down[x][i];
        if (Check(id, x)) {
          qmax.push(id);
          qmin.push(id);
        }
      }
    Remove(x);
  }
}
int main() {
  scanf("%d", &m);
  for (int i = 1; i <= (m); ++i) {
    scanf("%d%d", &v[i][0], &v[i][1]);
    mp[make_pair(v[i][0], v[i][1])] = i;
  }
  for (int i = 1; i <= (m); ++i) {
    int id, a = v[i][0], b = v[i][1];
    if (mp.find(make_pair(a - 1, b - 1)) != mp.end())
      down[i][0] = mp[make_pair(a - 1, b - 1)];
    if (mp.find(make_pair(a, b - 1)) != mp.end())
      down[i][1] = mp[make_pair(a, b - 1)];
    if (mp.find(make_pair(a + 1, b - 1)) != mp.end())
      down[i][2] = mp[make_pair(a + 1, b - 1)];
    if (mp.find(make_pair(a - 1, b + 1)) != mp.end())
      up[i][0] = mp[make_pair(a - 1, b + 1)];
    if (mp.find(make_pair(a, b + 1)) != mp.end())
      up[i][1] = mp[make_pair(a, b + 1)];
    if (mp.find(make_pair(a + 1, b + 1)) != mp.end())
      up[i][2] = mp[make_pair(a + 1, b + 1)];
  }
  Topo();
  long long rlt = 0;
  for (int i = 1; i <= anscnt; ++i) rlt = (rlt * m % mod + ans[i] - 1) % mod;
  printf("%I64d\n", rlt);
  return 0;
}

### Prompt

In Cpp, your task is to solve the following problem:

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 109 + 9.

Input

The first line contains number m (2 ≤ m ≤ 105).

The following m lines contain the coordinates of the cubes xi, yi ( - 109 ≤ xi ≤ 109, 0 ≤ yi ≤ 109) 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

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = (1 << 30) - 1;
const long long mod = 1e9 + 9;
map<pair<int, int>, int> mp;
int m;
int v[100010][2];
int vis[100010];
int ans[100010], anscnt;
int up[100010][3];
int down[100010][3];
priority_queue<int> qmax;
priority_queue<int, vector<int>, greater<int> > qmin;
bool check(int a) {
  for (int i = 0; i < 3; ++i)
    if (up[a][i]) {
      int id = up[a][i], cnt = 0;
      for (int j = 0; j < 3; ++j) cnt += !down[id][j];
      if (cnt == 2) return false;
    }
  return true;
}
bool Check(int a, int x) {
  for (int i = 0; i < 3; ++i)
    if (up[a][i]) {
      int id = up[a][i], cnt = 0;
      if (id == x) continue;
      for (int j = 0; j < 3; ++j) cnt += !down[id][j];
      if (cnt == 2) return false;
    }
  return true;
}
void Remove(int a) {
  int now;
  for (int i = 0; i < 3; ++i)
    if (up[a][i]) {
      int id = up[a][i];
      for (int j = 2; j >= 0; --j)
        if (down[id][j]) {
          now = down[id][j];
          if (now == a) down[id][j] = 0;
        }
    }
  for (int i = 0; i < 3; ++i)
    if (down[a][i]) {
      int id = down[a][i];
      for (int j = 2; j >= 0; --j)
        if (up[id][j]) {
          now = up[id][j];
          if (now == a) up[id][j] = 0;
        }
    }
}
void Topo() {
  anscnt = 0;
  for (int i = 1; i <= m; ++i)
    if (check(i)) {
      qmax.push(i);
      qmin.push(i);
    }
  int cur = 1;
  while (1) {
    int x;
    if (cur % 2) {
      if (qmax.empty()) break;
      x = qmax.top();
      qmax.pop();
      if (vis[x]) continue;
    } else {
      if (qmin.empty()) break;
      x = qmin.top();
      qmin.pop();
      if (vis[x]) continue;
    }
    if (check(x) == false) continue;
    cur++;
    vis[x] = 1;
    ans[++anscnt] = x;
    for (int i = 0; i < 3; ++i)
      if (down[x][i]) {
        int id = down[x][i];
        if (Check(id, x)) {
          qmax.push(id);
          qmin.push(id);
        }
      }
    Remove(x);
  }
}
int main() {
  scanf("%d", &m);
  for (int i = 1; i <= (m); ++i) {
    scanf("%d%d", &v[i][0], &v[i][1]);
    mp[make_pair(v[i][0], v[i][1])] = i;
  }
  for (int i = 1; i <= (m); ++i) {
    int id, a = v[i][0], b = v[i][1];
    if (mp.find(make_pair(a - 1, b - 1)) != mp.end())
      down[i][0] = mp[make_pair(a - 1, b - 1)];
    if (mp.find(make_pair(a, b - 1)) != mp.end())
      down[i][1] = mp[make_pair(a, b - 1)];
    if (mp.find(make_pair(a + 1, b - 1)) != mp.end())
      down[i][2] = mp[make_pair(a + 1, b - 1)];
    if (mp.find(make_pair(a - 1, b + 1)) != mp.end())
      up[i][0] = mp[make_pair(a - 1, b + 1)];
    if (mp.find(make_pair(a, b + 1)) != mp.end())
      up[i][1] = mp[make_pair(a, b + 1)];
    if (mp.find(make_pair(a + 1, b + 1)) != mp.end())
      up[i][2] = mp[make_pair(a + 1, b + 1)];
  }
  Topo();
  long long rlt = 0;
  for (int i = 1; i <= anscnt; ++i) rlt = (rlt * m % mod + ans[i] - 1) % mod;
  printf("%I64d\n", rlt);
  return 0;
}

```