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>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
typedef long long LL;
int N;
LL X[200001];
LL V[200001];
//各頂点に対して区間を作る
LL L[200001];
LL R[200001];

map<LL,int>v;
map<LL,int>v2;

const int MAX_N = 1 << 20;

//セグメント木を持つグローバル配列
LL n,dat[2*MAX_N-1],dat_ma[2*MAX_N-1],dat_sa[2*MAX_N-1];

//初期化
void init(LL n_){
   //簡単のために要素数を2のべき乗に
   n=1;
   while(n<n_) n*=2;
   //全ての値をINT_MAXに
   for(int i=0;i<2*n-1;i++){
       dat[i]=INT_MAX;
       dat_ma[i]=-100;
       dat_sa[i]=0;
   }
}

//k番目の値(0-indexed)をaに変更
void update(int k,int a){
   //葉の節点
   k+=n-1;
  dat[k]=a;
  dat_ma[k]=a;
  dat_sa[k]+=a;
  //登りながら更新
  while(k>0){
     k=(k-1)/2;
     dat[k] = min(dat[k*2+1],dat[k*2+2]);
     dat_ma[k]=max(dat_ma[k*2+1],dat_ma[k*2+2]);
     dat_sa[k]=dat_sa[k*2+1]+dat_sa[k*2+2];
  }
}
//[a,b)の最小値を求める
//後ろの方の引数は、計算の簡単のための引数
//kは節点の番号、l,rはその接点が[l,r)に対応していることを表す
//従って外からはquery(a,b,0,0,nと呼ぶ)
LL query(int a,int b,int k,int l,int r){
    //[a,b)と[l,r)が交差しなければINT_MAX
    if(r<=a||b<=l) return INT_MAX;
    //[a,b)が[l,r)を完全に含んでいればこの接点の値
    if(a<=l&&r<=b) return dat[k];
    else{
       //そうでなければ、2つの子の最小値
       int vl =query(a,b,k*2+1,l,(l+r)/2);
       int vr =query(a,b,k*2+2,(l+r)/2,r);
       return min(vl,vr);
    }
}

//[a,b)の最大値を求める
//後ろの方の引数は、計算の簡単のための引数
//kは節点の番号、l,rはその接点が[l,r)に対応していることを表す
//従って外からはquery(a,b,0,0,nと呼ぶ)
LL query_ma(int a,int b,int k,int l,int r){
    //[a,b)と[l,r)が交差しなければINT_MAX
    if(r<=a||b<=l) return -INT_MAX;
    //[a,b)が[l,r)を完全に含んでいればこの接点の値
    if(a<=l&&r<=b) return dat_ma[k];
    else{
       //そうでなければ、2つの子の最大値
       int vl =query_ma(a,b,k*2+1,l,(l+r)/2);
       int vr =query_ma(a,b,k*2+2,(l+r)/2,r);
       return max(vl,vr);
    }
}

//[a,b)の最大値を求める
//後ろの方の引数は、計算の簡単のための引数
//kは節点の番号、l,rはその接点が[l,r)に対応していることを表す
//従って外からはquery(a,b,0,0,nと呼ぶ)
int query_sa(int a,int b,int k,int l,int r){
    //[a,b)と[l,r)が交差しなければINT_MAX
    if(r<=a||b<=l) return 0;
    //[a,b)が[l,r)を完全に含んでいればこの接点の値
    if(a<=l&&r<=b) return dat_sa[k];
    else{
       //そうでなければ、2つの子の最大値
       int vl =query_sa(a,b,k*2+1,l,(l+r)/2);
       int vr =query_sa(a,b,k*2+2,(l+r)/2,r);
       return vl+vr;
    }
}




int main(){
    cin>>N;
    REP(i,N){
        cin>>X[i]>>V[i];
        v[X[i]]=0;
        v2[V[i]]=0;
    }
    int count=1;
    for(auto it=v.begin();it!=v.end();it++){
        it->second=count;
        count++;
    }
    count=1;
    for(auto it=v2.begin();it!=v2.end();it++){
        it->second=count;
        count++;
    }

    //セグ木の初期化
    init(N+10);
    //座圧してセグ木につっこむ
    REP(i,N){
       X[i]=v[X[i]];
       V[i]=v2[V[i]];
       update(X[i],V[i]);
    }
    //各クエリを取得
    REP(i,N){
       L[i]=query(X[i],N+10,0,0,n);
       R[i]=query_ma(0,X[i]+1,0,0,n);
    }
    
    init(N+10);
    update(1,1);
    REP(i,N){
        update(R[i],query_sa(L[i],n,0,0,n));
    }
    cout<<query_sa(v2.size(),v2.size()+1,0,0,n)<<endl;
    
    
    return(0);
}