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 Set(a,b) memset(a,b,sizeof(a))
template<class T>inline void init(T&x){
	x=0;char ch=getchar();bool t=0;
	for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') t=1;
	for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+(ch-48);
	if(t) x=-x;return;
}typedef long long ll;
const int N=2e5+10;
int f[N],V[N],X[N];
struct pegion{
	int X,V;
	inline bool operator <(const pegion b)const{return X<b.X;}
}P[N];
int id[N],L[N],R[N];
int n;
inline int cmp(int i,int j){return P[i].V<P[j].V;}
inline int cmp2(int i,int j){return R[i]<R[j]||(R[i]==R[j]&&L[i]<L[j]);}
int Pfast[N],Sslow[N],pos[N];
const int mod=1e9+7;
template<class T>inline void Inc(T&x,int y){x+=y;if(x>=mod) x-=mod;}
template<class T>inline void Dec(T&x,int y){x-=y;if(x < 0 ) x+=mod;}
int tr[N];
#define lowbit(a) ((a)&(-a))
inline void Update(int p,int x){for(;p<=n;p+=lowbit(p)) Inc(tr[p],x);return;}
inline int  Query(int p){int re=p? 1:0;for(;p;p-=lowbit(p)) Inc(re,tr[p]);return re;}
inline int Lower(int x){
	int l=1,r=n,pos=n;
	while(l<=r){
		int mid=(l+r)>>1;
		if(P[id[mid]].V>=x) pos=mid,r=mid-1;
		else l=mid+1;
	}
	return pos;
}

int main()
{
	init(n);
	for(int i=1;i<=n;++i) {init(P[i].X),init(P[i].V);id[i]=i;}
	sort(P+1,P+1+n);sort(id+1,id+1+n,cmp);
	for(int i=1;i<=n;++i) {
		pos[id[i]]=i;
		Pfast[i]=max(Pfast[i-1],P[i].V);
	}Sslow[n]=P[n].V;
	for(int i=n-1;i;--i) Sslow[i]=min(Sslow[i+1],P[i].V);
	for(int i=1;i<=n;++i) {R[i]=Lower(Pfast[i]);L[i]=Lower(Sslow[i]);}
	sort(id+1,id+1+n,cmp2);
	int ans=0;int sum=0;
	for(int i=1;i<=n;++i) {
		int p=id[i];
		sum=Query(R[p]);Dec(sum,Query(L[p]-1));
		Update(min(n,R[p]),sum);
		if(R[p]==n) Inc(ans,sum);
	}
	cout<<ans<<endl;
	return 0;
}