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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r)for(int i=(l);i<=(r);++i)
#define REP(i,n)FOR(i,0,(n)-1)
#define ssize(x)int(x.size())
#ifdef DEBUG
auto operator<<(auto&o,auto x)->decltype(x.end(),o);
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto&operator<<(auto&o,tuple<auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<")";}
auto&operator<<(auto&o,tuple<auto,auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<", "<<get<3>(t)<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

int fastin() {
	int n = 0, c = getchar_unlocked();
	while(isspace(c))
		c = getchar_unlocked();
	while(isdigit(c)) {
		n = 10 * n + (c - '0');
		c = getchar_unlocked();
	}
	return n;
}
void fastout(int x) {
	if(x == 0) {
		putchar_unlocked('0');
		putchar_unlocked('\n');
		return;
	}
	static char t[10];
	int i = 0;
	while(x) {
		t[i++] = char('0' + (x % 10));
		x /= 10;
	}
	while(--i >= 0)
		putchar_unlocked(t[i]);
	putchar_unlocked('\n');
}

struct Tree {
	int sz = 1;
	vector<int> tree;
	Tree() {}
	Tree(int N) {
		while (sz < N)
			sz *= 2;
		tree.resize(2 * sz, -1);
	}
	int l, r;
	int val;
	void upd(int w, int a, int b) {
		if (a > r or b < l)
			return;
		if (a >= l and b <= r) {
			tree[w] = max(tree[w], val);
			return;
		}
		const int mid = (a + b) / 2;
		upd(w * 2, a, mid);
		upd(w * 2 + 1, mid + 1, b);
	}
	void update(int _l, int _r, int _val) {
		l = _l;
		r = _r;
		val = _val;
		upd(1, 0, sz - 1);
	}
	int query(int x) {
		int ret = -1;
		x += sz;
		while (x) {
			ret = max(ret, tree[x]);
			x /= 2;
		}
		return ret;
	}
};

constexpr int mod = int(1e9) + 7;
int add(int a, int b) {
	a += b;
	return a >= mod ? a - mod : a;
}
int sub(int a, int b) {
	return add(a, mod - b);
}
int mul(int a, int b) {
	return int(a * LL(b) % mod);
}
int powi(int a, int b) {
	for(int ret = 1;; b /= 2) {
		if(b == 0)
			return ret;
		if(b & 1)
			ret = mul(ret, a);
		a = mul(a, a);
	}
}
int inv(int x) {
	return powi(x, mod - 2);
}
struct BinomCoeff {
	vector<int> fac, rev;
	BinomCoeff(int n) {
		fac = rev = vector(n + 1, 1);
		FOR(i, 1, n) fac[i] = mul(fac[i - 1], i);
		rev[n] = inv(fac[n]);
		for(int i = n; i > 0; --i)
			rev[i - 1] = mul(rev[i], i);
	}
	int operator()(int n, int k) {
		return mul(fac[n], mul(rev[n - k], rev[k]));
	}
};

int main() {
	int n;
	n = fastin();
	vector<int> l(n), r(n), jl(n), jr(n);
	REP(i, n) {
		l[i] = fastin();
		r[i] = fastin();
		--l[i], --r[i];
	}

	Tree tree(2 * n);
	REP(i, n) {
		jl[i] = tree.query(l[i]);
		jr[i] = tree.query(r[i]);
		tree.update(l[i], r[i], i);
	}

	const int bits = __lg(max(1, n)) + 1;
	auto get_jmp = [&](const vector<int>& v) {
		vector jmp(bits, vector(n, -1));
		jmp[0] = v;
		FOR(b, 1, bits - 1)
			REP(i, n)
				if(jmp[b - 1][i] != -1)
					jmp[b][i] = jmp[b - 1][jmp[b - 1][i]];
		return jmp;
	};

	const auto l_jmp = get_jmp(jl);
	const auto r_jmp = get_jmp(jr);

	auto common = [&](int a, int b) {
		int pa = bits - 1;
		int pb = bits - 1;
		while (max(pa, pb) >= 0) {
			if (a == b) {
				return a;
			}
			if (pa >= 0 and r_jmp[pa][a] == -1) {
				--pa;
				continue;
			}
			if (pb >= 0 and l_jmp[pb][b] == -1) {
				--pb;
				continue;
			}
			if (pb != -1 and l_jmp[pb][b] > a) {
				b = l_jmp[pb][b];
				--pb;
				continue;
			}
			if (pa != -1 and r_jmp[pa][a] > b) {
				a = r_jmp[pa][a];
				--pa;
				continue;
			}
			if (pa == -1) {
				if (l[l_jmp[pb][b]] >= r[a])
					b = l_jmp[pb][b];
				--pb;
				continue;
			}
			if (pb == -1) {
				if (r[r_jmp[pa][a]] <= l[b])
					a = r_jmp[pa][a];
				--pa;
				continue;
			}
			const auto na = r_jmp[pa][a];
			const auto nb = l_jmp[pb][b];
			auto bottom = [&](int x, int y) {
				return x == -1 or y == -1 or (r[x] >= r[y] or l[x] >= l[y]);
			};
			if (bottom(a, nb)) {
				--pb;
				continue;
			}
			if (bottom(na, b)) {
				--pa;
				continue;
			}
			if (bottom(na, nb)) {
				if (na > nb) {
					--pb;
				}
				else {
					--pa;
				}
			}
			else {
				if (na > nb) {
					a = na;
					--pa;
				}
				else {
					b = nb;
					--pb;
				}
			}
		}
		if (a == b) {
			return a;
		}
		if (l_jmp[0][b] == a) {
			return a;
		}
		if (r_jmp[0][a] == b) {
			return b;
		}
		if (r_jmp[0][a] == l_jmp[0][b]) {
			return r_jmp[0][a];
		}
		return -1;
	};

	int ans = 0;
	vector<int> push(n, 1);
	for (int i = n - 1; i >= 0; --i) {
		ans = add(ans, inv(push[i]));
		int cnt = 0;
		if (jl[i] != -1) {
			push[jl[i]] = add(push[jl[i]], push[i]);
			++cnt;
		}
		if (jr[i] != -1) {
			push[jr[i]] = add(push[jr[i]], push[i]);
			++cnt;
		}
		if (cnt != 2)
			continue;
		const int mid = common(jl[i], jr[i]);
		if (mid != -1)
			push[mid] = sub(push[mid], push[i]);
	}
	fastout(ans);
}