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: Runtime Error)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
 
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld; 
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;

const int MOD = 1e9 + 7;
int add(int a, int b)
{
	a+=b;
	while(a>=MOD) a-=MOD;
	return a;
}
int mult(int a, int b)
{
	return (a*1LL*b)%MOD;
}

ii a[211111];
int dp[211111],L[211111],R[211111];
int pref[211111];

bool cmp(const ii &a, const ii &b)
{
	if(a.fi!=b.fi) return a.fi<b.fi;
	else return a.se>b.se;
}

int sum(int l, int r)
{
	if(l==0) return pref[r];
	else return add(pref[r],MOD-pref[l-1]);
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n; cin>>n;
	vector<ii> vec;
	for(int i=0;i<n;i++)
	{
		cin>>a[i].fi>>a[i].se;
	}
	sort(a,a+n);
	for(int i=0;i<n;i++)
	{
		vec.pb(mp(a[i].se,i));
	}
	sort(vec.begin(),vec.end());
	int mx=0; int mn=n;
	for(int i=0;i<n;i++)
	{
		mx=max(mx,vec[i].se);
		R[vec[i].se]=mx;
	}
	for(int i=n-1;i>=0;i--)
	{
		mn=min(mn,vec[i].se);
		L[vec[i].se]=mn;
	}
	vector<ii> tmp;
	for(int i=0;i<n;i++) 
	{
		L[i]++,R[i]++;
		tmp.pb(mp(L[i],R[i]));
	}
	sort(tmp.begin(),tmp.end(),cmp);
	for(int i=0;i<tmp.size();i++)
	{
		while(!vec.empty()&&vec.back().fi<=tmp[i].fi&&tmp[i].se<=vec.back().se)
		{
			vec.pop_back();
		}
		vec.pb(tmp[i]);
	}
	vi ele2;
	for(int i=0;i<vec.size();i++) ele2.pb(vec[i].se);
	dp[0]=1; pref[0]=1;
	for(int i=1;i<=n;i++)
	{
		int z=lower_bound(ele2.begin(),ele2.end(),i)-ele2.begin();
		z--;
		int lastgood = 0;
		if(z>=0) lastgood = vec[z].fi;
		dp[i] = add(dp[i],sum(lastgood,i-1));
		pref[i]=add(pref[i-1],dp[i]);
	}
	cout<<add(pref[n],MOD-pref[vec.back().fi-1])<<'\n';
}