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: Wrong Answer)

#include <bits/stdc++.h>
#define db double
#define ls rt << 1
#define rs rt << 1 | 1
#define pb push_back
#define ll long long
#define mp make_pair
#define pii pair<int, int>
#define X first
#define Y second
#define pcc pair<char, char>
#define vi vector<int>
#define vl vector<ll>
#define rep(i, x, y) for(int i = x; i <= y; i ++)
#define rrep(i, x, y) for(int i = x; i >= y; i --)
#define eps 1e-9
#define all(x) (x).begin(), (x).end()
#define cmax(u,v) (u)<(v)?(u)=(v):0;
using namespace std;
inline int read()
{
	int x = 0, f = 1; char ch = getchar();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}

inline ll read1()
{
	ll x = 0, f = 1; char ch = getchar();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
using namespace std;

const int mod = 1e9 + 7;
const int N = 200010;
int f[N], L[N], R[N], n, q[N];
struct Node
{
    int x, v, id;
}a[N];

bool cmp1(Node a, Node b)
{
    return a.v < b.v;
}

bool cmp2(Node a, Node b)
{
    return a.x < b.x;
}
int main()
{
	n = read();
	rep(i, 1, n)
		a[i].x = read(), a[i].v = read();
	sort(a + 1, a + 1 + n, cmp1);
    rep(i, 1, n) a[i].id = i;
    sort(a + 1, a + 1 + n, cmp2);
    int Mx = 0, Mn = n + 1;
    rep(i, 1, n)
    {
        Mx = (a[i].id > Mx) ? a[i].id : Mx;
        R[i] = Mx;
    }
    rrep(i, n, 1)
    {
        Mn = (a[i].id < Mn) ? a[i].id : Mn;
        L[i] = Mn;
    }
    int sum = 0, l = 0, r = 0;
    L[0] = 1;
    R[0] = 0;
    f[0] = 1;
    q[r ++] = 0, sum = 1;
    n ++;
    L[n] = R[n] = n;
    rep(i, 1, n)
    {
        while (l < r && L[i] > R[q[l]] + 1)
            (sum -= f[q[l]]) %= mod, l ++;
        f[i] = sum;
        (sum += f[i]);
        q[r ++] = i;
    }
    printf("%d", f[n]);
    return 0;
}