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++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;
#define pii pair <int , int>
#define F first
#define S second
#define X first
#define V second
const int N = 2e5 + 5, inf = 1e9 + 1, MOD = 1e9 + 7;
int dp[N], ps[N], a[N], b[N], mn[N], seg[N<<2], n;
pii P[N];

int sum(int a, int b) {return a + b - (a + b >= MOD ? MOD : 0);}
void Set(int p, int val, int L = 1, int R = n + 1, int id = 1) {
    if (R - L == 1) {seg[id] = val; return ;}
    int mid = (L+R)>>1;
    if (p < mid) Set(p, val, L, mid, id<<1);
    else Set(p, val, mid, R, id<<1|1);
    seg[id] = max(seg[id<<1], seg[id<<1|1]);
    return ;
}
int Get(int l, int r, int L = 1, int R = n + 1, int id = 1) {
    if (r <= L || R <= l) return 0;
    if (l <= L && R <= r) return seg[id];
    int mid = (L+R)>>1;
    return max(Get(l, r, L, mid, id<<1), Get(l, r, mid, R, id<<1|1));
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> P[i].X >> P[i].V;
    sort(P + 1, P + n + 1);
    int* mx = a + 1;
    for (int i = 1; i <= n; i++) mx[i] = max(mx[i - 1], P[i].V);
    mn[n + 1] = inf;
    for (int i = n; i >= 1; i--) mn[i] = min(mn[i + 1], P[i].V);
    int* ps = b + 1;
    ps[0] = dp[0] = 1;
    priority_queue <pii> pq;
    for (int i = 1; i <= n; i++) {
        while (!pq.empty() && pq.top().F > mn[i]) Set(pq.top().S, 0), pq.pop();
        int dw = -1, up = i - 1;
        while (up - dw > 1) {
            int md = (up+dw)>>1;
            if (mx[md] >= Get(md + 1, i)) up = md;
            else dw = md;
        }
        dp[i] = sum(ps[i - 1], MOD - ps[up - 1]);
        ps[i] = sum(ps[i - 1], dp[i]);
        Set(i, P[i].V), pq.push({P[i].V, i});
    }
    int ans = 0, Max = 0;
    for (int i = n; i >= 1; i--) {
        if (mx[i] >= Max) ans = sum(ans, dp[i]);
        Max = max(Max, P[i].V);
    }
    cout << ans << "\n";

    return 0;
}