#include <bits/stdc++.h> #define LLINF 100000000000000007 using namespace std; typedef long long ll; typedef string str; template <typename T> using vec = vector<T>; const ll MOD = 1e9 + 7; // const ll MOD2 = 1000992299; ll powm(ll a, ll n); ll multm(ll a, ll b); ll divm(ll a, ll b); ll n, N; // {age, v} vec<int> seg_tree; vec<vec<int>> tG; vec<ll> facts; void init(); void prep_factorials(); void change_on_range(int cur, int left, int right, int lOb, int rOb, int v) { if(left == lOb && right == rOb) { seg_tree[cur] = v; return; } int mid = (lOb + rOb) / 2; if(right <= mid) return change_on_range(cur * 2, left, right, lOb, mid, v); if(left > mid) return change_on_range(cur * 2 + 1, left, right, mid + 1, rOb, v); change_on_range(cur * 2, left, mid, lOb, mid, v); change_on_range(cur * 2 + 1, mid + 1, right, mid + 1, rOb, v); } int get_latest_node(int v) { v += N - 1; int node = 0; while(v > 0) { node = max(node, seg_tree[v]); v /= 2; } return node; } ll mult_ft(ll from, ll to) { return divm(facts[to], facts[from - 1]); } ll combs_heura(ll on_right) { ll frow = multm(mult_ft(1, n - on_right - 1), mult_ft(1, on_right)); return divm(multm(frow, mult_ft(on_right + 2, n)), mult_ft(1, (n - on_right - 1))); } int bfs_count(int s) { const int LIM = 5 * 1e5 + 7; queue<int> q; bitset<LIM> vis; q.push(s); vis[s] = true; int count = 0; while(!q.empty()) { int v = q.front(); q.pop(); for(auto e : tG[v]) { if(vis[e]) continue; q.push(e); count++; vis[e] = true; } } return count; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin>>n; init(); prep_factorials(); for(int i = 1; i <= n; i++) { int l, r; cin>>l>>r; int gl = get_latest_node(l); int gr = get_latest_node(r); // cout<<"i: "<<i<<"\n"; // cout<<"(gr, gl): ("<<gr<<","<<gl<<")\n"; if(gl == gr) tG[gl].push_back(i); else { tG[gl].push_back(i); tG[gr].push_back(i); } change_on_range(1, l, r, 1, N, i); } ll wyn = 0; for(int i = 1; i <= n; i++) { ll c = bfs_count(i); // cout<<"c: "<<c<<"\n"; // cout<<"ch(c): "<<combs_heura(c)<<"\n"; wyn = (wyn + combs_heura(c)) % MOD; } cout<<divm(wyn, facts[n]); cout.flush(); return 0; } void init() { tG = vec<vec<int>>(n + 1); N = 1; while(N < 2 * n) N *= 2; seg_tree = vec<int>(2 * N, 0); } void prep_factorials() { facts = vec<ll>(n + 7); facts[0] = 1; for(ll i = 1; i < n + 7; i++) facts[i] = multm(facts[i - 1], i); } ll powm(ll a, ll n) { ll w = 1; while (n > 0) { if (n % 2 == 1) w = (w * a) % MOD; a = (a * a) % MOD; n /= 2; } return w; } ll multm(ll a, ll b) { return (a * b) % MOD; } ll divm(ll a, ll b) { return multm(a, powm(b, MOD - 2)); }
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 | #include <bits/stdc++.h> #define LLINF 100000000000000007 using namespace std; typedef long long ll; typedef string str; template <typename T> using vec = vector<T>; const ll MOD = 1e9 + 7; // const ll MOD2 = 1000992299; ll powm(ll a, ll n); ll multm(ll a, ll b); ll divm(ll a, ll b); ll n, N; // {age, v} vec<int> seg_tree; vec<vec<int>> tG; vec<ll> facts; void init(); void prep_factorials(); void change_on_range(int cur, int left, int right, int lOb, int rOb, int v) { if(left == lOb && right == rOb) { seg_tree[cur] = v; return; } int mid = (lOb + rOb) / 2; if(right <= mid) return change_on_range(cur * 2, left, right, lOb, mid, v); if(left > mid) return change_on_range(cur * 2 + 1, left, right, mid + 1, rOb, v); change_on_range(cur * 2, left, mid, lOb, mid, v); change_on_range(cur * 2 + 1, mid + 1, right, mid + 1, rOb, v); } int get_latest_node(int v) { v += N - 1; int node = 0; while(v > 0) { node = max(node, seg_tree[v]); v /= 2; } return node; } ll mult_ft(ll from, ll to) { return divm(facts[to], facts[from - 1]); } ll combs_heura(ll on_right) { ll frow = multm(mult_ft(1, n - on_right - 1), mult_ft(1, on_right)); return divm(multm(frow, mult_ft(on_right + 2, n)), mult_ft(1, (n - on_right - 1))); } int bfs_count(int s) { const int LIM = 5 * 1e5 + 7; queue<int> q; bitset<LIM> vis; q.push(s); vis[s] = true; int count = 0; while(!q.empty()) { int v = q.front(); q.pop(); for(auto e : tG[v]) { if(vis[e]) continue; q.push(e); count++; vis[e] = true; } } return count; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin>>n; init(); prep_factorials(); for(int i = 1; i <= n; i++) { int l, r; cin>>l>>r; int gl = get_latest_node(l); int gr = get_latest_node(r); // cout<<"i: "<<i<<"\n"; // cout<<"(gr, gl): ("<<gr<<","<<gl<<")\n"; if(gl == gr) tG[gl].push_back(i); else { tG[gl].push_back(i); tG[gr].push_back(i); } change_on_range(1, l, r, 1, N, i); } ll wyn = 0; for(int i = 1; i <= n; i++) { ll c = bfs_count(i); // cout<<"c: "<<c<<"\n"; // cout<<"ch(c): "<<combs_heura(c)<<"\n"; wyn = (wyn + combs_heura(c)) % MOD; } cout<<divm(wyn, facts[n]); cout.flush(); return 0; } void init() { tG = vec<vec<int>>(n + 1); N = 1; while(N < 2 * n) N *= 2; seg_tree = vec<int>(2 * N, 0); } void prep_factorials() { facts = vec<ll>(n + 7); facts[0] = 1; for(ll i = 1; i < n + 7; i++) facts[i] = multm(facts[i - 1], i); } ll powm(ll a, ll n) { ll w = 1; while (n > 0) { if (n % 2 == 1) w = (w * a) % MOD; a = (a * a) % MOD; n /= 2; } return w; } ll multm(ll a, ll b) { return (a * b) % MOD; } ll divm(ll a, ll b) { return multm(a, powm(b, MOD - 2)); } |