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
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, b, e) for(int i = (b); i < (e); i++)
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;

auto &operator<<(auto &o, pair<auto, auto> p) {
	return o << "(" << p.st << ", " << p.nd << ")"; }
auto operator<<(auto &o, auto x)->decltype(end(x), o) {
	o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e;
	return o << "}"; }
#ifdef LOCAL
#define deb(x...) cerr << "[" #x "]: ", [](auto...$) { \
					((cerr << $ << "; "),...) << endl; }(x)
#else
#define deb(...)
#endif

struct SegmentTree {
	vector<pii> tree;
	int T = 1, czas = 0;

	SegmentTree(int n) {
		while(T < n) T *= 2;
		tree.resize(T * 2);
	}

	int ask(int p) {
		pii res;
		for(p += T; p > 0; p >>= 1) res = max(res, tree[p]);
		return res.nd;
	}

	void sett(int l, int r, int val) {
		czas++;
		for(l += T, r += T; l <= r; l >>= 1, r >>= 1) {
			if(l & 1) tree[l++] = {czas, val};
			if((r & 1) ^ 1) tree[r--] = {czas, val};
		}
	}
};

const int mod = 1e9 + 7;

int pov(int base, int exp) {
	int res = 1;
	for(; exp; exp >>= 1) {
		if(exp & 1) res = ll(res) * base % mod;
		base = ll(base) * base % mod;
	}
	return res;
}

static inline uint_fast8_t popcnt_u128(__uint128_t n) {
	return popcount(uint64_t(n)) + popcount(uint64_t(n >> 64));
}

using ull = __uint128_t;
const int N = 5e5 + 5;
const int B = 128;
ull reach1[N], reach2[N];

void solve() {
	int n;
	cin >> n;
	n++;
	vector<pii> vec(n);
	vec[0] = {0, n * 2 - 1};
	FOR(i, 1, n) cin >> vec[i].st >> vec[i].nd;
	vi lewy(n), prawy(n);
	SegmentTree jan(n * 2);
	FOR(i, 0, n) {
		lewy[i] = jan.ask(vec[i].st);
		prawy[i] = jan.ask(vec[i].nd);
		jan.sett(vec[i].st, vec[i].nd, i);
	}
	vi przed(n);
	for(int i = 0; i < n; i += B * 2) {
		int bound = min(i + B * 2, n);
		FOR(j, 0, i) reach1[j] = 0, reach2[j] = 0;
		FOR(j, i, bound) {
			if(j - i < B) reach1[j] = ull(1) << (j - i);
			else reach2[j] = ull(1) << (j - i - B);
		}
		for(int j = bound - 1; j >= 0; j--) {
			reach1[lewy[j]] |= reach1[j];
			reach2[lewy[j]] |= reach2[j];
			reach1[prawy[j]] |= reach1[j];
			reach2[prawy[j]] |= reach2[j];
			przed[j] += popcnt_u128(reach1[j]) + popcnt_u128(reach2[j]);
		}
	}
	int ans = 0;
	FOR(i, 1, n) ans = (ans + pov(przed[i], mod-2)) % mod;
	cout << ans << '\n';
}

int main() {
	cin.tie(0)->sync_with_stdio(0);
	int tt = 1;
	// cin >> tt;
	FOR(te, 0, tt) solve();
	return 0;
}