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
189
190
191
//ANMHLIJKTJIY!
#pragma GCC optimize(2)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define mod 1000000007
#define F first
#define S second
#define ll int
#define N 500010
#define M 20000010
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
using namespace std;
char buf[1<<23],*p1=buf,*p2=buf;
ll rint(){
	ll ret=0;
	char c=getchar();
	while(!isdigit(c)) c=getchar();
	while(isdigit(c)) ret=ret*10+(c-'0'),c=getchar();
	return ret;
}
struct SegT{
	ll lo[N<<3],hi[N<<3],val[N<<3];
	void build(ll x,ll l,ll r)
	{
		lo[x]=l,hi[x]=r,val[x]=-1;
		if(l==r)
		{
			return;
		}
		ll mid=(l+r)>>1,a=x<<1;
		build(a,l,mid);
		build(a|1,mid+1,r);
		return;
	}
	void pushdown(ll x)
	{
		if(val[x]==-1)
		{
			return;
		}
		ll a=x<<1;
		val[a]=val[a|1]=val[x];
		val[x]=-1;
		return;
	}
	void update(ll x,ll l,ll r,ll v)
	{
		ll tl=lo[x],tr=hi[x];
		if(l<=tl&&tr<=r)
		{
			val[x]=v;
			return;
		}
		pushdown(x);
		ll mid=(tl+tr)>>1,a=x<<1;
		if(mid>=l)
		{
			update(a,l,r,v);
		}
		if(mid<r)
		{
			update(a|1,l,r,v);
		}
		return;
	}
	ll query(ll x,ll l)
	{
		ll tl=lo[x],tr=hi[x];
		if(tl==tr)
		{
			return val[x];
		}
		pushdown(x);
		ll mid=(tl+tr)>>1,a=x<<1;
		if(mid>=l)
		{
			return query(a,l);
		}
		return query(a|1,l);
	}
}segt;
ll ch[M][2],tot,val[M],rt[N],curto;
bool fl[M];
void ins(ll &x,ll l,ll r,ll i,ll v)
{
	x=++tot;
	if(l==r)
	{
		val[x]=v;
		fl[x]=true;
		return;
	}
	ll mid=(l+r)>>1;
	if(mid>=i)
	{
		ins(ch[x][0],l,mid,i,v);
	}
	else
	{
		ins(ch[x][1],mid+1,r,i,v);
	}
	val[x]=val[ch[x][0]]+val[ch[x][1]];
	fl[x]=fl[ch[x][0]]&fl[ch[x][1]];
	return;
}
void Merge(ll &x,ll &y,ll l,ll r)
{
	if((!x)||(!y)||x==y)
	{
		x=x|y;
		return;
	}
	if(fl[x])
	{
		return;
	}
	if(fl[y])
	{
		x=y;
		return;
	}
	if(x<=curto)
	{
		++tot;
		ch[tot][0]=ch[x][0],ch[tot][1]=ch[x][1],val[tot]=val[x],fl[tot]=fl[x];
		x=tot;
	}
	ll mid=(l+r)>>1;
	Merge(ch[x][0],ch[y][0],l,mid);
	Merge(ch[x][1],ch[y][1],mid+1,r);
	val[x]=val[ch[x][0]]+val[ch[x][1]];
	fl[x]=fl[ch[x][0]]&fl[ch[x][1]];
	return;
}
ll n,lo[N],hi[N],inv[N],f[N];
vector<ll> vt[N];
int main(){
	ll i,x;
	inv[1]=1;
	for(i=2;i<N;i++)
	{
		inv[i]=(1ll*inv[mod%i]*(mod-mod/i))%mod;
	}
	n=rint();
	for(i=0;i<n;i++)
	{
		lo[i]=rint()-1,hi[i]=rint()-1;
	}
	segt.build(1,0,n*2-1);
	for(i=0;i<n;i++)
	{
		x=segt.query(1,lo[i]);
		if(x>=0)
		{
			vt[x].push_back(i);
		}
		x=segt.query(1,hi[i]);
		if(x>=0)
		{
			vt[x].push_back(i);
		}
		segt.update(1,lo[i],hi[i],i);
	}
	tot=0;
	for(i=0;i<n;i++)
	{
		ins(rt[i],0,n-1,i,1);
	}
	for(x=n-1;x>=0;x--)
	{
		curto=tot;
		for(i=0;i<vt[x].size();i++)
		{
			Merge(rt[x],rt[vt[x][i]],0,n-1);
		}
		f[x]=val[rt[x]];
	}
	ll ans=0;
	for(i=0;i<n;i++)
	{
		ans=(ans+inv[f[i]])%mod;
	}
	printf("%d\n",ans);
	return 0;
}