#include <bits/stdc++.h> #define FOR(i, a, b) for(int i =(a); i < (b); ++i) #define re(i, n) FOR(i, 1, n) #define ford(i, a, b) for(int i = (a); i >= (b); --i) #define rep(i, n) for(int i = 0;i <(n); ++i) #define all(x) (x).begin(), (x).end() #define sz(x) (int) (x).size() using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<ll> vll; typedef vector<int> vi; typedef vector<pll> vpll; typedef vector<pii> vpii; const ll inf = 1e18; const int N = 90'000; const int mod = 1e9 + 7; template<int N> struct bit_array { const static int arsz = (N >> 6) + 1; unsigned long long arr[arsz] = {0}; void set(int pos) { arr[pos >> 6] |= (1ull << (pos & 0x0000003F)); } void operator|=(const bit_array<N>& b) { rep(i, arsz) arr[i] |= b.arr[i]; } int count() { int ret = 0; rep(i, arsz) ret += __builtin_popcount(arr[i]); return ret; } }; struct event { int height; int type; }; set<int> sweep; vector<event> events; vector<vi> Adj; bitset<N> dp[N]; ll fact[N]; ll factinv[N]; int dp2[N]; inline ll c(ll n, ll k) { return (((fact[n] * factinv[n-k]) % mod) * factinv[k]) % mod; } ll pmod(ll x, ll exp) { x %= mod; ll ret = 1ll; while (exp) { if (exp & 1) { ret *= x; ret %= mod; } exp >>= 1; x *= x; x %= mod; } return ret; } ll minv(ll x) { return pmod(x, mod - 2); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; events.resize(2*n); Adj.resize(n); fact[0] = 1ll; rep(i, n) { fact[i+1] = fact[i] * (i+1); fact[i+1] %= mod; } rep(i, n + 1) { factinv[i] = minv(fact[i]); } rep(i, 2*n) { events[i].type = -1; } rep(i, n) { int l, r; cin >> l >> r; l--; r--; event start; start.height = i; start.type = 0; event end; end.height = i; end.type = 1; events[l] = start; events[r] = end; } rep(i, 2*n) { if (events[i].type != -1) { int h = events[i].height; auto it = sweep.upper_bound(-h); if (it != sweep.end()) Adj[h].push_back({-(*it)}); if (events[i].type == 0) { sweep.insert(-h); } else { sweep.erase(-h); } } } /*rep(i, n) { cout << (i + 1) << ": "; for (int u : Adj[i]) { cout << (u + 1) << " "; } cout << '\n'; }*/ for (int i = n - 1; i >= 0; i--) { int v = i; dp2[v] = dp[v].count(); dp[v].set(v); for (int u : Adj[v]) dp[u] |= dp[v]; } ll ans = 0ll; rep(i, n) { ll loc = c(n, dp2[i] + 1); loc *= fact[dp2[i]]; loc %= mod; loc *= fact[n-dp2[i] - 1]; loc %= mod; ans += loc; ans %= mod; } cout << ((ans * factinv[n]) % mod) << '\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 | #include <bits/stdc++.h> #define FOR(i, a, b) for(int i =(a); i < (b); ++i) #define re(i, n) FOR(i, 1, n) #define ford(i, a, b) for(int i = (a); i >= (b); --i) #define rep(i, n) for(int i = 0;i <(n); ++i) #define all(x) (x).begin(), (x).end() #define sz(x) (int) (x).size() using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<ll> vll; typedef vector<int> vi; typedef vector<pll> vpll; typedef vector<pii> vpii; const ll inf = 1e18; const int N = 90'000; const int mod = 1e9 + 7; template<int N> struct bit_array { const static int arsz = (N >> 6) + 1; unsigned long long arr[arsz] = {0}; void set(int pos) { arr[pos >> 6] |= (1ull << (pos & 0x0000003F)); } void operator|=(const bit_array<N>& b) { rep(i, arsz) arr[i] |= b.arr[i]; } int count() { int ret = 0; rep(i, arsz) ret += __builtin_popcount(arr[i]); return ret; } }; struct event { int height; int type; }; set<int> sweep; vector<event> events; vector<vi> Adj; bitset<N> dp[N]; ll fact[N]; ll factinv[N]; int dp2[N]; inline ll c(ll n, ll k) { return (((fact[n] * factinv[n-k]) % mod) * factinv[k]) % mod; } ll pmod(ll x, ll exp) { x %= mod; ll ret = 1ll; while (exp) { if (exp & 1) { ret *= x; ret %= mod; } exp >>= 1; x *= x; x %= mod; } return ret; } ll minv(ll x) { return pmod(x, mod - 2); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; events.resize(2*n); Adj.resize(n); fact[0] = 1ll; rep(i, n) { fact[i+1] = fact[i] * (i+1); fact[i+1] %= mod; } rep(i, n + 1) { factinv[i] = minv(fact[i]); } rep(i, 2*n) { events[i].type = -1; } rep(i, n) { int l, r; cin >> l >> r; l--; r--; event start; start.height = i; start.type = 0; event end; end.height = i; end.type = 1; events[l] = start; events[r] = end; } rep(i, 2*n) { if (events[i].type != -1) { int h = events[i].height; auto it = sweep.upper_bound(-h); if (it != sweep.end()) Adj[h].push_back({-(*it)}); if (events[i].type == 0) { sweep.insert(-h); } else { sweep.erase(-h); } } } /*rep(i, n) { cout << (i + 1) << ": "; for (int u : Adj[i]) { cout << (u + 1) << " "; } cout << '\n'; }*/ for (int i = n - 1; i >= 0; i--) { int v = i; dp2[v] = dp[v].count(); dp[v].set(v); for (int u : Adj[v]) dp[u] |= dp[v]; } ll ans = 0ll; rep(i, n) { ll loc = c(n, dp2[i] + 1); loc *= fact[dp2[i]]; loc %= mod; loc *= fact[n-dp2[i] - 1]; loc %= mod; ans += loc; ans %= mod; } cout << ((ans * factinv[n]) % mod) << '\n'; } |