//2024-03-17 //author: Marcin Rolbiecki #include <bits/stdc++.h> using namespace std; #define ll long long const int maxN = 5e5 + 2; const ll mod = 1e9 + 7; int n; int l[maxN], r[maxN], ile[maxN]; ll fac[maxN], invFac[maxN]; vector <int> G [maxN]; ll fastpow (ll a, ll b) { ll res = 1; while (b) { if (b & 1) { res = (res * a) % mod; } a = (a * a) % mod; b >>= 1; } return res; } ll modInv (ll a) { return fastpow(a, mod - 2); } ll choose (ll a, ll b) { return fac[a] * invFac[b] % mod * invFac[a-b] % mod; } ll formula (ll i) { return choose(n, ile[i]+1) * fac[ ile[i] ] % mod * fac[n - ile[i] - 1] % mod; } void prec () { fac[0] = 1; for (int i = 1; i < maxN; i++) { fac[i] = fac[i - 1] * i % mod; } invFac[maxN - 1] = modInv(fac[maxN-1]); for (int i = maxN-2; i >= 0; i--) { invFac[i] = invFac[i + 1] * (i + 1) % mod; } } int vis[maxN]; void dfs (int u, int root) { vis[u] = root; for (int v : G[u]) { if (vis[v] != root) { ile[v]++; dfs(v, root); } } } int main () { ios_base::sync_with_stdio(0); cin.tie(0); prec(); cin >> n; for (int i = 1; i <= n; i++) { cin >> l[i] >> r[i]; } for (int i = n; i >= 1; i--) { for (int j = i-1; j >= 1; j--) { if (l[j] < l[i] && l[i] < r[j]) { G[i].push_back(j); break; } } for (int j = i-1; j >= 1; j--) { if (l[j] < r[i] && r[i] < r[j]) { G[i].push_back(j); break; } } if (G[i].size() == 2 && G[i][0] == G[i][1]) { G[i].pop_back(); } } for (int i = 1; i <= n; i++) { dfs(i, i); } ll ans = 0; for (int i = 1; i <= n; i++) { ans = (ans + formula(i)) % mod; } ans = ans * invFac[n] % mod; cout << ans; return 0; }
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 | //2024-03-17 //author: Marcin Rolbiecki #include <bits/stdc++.h> using namespace std; #define ll long long const int maxN = 5e5 + 2; const ll mod = 1e9 + 7; int n; int l[maxN], r[maxN], ile[maxN]; ll fac[maxN], invFac[maxN]; vector <int> G [maxN]; ll fastpow (ll a, ll b) { ll res = 1; while (b) { if (b & 1) { res = (res * a) % mod; } a = (a * a) % mod; b >>= 1; } return res; } ll modInv (ll a) { return fastpow(a, mod - 2); } ll choose (ll a, ll b) { return fac[a] * invFac[b] % mod * invFac[a-b] % mod; } ll formula (ll i) { return choose(n, ile[i]+1) * fac[ ile[i] ] % mod * fac[n - ile[i] - 1] % mod; } void prec () { fac[0] = 1; for (int i = 1; i < maxN; i++) { fac[i] = fac[i - 1] * i % mod; } invFac[maxN - 1] = modInv(fac[maxN-1]); for (int i = maxN-2; i >= 0; i--) { invFac[i] = invFac[i + 1] * (i + 1) % mod; } } int vis[maxN]; void dfs (int u, int root) { vis[u] = root; for (int v : G[u]) { if (vis[v] != root) { ile[v]++; dfs(v, root); } } } int main () { ios_base::sync_with_stdio(0); cin.tie(0); prec(); cin >> n; for (int i = 1; i <= n; i++) { cin >> l[i] >> r[i]; } for (int i = n; i >= 1; i--) { for (int j = i-1; j >= 1; j--) { if (l[j] < l[i] && l[i] < r[j]) { G[i].push_back(j); break; } } for (int j = i-1; j >= 1; j--) { if (l[j] < r[i] && r[i] < r[j]) { G[i].push_back(j); break; } } if (G[i].size() == 2 && G[i][0] == G[i][1]) { G[i].pop_back(); } } for (int i = 1; i <= n; i++) { dfs(i, i); } ll ans = 0; for (int i = 1; i <= n; i++) { ans = (ans + formula(i)) % mod; } ans = ans * invFac[n] % mod; cout << ans; return 0; } |