Problem:
Score : 1200 points

Problem Statement

Takahashi is an expert of Clone Jutsu, a secret art that creates copies of his body.

On a number line, there are N copies of Takahashi, numbered 1 through N.
The i-th copy is located at position X_i and starts walking with velocity V_i in the positive direction at time 0.

Kenus is a master of Transformation Jutsu, and not only can he change into a person other than himself, but he can also transform another person into someone else.

Kenus can select some of the copies of Takahashi at time 0, and transform them into copies of Aoki, another Ninja.
The walking velocity of a copy does not change when it transforms.
From then on, whenever a copy of Takahashi and a copy of Aoki are at the same coordinate, that copy of Takahashi transforms into a copy of Aoki.

Among the 2^N ways to transform some copies of Takahashi into copies of Aoki at time 0, in how many ways will all the copies of Takahashi become copies of Aoki after a sufficiently long time?
Find the count modulo 10^9+7.

Constraints

1 ≤ N ≤ 200000

1 ≤ X_i,V_i ≤ 10^9(1 ≤ i ≤ N)

X_i and V_i are integers.

All X_i are distinct.

All V_i are distinct.

Input

The input is given from Standard Input in the following format:

N
X_1 V_1
:
X_N V_N

Output

Print the number of the ways that cause all the copies of Takahashi to turn into copies of Aoki after a sufficiently long time, modulo 10^9+7.

Sample Input 1

3
2 5
6 1
3 7

Sample Output 1

6

All copies of Takahashi will turn into copies of Aoki after a sufficiently long time if Kenus transforms one of the following sets of copies of Takahashi into copies of Aoki: (2), (3), (1,2), (1,3), (2,3) and (1,2,3).

Sample Input 2

4
3 7
2 9
8 16
10 8

Sample Output 2

9

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include<bits/stdc++.h>
#define int long long
#define out(x) cerr << #x << " = " << x << "\n"
using namespace std;
typedef long long ll;
//by piano
template<typename tp> inline void read(tp &x) {
  x = 0;char c = getchar();bool f = 0;
  for(; c < '0' || c > '9'; f |= (c == '-'), c = getchar());
  for(; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar());
  if(f) x = -x;
}
const int N = 6e5 + 2333, inf = 1e16 + 233, mo = 1e9 + 7;
struct P {
  int x, v;
}p[N];
int n, v[N], a[N], M, L[N], R[N], b[N];
inline bool cmp_x(P a, P b) {return a.x < b.x;}
inline int Qmin(int s, int t, int ans = inf) {
  for(s += M - 1, t += M + 1; s ^ t ^ 1; s >>= 1, t >>= 1) {
    if(~s & 1) ans = min(ans, a[s ^ 1]);
    if(t & 1) ans = min(ans, a[t ^ 1]);
  }return ans;
}
inline int Qmax(int s, int t, int ans = 0) {
  for(s += M - 1, t += M + 1; s ^ t ^ 1; s >>= 1, t >>= 1) {
    if(~s & 1) ans = max(ans, b[s ^ 1]);
    if(t & 1) ans = max(ans, b[t ^ 1]);
  }return ans;
}
vector<int> vec[N];
int tr[N];
inline void U(int &x, int y) {for(x += y; tr[x] >= mo; tr[x] -= mo);}
inline void A(int u, int v) {for(; u <= n; u += u & -u) U(tr[u], v);}
inline int Q(int u, int ans = 0) {for(; u >= 1; u -= u & -u) U(ans, tr[u]); return ans;}									      
main() {
  read(n);
  for(int i = 1; i <= n; i ++)
    read(p[i].x), read(p[i].v);
  sort(p + 1, p + n + 1, cmp_x);
  for(int i = 1; i <= n; i ++)
    v[i] = p[i].v;
  sort(v + 1, v + n + 1);
  for(int i = 1; i <= n; i ++)
    p[i].v = lower_bound(v + 1, v + n + 1, p[i].v) - v;
  for(M = 1; M <= n + 1; M ++);
  memset(a, 38, sizeof(a));
  for(int i = 1; i <= n; i ++)
    a[i + M] = b[i + M] = p[i].v;
  for(int i = M; i; i --) {
    a[i] = min(a[i << 1], a[i << 1 | 1]);
    b[i] = max(b[i << 1], b[i << 1 | 1]);
  }
  for(int i = 1; i <= n; i ++) {
    L[i] = Qmin(i, n), R[i] = Qmax(1, i);
    vec[R[i]].push_back(L[i]);
    // cout << L[i] << " " << R[i] << " " << p[i].x << " " << p[i].v <<  "\n";
  }
  A(1, 1);
  for(int i = 1; i <= n; i ++) {
    for(vector<int> ::iterator it = vec[i].begin(); it != vec[i].end(); ++ it)
      A(i, (Q(i) - Q(*it - 1) + mo) % mo);
  }
cout << Q(n) - Q(n - 1);
}