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: Accepted)

#include<bits/stdc++.h>
#define N 200005
#define LL long long
#define mod 1000000007
using namespace std;

struct team{
	int p,v;
	friend bool inline operator < (team a,team b){
		return a.v<b.v;
	}
}Tm[N];

struct T_node{
	int l,r;
	LL s,t1,t2;
};

struct sg{
	int l,r;
	friend bool inline operator < (sg a,sg b){
		if(a.r==b.r) return a.l<b.l;
		return a.r<b.r;
	}
}a[N];

int n,mx[N],mn[N],flag=1;

LL inline pw(LL x,LL y){
	LL ret=1,tmp=x;
	while(y){
		if(y&1) ret=ret*tmp%mod;
		y>>=1,tmp=tmp*tmp%mod;
	}
	return ret;
}

struct segment_tree{
	T_node p[N<<3];
	void inline pushdown(int x){
		if(p[x].t1==1&&p[x].t2==0) return;
		int T1=p[x].t1,T2=p[x].t2,ls=x<<1,rs=x<<1|1;
		p[x].t1=1,p[x].t2=0;
		(p[ls].s*=T1)%=mod,(p[ls].t1*=T1)%=mod,(p[ls].t2*=T1)%=mod;
		(p[ls].s+=1LL*T2*(p[ls].r-p[ls].l+1))%=mod,(p[ls].t2+=T2)%=mod;
		(p[rs].s*=T1)%=mod,(p[rs].t1*=T1)%=mod,(p[rs].t2*=T1)%=mod;
		(p[rs].s+=1LL*T2*(p[rs].r-p[rs].l+1))%=mod,(p[rs].t2+=T2)%=mod;
	}	
	void inline build(int x,int l,int r){
		p[x].l=l,p[x].r=r,p[x].t1=1;
		if(l==r) return;
		int mid=(l+r)>>1;
		build(x<<1,l,mid),build(x<<1|1,mid+1,r);	
	}
	void inline modify(int x,int l,int r,int opt,int v){
		int L=p[x].l,R=p[x].r;
		pushdown(x);
		if(l==L&&r==R){
			if(opt==2) (p[x].s+=1LL*v*(p[x].r-p[x].l+1))%=mod,(p[x].t2+=v)%=mod;
			else (p[x].s*=v)%=mod,(p[x].t1*=v)%=mod,(p[x].t2*=v)%=mod;
			return;
		}
		int mid=(L+R)>>1;
		if(r<=mid) modify(x<<1,l,r,opt,v);
		else if(l>mid) modify(x<<1|1,l,r,opt,v);
		else modify(x<<1,l,mid,opt,v),modify(x<<1|1,mid+1,r,opt,v);
		p[x].s=p[x<<1].s+p[x<<1|1].s;
	}
	LL inline query(int x,int pos){
		pushdown(x);
		if(p[x].l==p[x].r) return p[x].s;
		int mid=(p[x].l+p[x].r)>>1;
		if(pos<=mid) return query(x<<1,pos);
		return query(x<<1|1,pos);
	}
}T;

void inline solve(){
	sort(a+1,a+n+1),T.build(1,1,n);
	for(int i=1;i<=n;i++){
		int l=a[i].l,r=a[i].r;
		if(flag&&l==1) flag=0,T.modify(1,l,r,2,pw(2,i-1));
		else if(!flag){
			LL tmp=(l==1)?pw(2,i-1):T.query(1,l-1);
			T.modify(1,l,r,2,tmp);
			if(l!=1) T.modify(1,1,l-1,1,2);
		}
	}
	printf("%lld\n",T.query(1,n));
}

int inline ckmx(int p){
	int l=1,r=p,mid;
	while(l<r){
		mid=(l+r)>>1;
		if(mx[mid]>=Tm[p].p) r=mid;
		else l=mid+1;
	}
	return l;
}

int inline ckmn(int p){
	int l=p,r=n,mid;
	while(l+1<r){
		mid=(l+r)>>1;
		if(mn[mid]<=Tm[p].p) l=mid;
		else r=mid-1;
	}
	return (mn[r]<=Tm[p].p)?r:l; 
}

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d%d",&Tm[i].p,&Tm[i].v);
	sort(Tm+1,Tm+n+1),mx[0]=0,mn[n+1]=0x3f3f3f3f;
	for(int i=1;i<=n;i++) mx[i]=max(mx[i-1],Tm[i].p);
	for(int i=n;i;i--) mn[i]=min(mn[i+1],Tm[i].p);
	for(int i=1;i<=n;i++) a[i].l=ckmx(i),a[i].r=ckmn(i);
	solve();
}