#include <bits/stdc++.h> using namespace std; using ll = long long; /////////////////////// ll P = 1e9 + 7; ll fastexp(ll base, ll exp, ll mod) { if(exp == 0) return 1; if(exp % 2 == 0) { ll t = fastexp(base, exp / 2, mod); return (t * t) % mod; } else { return (base * fastexp(base, exp - 1, mod)) % mod; } } ll inverse_mod_prime(ll a, ll p) { return fastexp(a, p - 2, p); } ll factorials[500000]; void init() { factorials[0] = 1; for(ll i = 1; i < 500000; i++) { factorials[i] = (i * factorials[i - 1]) % P; } } /////////////////////// int n; vector<int> l, r; vector<vector<int>> E; void dfs(int x, vector<bool>& visited, int& count) { if(visited[x]) return; visited[x] = true; count++; for(int child : E[x]) { dfs(child, visited, count); } } int main() { init(); cin >> n; l = vector<int>(n + 1); r = vector<int>(n + 1); E = vector<vector<int>>(n + 1); for(int i = 1; i <= n; i++) { cin >> l[i] >> r[i]; } map<int, int> water_lines; for(int x = n; x >= 1; x--) { //for(auto p : water_lines) { // std::cerr << std::format("({}, {}) ", p.first, p.second); //} //std::cerr << "\n"; for(auto ptr = water_lines.lower_bound(l[x]); ptr != water_lines.upper_bound(r[x]); ptr++) { //E[ptr->second].push_back(x); E[x].push_back(ptr->second); //std::cerr << ptr->second << " -> " << x << "\n"; } water_lines.erase(water_lines.lower_bound(l[x]), water_lines.upper_bound(r[x])); water_lines[l[x]] = x; water_lines[r[x]] = x; } vector<int> upward_counts(n + 1, -1); for(int x = 1; x <= n; x++) { vector<bool> visited(n + 1, false); dfs(x, visited, upward_counts[x]); //std::cerr << x << " " << upward_counts[x] << "\n"; } ll total = 0; for(int x = 1; x <= n; x++) { ll ci = n - 1 - upward_counts[x]; assert(ci >= 0); assert(n - 1 >= ci); ll term = 0; for(int j = 0; j <= ci; j++) { term += (factorials[n - j - 1] * inverse_mod_prime(factorials[ci - j], P)); term %= P; } term *= factorials[ci]; term %= P; //std::cerr << x << " " << ci << " " << term << "\n"; total += term * inverse_mod_prime(factorials[n], P) % P; } std::cout << total % P << "\n"; }
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 | #include <bits/stdc++.h> using namespace std; using ll = long long; /////////////////////// ll P = 1e9 + 7; ll fastexp(ll base, ll exp, ll mod) { if(exp == 0) return 1; if(exp % 2 == 0) { ll t = fastexp(base, exp / 2, mod); return (t * t) % mod; } else { return (base * fastexp(base, exp - 1, mod)) % mod; } } ll inverse_mod_prime(ll a, ll p) { return fastexp(a, p - 2, p); } ll factorials[500000]; void init() { factorials[0] = 1; for(ll i = 1; i < 500000; i++) { factorials[i] = (i * factorials[i - 1]) % P; } } /////////////////////// int n; vector<int> l, r; vector<vector<int>> E; void dfs(int x, vector<bool>& visited, int& count) { if(visited[x]) return; visited[x] = true; count++; for(int child : E[x]) { dfs(child, visited, count); } } int main() { init(); cin >> n; l = vector<int>(n + 1); r = vector<int>(n + 1); E = vector<vector<int>>(n + 1); for(int i = 1; i <= n; i++) { cin >> l[i] >> r[i]; } map<int, int> water_lines; for(int x = n; x >= 1; x--) { //for(auto p : water_lines) { // std::cerr << std::format("({}, {}) ", p.first, p.second); //} //std::cerr << "\n"; for(auto ptr = water_lines.lower_bound(l[x]); ptr != water_lines.upper_bound(r[x]); ptr++) { //E[ptr->second].push_back(x); E[x].push_back(ptr->second); //std::cerr << ptr->second << " -> " << x << "\n"; } water_lines.erase(water_lines.lower_bound(l[x]), water_lines.upper_bound(r[x])); water_lines[l[x]] = x; water_lines[r[x]] = x; } vector<int> upward_counts(n + 1, -1); for(int x = 1; x <= n; x++) { vector<bool> visited(n + 1, false); dfs(x, visited, upward_counts[x]); //std::cerr << x << " " << upward_counts[x] << "\n"; } ll total = 0; for(int x = 1; x <= n; x++) { ll ci = n - 1 - upward_counts[x]; assert(ci >= 0); assert(n - 1 >= ci); ll term = 0; for(int j = 0; j <= ci; j++) { term += (factorials[n - j - 1] * inverse_mod_prime(factorials[ci - j], P)); term %= P; } term *= factorials[ci]; term %= P; //std::cerr << x << " " << ci << " " << term << "\n"; total += term * inverse_mod_prime(factorials[n], P) % P; } std::cout << total % P << "\n"; } |