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 <set>
#include <map>
#include <queue>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <bitset>
#include <vector>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
typedef double db;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pa;
typedef unsigned int uint;
typedef unsigned long long ull;
#define w1 first
#define ls (x<<1)
#define w2 second
#define ins insert
#define rs (x<<1|1) 
#define mp make_pair
#define pb push_back
#define mid ((l+r)>>1)
#define sqr(x) ((x)*(x))
#define cle(x) ((x).clear())
#define lowbit(x) ((x)&(-x))
#define SZ(x) (int((x).size()))
#define ms(x,y) memset(x,y,sizeof (x))
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define rep2(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define per(i,a,b) for(int (i)=(a);(i)>=(b);(i)--)
#define Rep(p,x) for(int (p)=head[(x)];(p);(p)=nxt[(p)]) 
#define Rep2(p,x) for(int (p)=cur[(x)];(p);(p)=nxt[(p)])
template<class T>inline void read(T&num){
	num=0;T f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();
	num*=f;
}
inline int getgcd(int x,int y){if(!x)return y;return getgcd(y%x,x);}
inline int power(int x,int k,int p){int res=1;for(;k;k>>=1,x=(ll)x*x%p)if(k&1)res=(ll)res*x%p;return res;}
const double pi=acos(-1);	
inline void judge(){
	freopen("input.txt","r",stdin);
} 
//********************************head*************************************
const int maxn=2e5+5,mod=1e9+7;
int n;
int x[maxn],v[maxn],q[maxn],maxi[maxn],mini[maxn],c[maxn];
pa seg[maxn];
void M(int x,int num){
	for(;x<=n;x+=lowbit(x))c[x]=(c[x]+num)%mod;
}
int Q(int x){
	if(x<0)return 0;
	int res=1;
	for(;x;x-=lowbit(x))res=(res+c[x])%mod;
	return res;
}
bool cmp(int a,int b){
	return v[a]<v[b];
}
bool cmp2(pa a,pa b){
	return a.w2<b.w2;
}
int main(){
	read(n);
	rep(i,1,n)read(x[i]),read(v[i]);
	rep(i,1,n)q[i]=i;
	sort(q+1,q+n+1,cmp);
	rep(i,1,n)q[i]=x[q[i]];
	maxi[1]=q[1];rep(i,2,n)maxi[i]=max(maxi[i-1],q[i]);
	mini[n]=q[n];per(i,n-1,1)mini[i]=min(mini[i+1],q[i]);
	rep(i,1,n){
		int l=1,r=i,ans=r;
		while(l<=r){
			if(maxi[mid]>=q[i])ans=mid,r=mid-1;
			else l=mid+1;
		}
		seg[i].w1=ans;
		l=i,r=n,ans=l;
		while(l<=r){
			if(mini[mid]<=q[i])ans=mid,l=mid+1;
			else r=mid-1;
		}
		seg[i].w2=ans;
	}
	sort(seg+1,seg+n+1,cmp2);
	rep(i,1,n){
		int tmp=(Q(seg[i].w2)-Q(seg[i].w1-2)+mod)%mod;
		M(seg[i].w2,tmp);
	}
	printf("%d\n",(Q(n)-Q(n-1)+mod)%mod);
	return 0;
}