1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
Things to notice:
1. do not calculate useless values
2. do not use similar names
 
Things to check:
1. submit the correct file
2. time (it is log^2 or log)
3. memory
4. prove your naive thoughts 
5. long long
6. corner case like n=0,1,inf or n=m
7. check if there is a mistake in the ds or other tools you use
8. fileio in some oi-contest

9. module on time 
10. the number of a same divisor in a math problem
11. multi-information and queries for dp and ds problems
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pii pair<long long,long long>
#define mp make_pair
#define pb push_back
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const int INF=1e18;
int fpow(int x,int b)
{
	if(x==0) return 0;
	if(b==0) return 1;
	int res=1;
	while(b>0)
	{
		if(b&1)	res=1LL*res*x%mod;
		x=1LL*x*x%mod;
		b>>=1;
	}
	return res;
}
int n;
int L[500005],R[500005];
struct Segtree
{
	pii t[4000005];
	void update(int id,int l,int r,int x,int y,pii d)
	{
//		cout<<"... "<<id<<" "<<l<<" "<<r<<" "<<x<<" "<<y<<" "<<d.fi<<" "<<d.se<<"\n";
		if(x<=l&&r<=y)
		{
			t[id]=max(t[id],d);
			return;
		}
		int mid=(l+r)>>1;
		if(x<=mid) update(id<<1,l,mid,x,y,d);
		if(y>mid) update(id<<1|1,mid+1,r,x,y,d);
	}
	pii query(int id,int l,int r,int x)
	{
		if(l==r) return t[id];
		int mid=(l+r)>>1;
		if(x<=mid) return max(t[id],query(id<<1,l,mid,x));
		else return max(t[id],query(id<<1|1,mid+1,r,x));
	}
}st;
struct BIT
{
	int t[1000005];
int lowbit(int x)
{
	return x&(-x);	
} 
void update(int x,int d)
{
	for(int i=x;i<=2*n;i+=lowbit(i)) t[i]+=d;
}
int query(int x)
{
	int res=0;
	for(int i=x;i>=1;i-=lowbit(i)) res+=t[i];
	return res;
}
}bt;
int dl[500005],dr[500005],ul[500005],ur[500005],hlim[500005],maxr[500005];
int wl[20][500005],wr[20][500005],tol[20][500005],tor[20][500005];
vector <array<int,3> > opt[1000005];
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++) cin>>L[i]>>R[i];
//	for(int i=1;i<=n;i++) cin>>L[i];
//	for(int i=1;i<=n;i++) cin>>R[i];
	reverse(L+1,L+1+n),reverse(R+1,R+1+n);
	memset(dl,-1,sizeof(dl)),memset(dr,-1,sizeof(dr)),memset(ul,-1,sizeof(ul)),memset(ur,-1,sizeof(ur));
	memset(st.t,-1,sizeof(st.t));
	for(int i=n;i>=1;i--)
	{
		dl[i]=st.query(1,1,2*n,L[i]).se,dr[i]=st.query(1,1,2*n,R[i]).se;
//		cout<<i<<" "<<dl[i]<<" "<<dr[i]<<"\n";
		st.update(1,1,2*n,L[i],R[i],mp(n-i+1,i));
	}
	memset(st.t,-1,sizeof(st.t));
	for(int i=1;i<=n;i++)
	{
		ul[i]=st.query(1,1,2*n,L[i]).se,ur[i]=st.query(1,1,2*n,R[i]).se;
		st.update(1,1,2*n,L[i],R[i],mp(i,i));
	}
	for(int i=1;i<=n;i++) maxr[i]=i;
	for(int i=1;i<=n;i++) 
	{
		int v=dl[i];
		if(v!=-1&&R[maxr[i]]>R[maxr[v]]) maxr[v]=maxr[i];
		v=dr[i];
		if(v!=-1&&R[maxr[i]]>R[maxr[v]]) maxr[v]=maxr[i];
	} 
//	for(int i=1;i<=n;i++) cout<<maxr[i]<<"\n";
	memset(tol,-1,sizeof(tol)),memset(tor,-1,sizeof(tor));
	for(int i=1;i<=n;i++) tol[0][i]=(ul[i]==-1?0:ul[i]),tor[0][i]=(ur[i]==-1?0:ur[i]);//,cerr<<tol[0][i]<<" ";
//	cerr<<"...\n";
	for(int i=1;i<=n;i++) 
	{
		int v=maxr[i];
		if(ur[v]!=-1) hlim[i]=ur[v];
	} 
	for(int i=1;i<=n;i++) 
	{
		if(ul[i]==-1) ul[i]=1;
		if(ur[i]==-1) ur[i]=1;
		if(ul[i]!=-1) opt[L[i]].pb({1,i,0});
		if(ur[i]!=-1) opt[R[i]].pb({1,i,1});
		opt[R[i]].pb({0,i,0});
	}
	for(int i=1;i<=2*n;i++)
	{
		sort(opt[i].begin(),opt[i].end());
		for(int j=0;j<opt[i].size();j++)
		{
			if(opt[i][j][0]==0) bt.update(opt[i][j][1],1);
			else
			{
				int x=opt[i][j][1];
				if(opt[i][j][2]==0) wl[0][x]=bt.query(x)-bt.query(ul[x]-1);
				else wr[0][x]=bt.query(x)-bt.query(ur[x]-1);
			}
		}
	}
//	cerr<<"...\n";
//	for(int i=1;i<=n;i++) cout<<i<<": "<<wl[0][i]<<" "<<wr[0][i]<<"\n";
	for(int k=1;k<20;k++) 
	{
		for(int i=1;i<=n;i++)
		{
			if(tor[k-1][i]>0) tor[k][i]=tor[k-1][tor[k-1][i]],wr[k][i]=wr[k-1][i]+wr[k-1][tor[k-1][i]];
//			cerr<<k<<" "<<i<<" "<<tol[k-1][i]<<" "<<tol[k-1][tol[k-1][i]]<<"\n";
			if(tol[k-1][i]>0) tol[k][i]=tol[k-1][tol[k-1][i]],wl[k][i]=wl[k-1][i]+wl[k-1][tol[k-1][i]];
		}
//		cerr<<k<<"\n";
	}
//	cerr<<"...\n";
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		if(!hlim[i]) hlim[i]=-1;
		int cnt=0;
		int u=i;
		for(int k=19;k>=0;k--) if(tor[k][u]!=-1&&tor[k][u]>hlim[i]) cnt+=wr[k][u],u=tor[k][u];
		cnt+=wr[0][u];
		u=i;
		for(int k=19;k>=0;k--) if(tol[k][u]!=-1&&tol[k][u]>hlim[i]) cnt-=wl[k][u],u=tol[k][u];
		cnt-=wl[0][u];
//		cout<<i<<": "<<cnt<<"\n";
		ans=(ans+fpow(cnt,mod-2))%mod;
	}
	cout<<ans<<"\n";
}
signed main()
{
//	freopen("input.txt","r",stdin);
	ios::sync_with_stdio(0);
	cin.tie(0);
	int _=1;
//	cin>>_;
	while(_--) solve();
	return 0;
}