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
#include <bits/stdc++.h>
using namespace std;

const int MOD = 1e9 + 7;
int my_pow(int a, int b) {
	int r = 1;
	while (b) {
		if (b % 2) {
			r = (long long) r * a % MOD;
		}
		a = (long long) a * a % MOD;
		b /= 2;
	}
	return r;
}
int my_inv(int a) {
	return my_pow(a, MOD - 2);
}

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	int n;
	cin >> n;
	// from the bottom
	vector<pair<int,int>> a(n);
	
	for (pair<int,int>& p : a) {
		cin >> p.first >> p.second;
	}
	// vector<int> perm(2 * n);
	// for (int i = 0; i < 2 * n; i++) {
		// perm[i] = i + 1;
	// }
	// random_shuffle(perm.begin(), perm.end());
	// for (int i = 0; i < n; i++) {
		// int x = min(perm[2*i], perm[2*i+1]);
		// int y = max(perm[2*i], perm[2*i+1]);
		// a[i] = {x, y};
	// }
	
	// from the top
	reverse(a.begin(), a.end());
	vector<vector<int>> edges(n);
	set<pair<int,int>> s;
	for (int i = 0; i < n; i++) {
		auto it = s.lower_bound({a[i].first,0});
		while (it != s.end() && it->first < a[i].second) {
			auto it2 = it;
			it2++;
			edges[it->second].push_back(i);
			s.erase(it);
			it = it2;
		}
		s.insert({a[i].first, i});
		s.insert({a[i].second, i});
		/*
		for (int x : {a[i].first, a[i].second}) {
			// if (i + 1 < n) {
				// int k = i + 1 + rand() % (n - (i + 1));
				// edges[i].push_back(k);
			// }
			for (int k = i + 1; k < n; k++) {
				if (a[k].first < x && x < a[k].second) {
					edges[i].push_back(k);
					break;
				}
			}
		}
		edges[i].resize(2);*/
	}
	for (int i = 0; i < n; i++) {
		edges[i].resize(2);
		// if (i + 1 < n) {
			// for (int& x : edges[i]) {
				// x = i + 1 + rand() % (n - (i + 1));
			// }
		// }
			
	}
	vector<int> answer(n);
	const int MAGIC = 128;
	vector<bitset<MAGIC>> mask(n);
	for (int start = 0; start < n; start += MAGIC) {
		for (int i = start; i < min(n, start + MAGIC); i++) {
			mask[i][i-start] = 1;
			mask[edges[i][0]] |= mask[i];
			mask[edges[i][1]] |= mask[i];
			answer[i] += mask[i].count();
			mask[i] = 0;
		}
		for (int i = start + MAGIC; i < n; i++) {
			mask[edges[i][0]] |= mask[i];
			mask[edges[i][1]] |= mask[i];
			answer[i] += mask[i].count();
			mask[i] = 0;
		}
	}
	int total = 0;
	for (int i = 0; i < n; i++) {
		total = (total + my_inv(answer[i])) % MOD;
	}
	cout << total << endl;
}