#include "bits/stdc++.h" using namespace std; using ll = long long; const int INF = 1e9 + 7; #define all(x) x.begin(), x.end() #define pb push_back #define cmax(x, y) (x = max(x, y)) #define cmin(x, y) (x = min(x, y)) #ifdef LOCAL #include "debug.cpp" #else #define debug(x...) #endif template<const int &MOD> struct _m_int { int val; _m_int(int64_t v = 0) { if (v < 0) v = v % MOD + MOD; if (v >= MOD) v %= MOD; val = int(v); } _m_int(uint64_t v) { if (v >= MOD) v %= MOD; val = int(v); } _m_int(int v) : _m_int(int64_t(v)) {} _m_int(unsigned v) : _m_int(uint64_t(v)) {} explicit operator int() const { return val; } explicit operator unsigned() const { return val; } explicit operator int64_t() const { return val; } explicit operator uint64_t() const { return val; } explicit operator double() const { return val; } explicit operator long double() const { return val; } _m_int& operator+=(const _m_int &other) { val -= MOD - other.val; if (val < 0) val += MOD; return *this; } _m_int& operator-=(const _m_int &other) { val -= other.val; if (val < 0) val += MOD; return *this; } static unsigned fast_mod(uint64_t x, unsigned m = MOD) { #if !defined(_WIN32) || defined(_WIN64) return unsigned(x % m); #endif // Optimized mod for Codeforces 32-bit machines. // x must be less than 2^32 * m for this to work, so that x / m fits in an unsigned 32-bit int. unsigned x_high = unsigned(x >> 32), x_low = unsigned(x); unsigned quot, rem; asm("divl %4\n" : "=a" (quot), "=d" (rem) : "d" (x_high), "a" (x_low), "r" (m)); return rem; } _m_int& operator*=(const _m_int &other) { val = fast_mod(uint64_t(val) * other.val); return *this; } _m_int& operator/=(const _m_int &other) { return *this *= other.inv(); } friend _m_int operator+(const _m_int &a, const _m_int &b) { return _m_int(a) += b; } friend _m_int operator-(const _m_int &a, const _m_int &b) { return _m_int(a) -= b; } friend _m_int operator*(const _m_int &a, const _m_int &b) { return _m_int(a) *= b; } friend _m_int operator/(const _m_int &a, const _m_int &b) { return _m_int(a) /= b; } _m_int& operator++() { val = val == MOD - 1 ? 0 : val + 1; return *this; } _m_int& operator--() { val = val == 0 ? MOD - 1 : val - 1; return *this; } _m_int operator++(int) { _m_int before = *this; ++*this; return before; } _m_int operator--(int) { _m_int before = *this; --*this; return before; } _m_int operator-() const { return val == 0 ? 0 : MOD - val; } friend bool operator==(const _m_int &a, const _m_int &b) { return a.val == b.val; } friend bool operator!=(const _m_int &a, const _m_int &b) { return a.val != b.val; } friend bool operator<(const _m_int &a, const _m_int &b) { return a.val < b.val; } friend bool operator>(const _m_int &a, const _m_int &b) { return a.val > b.val; } friend bool operator<=(const _m_int &a, const _m_int &b) { return a.val <= b.val; } friend bool operator>=(const _m_int &a, const _m_int &b) { return a.val >= b.val; } static const int SAVE_INV = int(1e6) + 5; static _m_int save_inv[SAVE_INV]; static void prepare_inv() { // Ensures that MOD is prime, which is necessary for the inverse algorithm below. for (int64_t p = 2; p * p <= MOD; p += p % 2 + 1) assert(MOD % p != 0); save_inv[0] = 0; save_inv[1] = 1; for (int i = 2; i < SAVE_INV; i++) save_inv[i] = save_inv[MOD % i] * (MOD - MOD / i); } _m_int inv() const { if (save_inv[1] == 0) prepare_inv(); if (val < SAVE_INV) return save_inv[val]; _m_int product = 1; int v = val; do { product *= MOD - MOD / v; v = MOD % v; } while (v >= SAVE_INV); return product * save_inv[v]; } _m_int pow(int64_t p) const { if (p < 0) return inv().pow(-p); _m_int a = *this, result = 1; while (p > 0) { if (p & 1) result *= a; p >>= 1; if (p > 0) a *= a; } return result; } friend ostream& operator<<(ostream &os, const _m_int &m) { return os << m.val; } friend istream& operator>>(istream &is, _m_int &m) { int64_t v; is >> v; m = _m_int(v); return is; } }; template<const int &MOD> _m_int<MOD> _m_int<MOD>::save_inv[_m_int<MOD>::SAVE_INV]; const int MOD = 1000000007; using mint = _m_int<MOD>; const int MAXN = 5e5; mint factorial[MAXN + 5]; int ile[MAXN + 5], height[2 * MAXN + 5]; mint newtwon(int a, int b) { if (b > a) return 0; return factorial[a] / (factorial[b] * factorial[a - b]); } void solve() { int n; cin >> n; vector <pair <int, int>> v(n + 1); for (int i = n; i >= 1; i--) { cin >> v[i].first >> v[i].second; } vector <set <int>> st(n + 1); for (int i = 1; i <= n; i++) { int l = -1, r = -1; for (int j = i + 1; j <= n; j++) { if (l == -1) { if (v[j].first < v[i].first && v[i].first < v[j].second) l = j; } if (r == -1) { if (v[j].first < v[i].second && v[i].second < v[j].second) r = j; } if (r != -1 && r != -1) break; } for (auto &x : st[i]) { if (l != -1) st[l].insert(x); if (r != -1) st[r].insert(x); } if (l != -1) st[l].insert(i); if (r != -1) st[r].insert(i); } for (int i = 1; i <= n; i++) ile[i] = st[i].size(); mint licznik = 0; for (int i = 1; i <= n; i++) { int s = ile[i] + 1; mint xd = 0; for (int j = 1, sgn = 1; j < s; j++) { xd += newtwon(s - 1, j) * newtwon(n, j + 1) * factorial[j] * factorial[n - j - 1] * sgn; sgn *= -1; } licznik += factorial[n] - xd; } cout << licznik / factorial[n] << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); factorial[0] = 1; for (int i = 1; i <= MAXN; i++) { factorial[i] = factorial[i - 1] * i; } int t = 1; // cin >> t; while (t--) solve(); }
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | #include "bits/stdc++.h" using namespace std; using ll = long long; const int INF = 1e9 + 7; #define all(x) x.begin(), x.end() #define pb push_back #define cmax(x, y) (x = max(x, y)) #define cmin(x, y) (x = min(x, y)) #ifdef LOCAL #include "debug.cpp" #else #define debug(x...) #endif template<const int &MOD> struct _m_int { int val; _m_int(int64_t v = 0) { if (v < 0) v = v % MOD + MOD; if (v >= MOD) v %= MOD; val = int(v); } _m_int(uint64_t v) { if (v >= MOD) v %= MOD; val = int(v); } _m_int(int v) : _m_int(int64_t(v)) {} _m_int(unsigned v) : _m_int(uint64_t(v)) {} explicit operator int() const { return val; } explicit operator unsigned() const { return val; } explicit operator int64_t() const { return val; } explicit operator uint64_t() const { return val; } explicit operator double() const { return val; } explicit operator long double() const { return val; } _m_int& operator+=(const _m_int &other) { val -= MOD - other.val; if (val < 0) val += MOD; return *this; } _m_int& operator-=(const _m_int &other) { val -= other.val; if (val < 0) val += MOD; return *this; } static unsigned fast_mod(uint64_t x, unsigned m = MOD) { #if !defined(_WIN32) || defined(_WIN64) return unsigned(x % m); #endif // Optimized mod for Codeforces 32-bit machines. // x must be less than 2^32 * m for this to work, so that x / m fits in an unsigned 32-bit int. unsigned x_high = unsigned(x >> 32), x_low = unsigned(x); unsigned quot, rem; asm("divl %4\n" : "=a" (quot), "=d" (rem) : "d" (x_high), "a" (x_low), "r" (m)); return rem; } _m_int& operator*=(const _m_int &other) { val = fast_mod(uint64_t(val) * other.val); return *this; } _m_int& operator/=(const _m_int &other) { return *this *= other.inv(); } friend _m_int operator+(const _m_int &a, const _m_int &b) { return _m_int(a) += b; } friend _m_int operator-(const _m_int &a, const _m_int &b) { return _m_int(a) -= b; } friend _m_int operator*(const _m_int &a, const _m_int &b) { return _m_int(a) *= b; } friend _m_int operator/(const _m_int &a, const _m_int &b) { return _m_int(a) /= b; } _m_int& operator++() { val = val == MOD - 1 ? 0 : val + 1; return *this; } _m_int& operator--() { val = val == 0 ? MOD - 1 : val - 1; return *this; } _m_int operator++(int) { _m_int before = *this; ++*this; return before; } _m_int operator--(int) { _m_int before = *this; --*this; return before; } _m_int operator-() const { return val == 0 ? 0 : MOD - val; } friend bool operator==(const _m_int &a, const _m_int &b) { return a.val == b.val; } friend bool operator!=(const _m_int &a, const _m_int &b) { return a.val != b.val; } friend bool operator<(const _m_int &a, const _m_int &b) { return a.val < b.val; } friend bool operator>(const _m_int &a, const _m_int &b) { return a.val > b.val; } friend bool operator<=(const _m_int &a, const _m_int &b) { return a.val <= b.val; } friend bool operator>=(const _m_int &a, const _m_int &b) { return a.val >= b.val; } static const int SAVE_INV = int(1e6) + 5; static _m_int save_inv[SAVE_INV]; static void prepare_inv() { // Ensures that MOD is prime, which is necessary for the inverse algorithm below. for (int64_t p = 2; p * p <= MOD; p += p % 2 + 1) assert(MOD % p != 0); save_inv[0] = 0; save_inv[1] = 1; for (int i = 2; i < SAVE_INV; i++) save_inv[i] = save_inv[MOD % i] * (MOD - MOD / i); } _m_int inv() const { if (save_inv[1] == 0) prepare_inv(); if (val < SAVE_INV) return save_inv[val]; _m_int product = 1; int v = val; do { product *= MOD - MOD / v; v = MOD % v; } while (v >= SAVE_INV); return product * save_inv[v]; } _m_int pow(int64_t p) const { if (p < 0) return inv().pow(-p); _m_int a = *this, result = 1; while (p > 0) { if (p & 1) result *= a; p >>= 1; if (p > 0) a *= a; } return result; } friend ostream& operator<<(ostream &os, const _m_int &m) { return os << m.val; } friend istream& operator>>(istream &is, _m_int &m) { int64_t v; is >> v; m = _m_int(v); return is; } }; template<const int &MOD> _m_int<MOD> _m_int<MOD>::save_inv[_m_int<MOD>::SAVE_INV]; const int MOD = 1000000007; using mint = _m_int<MOD>; const int MAXN = 5e5; mint factorial[MAXN + 5]; int ile[MAXN + 5], height[2 * MAXN + 5]; mint newtwon(int a, int b) { if (b > a) return 0; return factorial[a] / (factorial[b] * factorial[a - b]); } void solve() { int n; cin >> n; vector <pair <int, int>> v(n + 1); for (int i = n; i >= 1; i--) { cin >> v[i].first >> v[i].second; } vector <set <int>> st(n + 1); for (int i = 1; i <= n; i++) { int l = -1, r = -1; for (int j = i + 1; j <= n; j++) { if (l == -1) { if (v[j].first < v[i].first && v[i].first < v[j].second) l = j; } if (r == -1) { if (v[j].first < v[i].second && v[i].second < v[j].second) r = j; } if (r != -1 && r != -1) break; } for (auto &x : st[i]) { if (l != -1) st[l].insert(x); if (r != -1) st[r].insert(x); } if (l != -1) st[l].insert(i); if (r != -1) st[r].insert(i); } for (int i = 1; i <= n; i++) ile[i] = st[i].size(); mint licznik = 0; for (int i = 1; i <= n; i++) { int s = ile[i] + 1; mint xd = 0; for (int j = 1, sgn = 1; j < s; j++) { xd += newtwon(s - 1, j) * newtwon(n, j + 1) * factorial[j] * factorial[n - j - 1] * sgn; sgn *= -1; } licznik += factorial[n] - xd; } cout << licznik / factorial[n] << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); factorial[0] = 1; for (int i = 1; i <= MAXN; i++) { factorial[i] = factorial[i - 1] * i; } int t = 1; // cin >> t; while (t--) solve(); } |