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>
#define LL long long
#define P pair<int,LL>
#define db double
#define fi first
#define se second
const LL N=5e5+10;
const LL inf=0x3f3f3f3f;
const LL mod=1e9+7;
const double eps=1e-9;
using namespace std;
template<typename tp> inline void read(tp &x)
{
    x=0; char c=getchar(); bool f=0;
    for(;c<'0'||c>'9';f|=(c=='-'),c = getchar());
    for(;c>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0',c = getchar());
    if(f) x=-x;
}
int n;
struct Node{double k,b;}node[N];
bool cmp(Node a,Node b){return a.b<b.b;}
double maxn[N],minn[N];
int find_L(int l,int r,double k,int ret)
{
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(maxn[mid]>k) ret=mid,r=mid-1;
		else l=mid+1;
	}
	return ret;
}
int find_R(int l,int r,double k,int ret)
{
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(minn[mid]<k) ret=mid,l=mid+1;
		else r=mid-1;
	}
	return ret;
}
int L[N],R[N],ri=0;
vector<int> vec[N];
LL sum[N],dp[N];
void solve()
{
	sum[0]=dp[0]=1;
	for(int i=1;i<=n;i++)
	{
		dp[i]=(sum[i-1]-(ri?sum[ri-1]:0)+mod)%mod;
		sum[i]=(sum[i-1]+dp[i])%mod;
		for(int j=0;j<vec[i].size();j++)
			ri=max(ri,vec[i][j]);
	}
}
int main()
{
    read(n);
	for(int i=1;i<=n;i++) scanf("%lf %lf",&node[i].b,&node[i].k);
	sort(node+1,node+n+1,cmp);
	minn[n]=node[n].k;maxn[1]=node[1].k;
	for(int i=2;i<=n;i++) maxn[i]=max(maxn[i-1],node[i].k);
	for(int i=n-1;i>=1;i--) minn[i]=min(minn[i+1],node[i].k);
	// for(int i=1;i<=n;i++) L[i]=find_L(1,i,node[i].k,i);
	// for(int i=1;i<=n;i++) R[i]=find_R(i,n,node[i].k,i);
	// for(int i=1;i<=n;i++) cout<<maxn[i]<<" ";puts("");
	// for(int i=1;i<=n;i++) cout<<minn[i]<<" ";puts("");
	for(int i=1;i<=n;i++)
		printf("%lf %lf %d %d\n",node[i].k,node[i].b,L[i],R[i]);
	for(int i=1;i<=n;i++) vec[R[i]].push_back(L[i]);
	solve();
	printf("%lld\n",((sum[n]-(ri?sum[ri-1]:0))%mod+mod)%mod);
    return 0;
}