#include <cstdio> #include <algorithm> #include <set> using namespace std; pair<int, int> tab[500001]; int p[500001]; int vis[500001]; set<int> z[500001]; int dodane[500001]; int MOD = 1000000007; long long power(long long a, long long n) { long long r = 1; while (n > 0) { if (n % 2 == 1) r *= a; r %= MOD; n /= 2; a = a * a % MOD; } return r; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d %d", &tab[i].first, &tab[i].second); p[i] = i; } z[0].insert(0); for (int i = 1; i < n; i++) { z[i].insert(i); for (int j = i - 1; j >= 0; j--) { int l = tab[i].first; int r = tab[i].second; int l2 = tab[j].first; int r2 = tab[j].second; if (j == i - 1) { if (l < r2 && l > l2) z[i].insert(j); if (r > l2 && r < r2) z[i].insert(j); continue; } bool ok = true; for (int k = j + 1; k < i; k++) if (l < r2 && l > l2 && (l > tab[k].second || l < tab[k].first)); else ok = false; if (ok) z[i].insert(j); ok = true; for (int k = j + 1; k < i; k++) if (r > l2 && r < r2 && (r > tab[k].second || r < tab[k].first)); else ok = false; if (ok) z[i].insert(j); } int cnt = 0; for (auto it : z[i]) { for (auto it2 : z[it]) { dodane[cnt] = it2; cnt++; } } for (int j = 0; j < cnt; j++) { z[i].insert(dodane[j]); } } int a = 0; int b = 0; do { a++; for (int i = 0; i < n; i++) { vis[i] = 0; } int ret = 0; for (int i = 0; i < n; i++) { if (!vis[p[i]]) { vis[p[i]] = 1; ret++; for (auto it : z[p[i]]) { if (!vis[it]) { vis[it] = 1; } } } } b += ret; } while (next_permutation(p, p + n)); printf("%d\n", 1LL * power(a, MOD - 2) * b % MOD); 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 | #include <cstdio> #include <algorithm> #include <set> using namespace std; pair<int, int> tab[500001]; int p[500001]; int vis[500001]; set<int> z[500001]; int dodane[500001]; int MOD = 1000000007; long long power(long long a, long long n) { long long r = 1; while (n > 0) { if (n % 2 == 1) r *= a; r %= MOD; n /= 2; a = a * a % MOD; } return r; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d %d", &tab[i].first, &tab[i].second); p[i] = i; } z[0].insert(0); for (int i = 1; i < n; i++) { z[i].insert(i); for (int j = i - 1; j >= 0; j--) { int l = tab[i].first; int r = tab[i].second; int l2 = tab[j].first; int r2 = tab[j].second; if (j == i - 1) { if (l < r2 && l > l2) z[i].insert(j); if (r > l2 && r < r2) z[i].insert(j); continue; } bool ok = true; for (int k = j + 1; k < i; k++) if (l < r2 && l > l2 && (l > tab[k].second || l < tab[k].first)); else ok = false; if (ok) z[i].insert(j); ok = true; for (int k = j + 1; k < i; k++) if (r > l2 && r < r2 && (r > tab[k].second || r < tab[k].first)); else ok = false; if (ok) z[i].insert(j); } int cnt = 0; for (auto it : z[i]) { for (auto it2 : z[it]) { dodane[cnt] = it2; cnt++; } } for (int j = 0; j < cnt; j++) { z[i].insert(dodane[j]); } } int a = 0; int b = 0; do { a++; for (int i = 0; i < n; i++) { vis[i] = 0; } int ret = 0; for (int i = 0; i < n; i++) { if (!vis[p[i]]) { vis[p[i]] = 1; ret++; for (auto it : z[p[i]]) { if (!vis[it]) { vis[it] = 1; } } } } b += ret; } while (next_permutation(p, p + n)); printf("%d\n", 1LL * power(a, MOD - 2) * b % MOD); return 0; } |