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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*************************************************************************************************************
										⣿⣿⣿⣿⣿⣿⣿⣿⡿⠿⠛⠛⠛⠋⠉⠈⠉⠉⠉⠉⠛⠻⢿⣿⣿⣿⣿⣿⣿⣿
										⣿⣿⣿⣿⣿⡿⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⢿⣿⣿⣿⣿
										⣿⣿⣿⣿⡏⣀⠀⠀⠀⠀⠀⠀⠀⣀⣤⣤⣤⣄⡀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿
										⣿⣿⣿⢏⣴⣿⣷⠀⠀⠀⠀⠀⢾⣿⣿⣿⣿⣿⣿⡆⠀⠀⠀⠀⠀⠀⠀⠈⣿⣿
										⣿⣿⣟⣾⣿⡟⠁⠀⠀⠀⠀⠀⢀⣾⣿⣿⣿⣿⣿⣷⢢⠀⠀⠀⠀⠀⠀⠀⢸⣿
										⣿⣿⣿⣿⣟⠀⡴⠄⠀⠀⠀⠀⠀⠀⠙⠻⣿⣿⣿⣿⣷⣄⠀⠀⠀⠀⠀⠀⠀⣿
										⣿⣿⣿⠟⠻⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠶⢴⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⠀⠀⣿
										⣿⣁⡀⠀⠀⢰⢠⣦⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣿⡄⠀⣴⣶⣿⡄⣿
										⣿⡋⠀⠀⠀⠎⢸⣿⡆⠀⠀⠀⠀⠀⠀⣴⣿⣿⣿⣿⣿⣿⣿⠗⢘⣿⣟⠛⠿⣼
										⣿⣿⠋⢀⡌⢰⣿⡿⢿⡀⠀⠀⠀⠀⠀⠙⠿⣿⣿⣿⣿⣿⡇⠀⢸⣿⣿⣧⢀⣼
										⣿⣿⣷⢻⠄⠘⠛⠋⠛⠃⠀⠀⠀⠀⠀⢿⣧⠈⠉⠙⠛⠋⠀⠀⠀⣿⣿⣿⣿⣿
										⣿⣿⣧⠀⠈⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠟⠀⠀⠀⠀⢀⢃⠀⠀⢸⣿⣿⣿⣿
										⣿⣿⡿⠀⠴⢗⣠⣤⣴⡶⠶⠖⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡸⠀⣿⣿⣿⣿
										⣿⣿⣿⡀⢠⣾⣿⠏⠀⠠⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠛⠉⠀⣿⣿⣿⣿
										⣿⣿⣿⣧⠈⢹⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⣿⣿⣿⣿
										⣿⣿⣿⣿⡄⠈⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣴⣾⣿⣿⣿⣿⣿
										⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿
										⣿⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
										⣿⣿⣿⣿⣿⣦⣄⣀⣀⣀⣀⠀⠀⠀⠀⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
										⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡄⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
										⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠙⣿⣿⡟⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿
										⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⠁⠀⠀⠹⣿⠃⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿
										⣿⣿⣿⣿⣿⣿⣿⣿⡿⠛⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⢐⣿⣿⣿⣿⣿⣿⣿⣿⣿
										⣿⣿⣿⣿⠿⠛⠉⠉⠁⠀⢻⣿⡇⠀⠀⠀⠀⠀⠀⢀⠈⣿⣿⡿⠉⠛⠛⠛⠉⠉
										⣿⡿⠋⠁⠀⠀⢀⣀⣠⡴⣸⣿⣇⡄⠀⠀⠀⠀⢀⡿⠄⠙⠛⠀⣀⣠⣤⣤⠄
*************************************************************************************************************/
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <tuple>
#include <random>
#include <vector>
#include <bitset>
#include <chrono>
#include <iomanip>
#include <cassert>
#include <complex>
#include <fstream>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
using namespace std;
#define UwU cin.tie(0);ios_base::sync_with_stdio(0);
#define test int test_num;cin>>test_num;while(test_num--)
#define prec(k) cout<<fixed<<setprecision(k);
#define cl(n,k) (((n)+(k)-1)/(k))
#define f first
#define s second
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using point = complex<double>;
ll gcd(ll a, ll b){return (b==0?a:gcd(b,a%b));}
ll lcm(ll a, ll b){return a/gcd(a,b)*b;}
const int M = 1e9 + 7;
const int O = 998244353;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323846;
const ll INF = (ll)1e18 + 5;

const int N = 5e5 + 5;
const int K = 22;

int n;
ll fac[N];
ll inv[N];
ll ifc[N];
pii w[N];
vector<pii> vec;
set<int,greater<int>> Q;
vector<int> adj[N];
bool vis[N];
mt19937 g(chrono::steady_clock::now().time_since_epoch().count());
int upl[K][N];
int upr[K][N];
int dp[N];
ll ans;

ll C(ll n, ll k){
	if(n < k || k < 0) return 0;
	return fac[n] * ifc[k] % M * ifc[n-k] % M;
}

ll C2(ll k){
	return inv[k] % M; 
}

int main(){
	UwU

	cin >> n;

	for(int i=0;i<n;i++){
		cin >> w[i].f >> w[i].s;
		vec.emplace_back(w[i].f, i);
		vec.emplace_back(w[i].s, i);
	}

	fac[0] = 1;
	for(int i=1;i<=n;i++){
		fac[i] = fac[i-1] * i % M;
	}
	inv[1] = 1;
	for(int i=2;i<=n;i++){
		inv[i] = M - (M/i) * inv[M%i] % M;
	}
	ifc[0] = 1;
	for(int i=1;i<=n;i++){
		ifc[i] = ifc[i-1] * inv[i] % M;
	}

	sort(vec.begin(), vec.end());

	for(int k=0;k<K;k++){
		fill(upl[k],upl[k]+n,-1);
		fill(upr[k],upr[k]+n,-1);
	}

	for(auto p : vec){
		if(!vis[p.s]){
			auto it = Q.lower_bound(p.s);
			if(it != Q.end()){
				adj[p.s].push_back(*it);
				upl[0][p.s] = *it;
			}
			Q.insert(p.s);
			vis[p.s] = true;
		}
		else{
			Q.erase(p.s);
			auto it = Q.lower_bound(p.s);
			if(it != Q.end()){
				adj[p.s].push_back(*it);
				upr[0][p.s] = *it;
			}
			vis[p.s] = false;
		}
	}

	for(int i=0;i<n;i++){
		for(int k=1;k<K;k++){
			int u = upl[k-1][i];
			if(u == -1) break;
			upl[k][i] = upl[k-1][u];
		}
		for(int k=1;k<K;k++){
			int u = upr[k-1][i];
			if(u == -1) break;
			upr[k][i] = upr[k-1][u];
		}
	}

	fill(dp,dp+n,1);
	for(int i=n-1;i>=0;i--){
		int l = upl[0][i];
		if(l != -1) dp[l] += dp[i];
		int r = upr[0][i];
		if(r != -1) dp[r] += dp[i];
		if(l != -1 && r != -1){
			if(w[l].s > w[r].f){
				int k = min(l, r);
				l = k;
				r = k;
			}
			int itl = K-1;
			int itr = K-1;
			int lstl = l;
			int lstr = r;
			while(l != r){
				if(itl >= 0 && (l > r || itr < 0)){
					int u = upr[itl--][l];
					if(u == -1) continue;
					lstl = l;
					l = u;
				}
				else if(itr >= 0){
					int u = upl[itr--][r];
					if(u == -1) continue;
					lstr = r;
					r = u;
				}
				else break;
				if(w[l].s > w[r].f){
					if(l < r){
						if(lstl == l){
							r = l;
							break;
						}
						l = lstl;
						lstl = -1;
					}
					else{
						if(lstr == r){
							l = r;
							break;
						}
						r = lstr;
						lstr = -1;
					}
				}
			}
			while(l != r && l != -1 && r != -1){
				if(l > r) l = upr[0][l];
				else r = upl[0][r];
			}
			if(l == r){
				dp[l] -= dp[i];
			}
		}
	}

	for(int i=0;i<n;i++){
		ans += C2(dp[i]);
		if(ans >= M) ans -= M;
	}

	cout << ans << "\n";

	return 0;
}