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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define se second

const int MOD = 1e9 + 7;
const int BASE = 1024 * 1024;
const int MAXN = 5e4 + 7;
ll inv(ll a){
	if(a <= 1){
		return a;
	}
	return MOD - (ll)(MOD / a) * inv(MOD % a) % MOD;
}

int n;
vector<pair<int, int>> vec;
int tree[2 * BASE + 7];//fi -> na co zmieniasz, se -> kiedy zmieniono
vector<int> g[MAXN];
ll fact[MAXN];
bitset<50007> vis[MAXN];

int query(int ind){
	ind += BASE;
	int res = 0;
	while(ind > 0){
		res = max(res, tree[ind]);
		ind /= 2;
	}

	return res;
}

void update(int v, int l, int p, int a, int b, int val){
	if(p < a || b < l){
		return;
	}

	if(a <= l && p <= b){
		tree[v] = val;
		return;
	}

	int mid = (l + p) / 2;
	update(2 * v, l, mid, a, b, val);
	update(2 * v + 1, mid + 1, p, a, b, val);
}

void preprocess(){
	fact[0] = 1;
	for(int i = 1; i <= n; i++){
		fact[i] = (ll)fact[i - 1] * i;
		fact[i] %= MOD;
	}
}

ll dwumian(ll n, ll k){
	return (ll)((ll)fact[n] * inv(fact[k]) % MOD) * inv(fact[n - k]) % MOD;
}

void getPos(){
	for(int i = n; i >= 1; i--){
		vis[i][i] = 1;

		for(auto& u : g[i]){
			vis[u] |= vis[i];
		}
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> n;
	vec.resize(n + 1);
	for(int i = 1; i <= n; i++){
		cin >> vec[i].first >> vec[i].second;
	}

	preprocess();

	for(int i = 1; i <= n; i++){
		int p = query(vec[i].first);
		int d = query(vec[i].second);

		if(p != 0){
			g[i].push_back(p);
		}
		if(d != 0){
			g[i].push_back(d);
		}
		update(1, 0, BASE - 1, vec[i].first, vec[i].second, i);
	}

	getPos();

	ll ans = 0;
	for(int i = 1; i <= n; i++){
		int k = (int)vis[i].count();
		k--;
		//cout << fact[k] << ' ' << dwumian(n, k + 1) << ' ' << fact[n - k - 1] << '\n';
		ans = (ll)ans + (ll)((ll)fact[k] * dwumian(n, k + 1) % MOD) * fact[n - k - 1] % MOD;
		ans %= MOD;
		//cout << i << ' ' << k << '\n';
		//cout << ans << '\n';
	}
	//cout << ans << '\n';
	cout << (ll)ans * inv(fact[n]) % MOD << '\n';

	return 0;
}