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
#include<bits/stdc++.h>
using namespace std;
vector<int>graf[500010];
const long long mod = 1000000007;
long long silnie[500010];
long long osilnie[500010];
long long qp(long long a, long long b){
	if(!b)return 1;
	if(b%2)return a*qp(a, b-1)%mod;
	auto c = qp(a, b/2);
	return c*c%mod;
}
long long o[500010];
void preprocess(int n) {
	o[1] = 1;
    for(long long i=2;i<=n;i++)
		o[i] = mod - (mod / i) * o[mod % i] % mod;
}


int policz[500010];
int lewy[500010], prawy[500010];
int mini[500010], maxi[500010];

const int base = 1<<20;
int tree2[2*base], lazy2[2*base];

void push2(int x){
	if(lazy2[x] == 0)return;
	tree2[x*2] = lazy2[x*2] = tree2[x*2+1] = lazy2[x*2+1] = lazy2[x];
	lazy2[x] = 0;
}

int miotla(int w, int l, int p, int a, int b){
//	printf("%d %d %d %d %d\n", w, l, p, a, b, tree2[w], lazy2[w]);
	if(l>b || p<a)return 0;
	if(a<=l && p<=b){
		return tree2[w];
	}
	push2(w);
	int pom = max(miotla(w*2, l, (l+p)/2, a, b), miotla(w*2+1, (l+p)/2+1, p, a, b));
	tree2[w] = max(tree2[w*2], tree2[w*2+1]);
	return pom;
}
void zamiec(int w, int l, int p, int a, int b, int val){
	if(l>b || p<a)return;
	if(a<=l && p<=b){
		tree2[w] = val;
		lazy2[w] = val;
		return;
	}
	push2(w);
	zamiec(w*2, l, (l+p)/2, a, b, val);
	zamiec(w*2+1, (l+p)/2+1, p, a, b, val);
	tree2[w] = max(tree2[w*2], tree2[w*2+1]);
}
pair<int,int> jumpL[500010][21], jumpR[500010][21];

int main()
{
	int n, i, j, a, b;
	scanf("%d", &n);
	silnie[0] = 1;
	for(int i=1;i<=n;i++)
		silnie[i] = (silnie[i-1]*i)%mod;
	osilnie[n] = qp(silnie[n], mod-2);
	for(int i=n-1;i>=0;i--)osilnie[i] = (osilnie[i+1]*(i+1))%mod;
	preprocess(n+5);
	mini[0] = 1000010;
	long long on = qp(n ,mod-2);
	vector<pair<int,int>>stosik;
	for(i=1;i<=n;i++){
		scanf("%d%d", &a, &b);
		mini[i] = lewy[i] = a;
		maxi[i] = prawy[i] = b;
		int pom = miotla(1, 0, base-1, a, a);
//		printf("%d   ", pom);
		if(pom!=0)
			graf[i].push_back(pom);
		pom = miotla(1, 0, base-1, b, b);
//		printf("%d\n", pom);
		if(pom!=0 && (graf[i].size()==0||graf[i][0]!=pom))
			graf[i].push_back(pom);
		
		for(auto j: graf[i])mini[i] = min(mini[i], mini[j]);
		for(auto j: graf[i])maxi[i] = max(maxi[i], maxi[j]);
		
		if(graf[i].size()==2){
			if(maxi[graf[i][0]]>mini[graf[i][1]]){
//				printf("MAJA CZESC WSPOLNA\n");
				if(prawy[graf[i][0]]>lewy[graf[i][1]]){
					graf[i].push_back(min(graf[i][0], graf[i][1]));
				}
				else{
					int j1=20, j2=20;
					int pom1 = graf[i][0], pom2 = graf[i][1];
					while(j1>=0 && jumpR[pom1][j1].first==0)j1--;
					while(j2>=0 && jumpL[pom2][j2].first==0)j2--;
					while(j1>=0 || j2>=0){
//						printf("%d %d %d %d\n", pom1, pom2, j1, j2);
						pair<int,int>pom3, pom4;
						if(j1==-1)
							pom3 = {pom1, prawy[pom1]};
						else
							pom3 = jumpR[pom1][j1];
						if(j2==-1)
							pom4 = {pom2, lewy[pom2]};
						else
							pom4 = jumpL[pom2][j2];
						if(pom3.second < pom4.second){
							if(j2==-1)pom1 = jumpR[pom1][j1--].first;
							else if(j1==-1)pom2 = jumpL[pom2][j2--].first;
							else{
								if(pom3.first>pom4.first)
									pom1 = jumpR[pom1][j1--].first;
								else
									pom2 = jumpL[pom2][j2--].first;
							}
						}
						else{
							if(j1==-1)j2--;
							else if(j2==-1)j1--;
							else{
								if(pom3.first<pom4.first)j1--;
								else j2--;
							}
						}
					}
	//				printf("%d %d %d %d\n", pom1, pom2, j1, j2);
	//				printf("%d %d %d\n", kon, skoczL(graf[i][1], kon), skoczR(graf[i][0], kon));
					if(prawy[pom1]>lewy[pom2])
						pom = min(pom1, pom2);
					else{
						if(jumpR[pom1][0].second>lewy[pom2] && jumpR[pom1][0].first>pom2)pom = pom2;
						else if(jumpL[pom2][0].first>pom1 && jumpL[pom2][0].second<prawy[pom1])pom = pom1;
						else
							pom = min(jumpR[pom1][0].first, jumpL[pom2][0].first);
					}
					graf[i].push_back(pom);
				}
			}
		}
		if(graf[i].size()){
			if(graf[i].size()<3){
				jumpR[i][0] = {graf[i].back(), max(prawy[i], prawy[graf[i].back()])};
				jumpL[i][0] = {graf[i][0], min(lewy[i], lewy[graf[i][0]])};
			}
			else{
				jumpR[i][0] = {graf[i][1], max(prawy[i], prawy[graf[i][1]])};
				jumpL[i][0] = {graf[i][0], min(lewy[i], lewy[graf[i][0]])};
			}
		}
		for(int j=1;j<21;j++){
			jumpL[i][j] = {jumpL[jumpL[i][j-1].first][j-1].first, min(jumpL[i][j-1].second, jumpL[jumpL[i][j-1].first][j-1].second)};
			jumpR[i][j] = {jumpR[jumpR[i][j-1].first][j-1].first, max(jumpR[i][j-1].second, jumpR[jumpR[i][j-1].first][j-1].second)};
		}
		zamiec(1, 0, base-1, a, b, i);
	}
	for(i=n;i>=1;i--){
		policz[i]++;
//		printf("%d: %d  ", i, policz[i]);for(auto j: graf[i])printf("%d ", j);printf("\n");
		if(graf[i].size()==3){
			policz[graf[i][0]]+=policz[i];
			policz[graf[i][1]]+=policz[i];
			policz[graf[i][2]]-=policz[i];
		}
		else{
			for(auto j: graf[i])policz[j]+=policz[i];
		}
	}
	long long wynik = 0;
	for(i=1;i<=n;i++){
//		printf("i:%d: POLICZ:%d   GRAF: ", i, policz[i]);for(auto j: graf[i])printf("%d ", j);printf("\n");

		policz[i]--;
		long long pom = on*silnie[n-1-policz[i]]%mod*osilnie[n-1]%mod*(silnie[n]*(n-policz[i])%mod*o[policz[i]+1]%mod*osilnie[n-policz[i]]%mod)%mod;
		wynik = (wynik + pom)%mod;
	}
	printf("%lld\n", wynik);
}