#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = (a); i <= (b); ++i) #define REP(i, n) for(int i = 0; i < (n); ++i) template<class T> int size(T && a) { return (int)(a.size()); } ostream& operator << (ostream &os, string str) { for(char c : str) os << c; return os; } template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << "," << p.second << ')'; } template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) { os << '{'; for(auto it = x.begin(); it != x.end(); ++it) os << *it << (it == prev(x.end()) ? "" : " "); return os << '}'; } template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) { for(auto x : vec) os << "\n " << x; return os; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n' #else # define debug(...) 0 #endif //-------------------------------------------------- constexpr int MOD = int(1e9) + 7; int add(int a, int b) { if (a + b >= MOD) return a + b - MOD; return a + b; } int mul(int a, int b) { return (1LL * a * b) % MOD; } int fast_pow(int a, int p) { if (p == 0) return 1; if (p % 2 == 1) return mul(fast_pow(a, p - 1), a); int ret = fast_pow(a, p / 2); return mul(ret, ret); } int divide(int a, int b) { return mul(a, fast_pow(b, MOD - 2)); } struct BinomCoeff { vector<int> fac, rev; BinomCoeff(int n) { fac = rev = vector<int>(n + 1, 1); FOR(i, 1, n) fac[i] = mul(fac[i - 1], i); rev[n] = divide(1, fac[n]); for(int i = n; i >= 1; i--) rev[i - 1] = mul(rev[i], i); } int operator()(int n, int k) { return mul(fac[n], mul(rev[n - k], rev[k])); } }; const int MAX_N = int(5e5); struct SegmentTree { vector<int> T; int size = 1; SegmentTree(int n) { while (size < n) size *= 2; T.resize(2 * size, -1); } void add(int l, int r, int value) { l += size; r += size; T[l] = T[r] = value; while (l / 2 != r / 2) { if (l % 2 == 0) T[l + 1] = value; if (r % 2 == 1) T[r - 1] = value; l /= 2, r /= 2; } debug(T); } int get(int p) { p += size; int ret = T[p]; while (p /= 2) { ret = max(ret, T[p]); } return ret; } }; using graph_t = vector<array<int, 2>>; graph_t get_dag(const vector<pair<int, int>> &segments) { int n = ssize(segments); graph_t graph(n, {-1, -1}); SegmentTree tree(2 * n); REP (i, n) { auto [l, r] = segments[i]; graph[i][0] = tree.get(l); graph[i][1] = tree.get(r); tree.add(l, r, i); } return graph; } vector<int> get_before_cnt(const graph_t &graph) { int n = ssize(graph); vector<bitset<MAX_N>> before(n); vector<int> answer(n); for (int u = n - 1; u >= 0; --u) { answer[u] = before[u].count(); before[u][u] = 1; for (int v : graph[u]) if (v != -1) before[v] |= before[u]; } return answer; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<pair<int, int>> segments(n); REP (i, n) { int l, r; cin >> l >> r; --l, --r; segments[i] = pair(l, r); } graph_t dag = get_dag(segments); debug(dag); vector<int> before_cnt = get_before_cnt(dag); debug(before_cnt); vector<int> fac(n + 1); fac[0] = 1; FOR (i, 1, n) fac[i] = mul(fac[i - 1], i); BinomCoeff C(n + 1); int answer = 0; REP (u, n) { int c = before_cnt[u]; int res = mul(mul(fac[n - c - 1], fac[c]), C(n, c + 1)); answer = add(answer, res); } cout << divide(answer, fac[n]) << '\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 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 | #include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = (a); i <= (b); ++i) #define REP(i, n) for(int i = 0; i < (n); ++i) template<class T> int size(T && a) { return (int)(a.size()); } ostream& operator << (ostream &os, string str) { for(char c : str) os << c; return os; } template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << "," << p.second << ')'; } template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) { os << '{'; for(auto it = x.begin(); it != x.end(); ++it) os << *it << (it == prev(x.end()) ? "" : " "); return os << '}'; } template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) { for(auto x : vec) os << "\n " << x; return os; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n' #else # define debug(...) 0 #endif //-------------------------------------------------- constexpr int MOD = int(1e9) + 7; int add(int a, int b) { if (a + b >= MOD) return a + b - MOD; return a + b; } int mul(int a, int b) { return (1LL * a * b) % MOD; } int fast_pow(int a, int p) { if (p == 0) return 1; if (p % 2 == 1) return mul(fast_pow(a, p - 1), a); int ret = fast_pow(a, p / 2); return mul(ret, ret); } int divide(int a, int b) { return mul(a, fast_pow(b, MOD - 2)); } struct BinomCoeff { vector<int> fac, rev; BinomCoeff(int n) { fac = rev = vector<int>(n + 1, 1); FOR(i, 1, n) fac[i] = mul(fac[i - 1], i); rev[n] = divide(1, fac[n]); for(int i = n; i >= 1; i--) rev[i - 1] = mul(rev[i], i); } int operator()(int n, int k) { return mul(fac[n], mul(rev[n - k], rev[k])); } }; const int MAX_N = int(5e5); struct SegmentTree { vector<int> T; int size = 1; SegmentTree(int n) { while (size < n) size *= 2; T.resize(2 * size, -1); } void add(int l, int r, int value) { l += size; r += size; T[l] = T[r] = value; while (l / 2 != r / 2) { if (l % 2 == 0) T[l + 1] = value; if (r % 2 == 1) T[r - 1] = value; l /= 2, r /= 2; } debug(T); } int get(int p) { p += size; int ret = T[p]; while (p /= 2) { ret = max(ret, T[p]); } return ret; } }; using graph_t = vector<array<int, 2>>; graph_t get_dag(const vector<pair<int, int>> &segments) { int n = ssize(segments); graph_t graph(n, {-1, -1}); SegmentTree tree(2 * n); REP (i, n) { auto [l, r] = segments[i]; graph[i][0] = tree.get(l); graph[i][1] = tree.get(r); tree.add(l, r, i); } return graph; } vector<int> get_before_cnt(const graph_t &graph) { int n = ssize(graph); vector<bitset<MAX_N>> before(n); vector<int> answer(n); for (int u = n - 1; u >= 0; --u) { answer[u] = before[u].count(); before[u][u] = 1; for (int v : graph[u]) if (v != -1) before[v] |= before[u]; } return answer; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<pair<int, int>> segments(n); REP (i, n) { int l, r; cin >> l >> r; --l, --r; segments[i] = pair(l, r); } graph_t dag = get_dag(segments); debug(dag); vector<int> before_cnt = get_before_cnt(dag); debug(before_cnt); vector<int> fac(n + 1); fac[0] = 1; FOR (i, 1, n) fac[i] = mul(fac[i - 1], i); BinomCoeff C(n + 1); int answer = 0; REP (u, n) { int c = before_cnt[u]; int res = mul(mul(fac[n - c - 1], fac[c]), C(n, c + 1)); answer = add(answer, res); } cout << divide(answer, fac[n]) << '\n'; } |