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

using namespace std;

const int N = 1e6 + 5, logN = 25, Mod = 1e9 + 7;

inline int power(int x, int y){
	int ret = 1;
	while(y){
		if(y & 1) ret = 1ll * ret * x % Mod;
		x = 1ll * x * x % Mod, y >>= 1;
	}
	return ret;
}

inline int inv(int x){
	return power(x, Mod - 2);
}


class segment_tree{
private:
	int n, t[N << 1];
public:
	inline void init(int n0){
		n = n0;
		memset(t, -1, sizeof(t));
	}
	inline void push(int p){
		for(int h = 20 ; h > 0 ; h --){
			int i = p >> h;
			if(t[i] != -1){
				t[i << 1] = t[i], t[i << 1 | 1] = t[i];
				t[i] = -1;
			}
		}
	}
	inline void inc(int l, int r, int x){
		for(push(l += n), push((r += n) - 1) ; l < r ; l >>= 1, r >>= 1){
			if(l & 1) t[l ++] = x;
			if(r & 1) t[-- r] = x;
		}
	}
	inline int query(int p){
		push(p += n);
		return t[p];
	}
} seg;

class binary_indexed_tree{
private:
	int n, t[N];
public:
	inline int lowbit(int x){
		return x & -x;
	}
	inline void init(int n0){
		n = n0;
		memset(t, 0, sizeof(t));
	}
	inline void modify(int p, int x){
		while(p <= n){
			t[p] += x;
			p = p + lowbit(p);
		}
	}
	inline int query(int p){
		int ret = 0;
		while(p > 0){
			ret = ret + t[p];
			p = p - lowbit(p);
		}
		return ret;
	}
} bit;

int n = 0, p[N] = {}, l[N] = {}, r[N] = {}, h[N] = {};
int f[logN][N] = {}, g[logN][N] = {}, a[logN][N] = {}, b[logN][N] = {}, ans = 0;
vector<int> E[N] = {};
vector<int> s[N] = {}, t[N] = {};

int main(){
	scanf("%d", &n);
	for(int i = n ; i >= 1 ; i --){
		scanf("%d %d", &l[i], &r[i]);
		p[l[i]] = p[r[i]] = i, h[i] = r[i];
	}
	p[0] = p[2 * n + 1] = 0, l[0] = -1, r[0] = 2 * n + 1;
	seg.init(2 * n + 2);
	for(int i = 0 ; i <= n ; i ++){
		if(i) f[0][i] = seg.query(l[i]), g[0][i] = seg.query(r[i]);
		seg.inc(l[i], r[i] + 1, i);
	}
	seg.init(2 * n + 2);
	for(int i = n ; i >= 1 ; i --){
		E[i].push_back(seg.query(l[i])), E[i].push_back(seg.query(r[i]));
		seg.inc(l[i], r[i] + 1, i);
	}
	for(int i = 1 ; i <= n ; i ++) for(int j : E[i]) if(j != -1) h[j] = max(h[j], h[i]);
	for(int i = 1 ; i <= n ; i ++) h[i] = g[0][p[h[i]]];
	bit.init(2 * n);
	for(int i = 1 ; i <= n ; i ++) s[f[0][i]].push_back(i), t[g[0][i]].push_back(i);
	for(int i = 1 ; i <= n ; i ++){
		bit.modify(r[i], 1);
		for(int j : s[i]) a[0][j] -= bit.query(l[j]);
		for(int j : t[i]) b[0][j] -= bit.query(r[j]);
		a[0][i] += bit.query(l[i]), b[0][i] += bit.query(r[i]);
	}
	for(int d = 0 ; d < 20 ; d ++) for(int i = 1 ; i <= n ; i ++){
		f[d + 1][i] = f[d][f[d][i]], g[d + 1][i] = g[d][g[d][i]];
		a[d + 1][i] = a[d][i] + a[d][f[d][i]], b[d + 1][i] = b[d][i] + b[d][g[d][i]];
	}
	for(int i = 1 ; i <= n ; i ++){
		int x = 0;
		for(int d = 20, j = i ; d >= 0 ; d --) if(f[d][j] >= h[i]) x -= a[d][j], j = f[d][j];
		for(int d = 20, j = i ; d >= 0 ; d --) if(g[d][j] >= h[i]) x += b[d][j], j = g[d][j];
		ans = (ans + inv(x)) % Mod;
	}
	printf("%d\n", ans);
	return 0;
}