#pragma GCC optimize("unroll-loops") #pragma GCC target("popcnt,avx,tune=native") #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define ll long long #ifdef LOC auto &operator<<(auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator<<(auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << __LINE__ << ": [" #x "]: ", dump(x) #else #define debug(...) 0 #endif struct Tree{ private: int lrSize = 2; vector<int> v; vector<int> time; int t = 1; void setRange(int a, int b, int l, int r, int pos, int val){ if(r < a || b < l)return; if(a <= l && r <= b){ v[pos] = val; time[pos] = t++; return; } int m = (l + r)/2; setRange(a, b, l, m, 2*pos, val); setRange(a, b, m+1, r, 2*pos + 1, val); } public: Tree(int n){ while(lrSize < n)lrSize *= 2; //debug(lrSize); v.resize(2*lrSize, -1); time.resize(2*lrSize, 0); } void print(){ for(int h = 1; h <= lrSize; h*=2){ for(int i = h; i < 2*h; i++)cout<<v[i]<<" "; cout<<"\n"; } } void set(int a, int b, int val){ setRange(a, b, 1, lrSize, 1, val); //print(); //debug(v); } int query(int pos){ pos += lrSize - 1; int ans = v[pos]; int tim = time[pos]; //debug(pos); while(pos > 1){ pos /= 2; //debug(pos); if(time[pos] > tim){ ans = v[pos]; tim = time[pos]; } } return ans; } }; const int K = 512; const ll MOD = 1e9 + 7; long long power(long long a, long long b){ long long res = 1; while(b){ while(!(b&1)){ a = (a * a) % MOD; b>>=1; } res = (res * a) % MOD; b--; } return res; } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); int n; cin>>n; vector<int> l(n); vector<int> r(n); Tree tree(2*n); vector<ll> inv(n+1); vector<ll> fact(n+1); fact[0] = 1; for(ll i = 1; i <= n; i++)fact[i] = (fact[i-1] * i) % MOD; ll x = power(fact[n], MOD - 2); for(ll i = n; i >= 1; i--){ inv[i] = (x * fact[i-1]) % MOD; x = (x * i) % MOD; } rep(i, n){ int a, b; cin>>a>>b; //debug(a, b); l[n-1-i] = tree.query(a); r[n-1-i] = tree.query(b); //debug(l[n-1-i], r[n-1-i]); tree.set(a, b, n-1-i); } //debug(l); //debug(r); vector<int> cnt(n, 0); vector<bitset<K>> v(n); for(int i =0; i < n; i+= K){ for(int j= i; j < i + K && j < n; j++){ v[j][j-i] = 1; } for(int j = i; j < n; j++){ if(l[j] != -1){ v[l[j]] |= v[j]; } if(r[j] != -1 && r[j] != l[j]){ v[r[j]] |= v[j]; } cnt[j] += v[j].count(); } for(int j= i; j < n; j++){ v[j].reset(); } } //debug(cnt); long long ans = 0; rep(i, n)ans = (ans + inv[cnt[i]]) % MOD; cout<<ans<<"\n"; 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 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 | #pragma GCC optimize("unroll-loops") #pragma GCC target("popcnt,avx,tune=native") #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define ll long long #ifdef LOC auto &operator<<(auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator<<(auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << __LINE__ << ": [" #x "]: ", dump(x) #else #define debug(...) 0 #endif struct Tree{ private: int lrSize = 2; vector<int> v; vector<int> time; int t = 1; void setRange(int a, int b, int l, int r, int pos, int val){ if(r < a || b < l)return; if(a <= l && r <= b){ v[pos] = val; time[pos] = t++; return; } int m = (l + r)/2; setRange(a, b, l, m, 2*pos, val); setRange(a, b, m+1, r, 2*pos + 1, val); } public: Tree(int n){ while(lrSize < n)lrSize *= 2; //debug(lrSize); v.resize(2*lrSize, -1); time.resize(2*lrSize, 0); } void print(){ for(int h = 1; h <= lrSize; h*=2){ for(int i = h; i < 2*h; i++)cout<<v[i]<<" "; cout<<"\n"; } } void set(int a, int b, int val){ setRange(a, b, 1, lrSize, 1, val); //print(); //debug(v); } int query(int pos){ pos += lrSize - 1; int ans = v[pos]; int tim = time[pos]; //debug(pos); while(pos > 1){ pos /= 2; //debug(pos); if(time[pos] > tim){ ans = v[pos]; tim = time[pos]; } } return ans; } }; const int K = 512; const ll MOD = 1e9 + 7; long long power(long long a, long long b){ long long res = 1; while(b){ while(!(b&1)){ a = (a * a) % MOD; b>>=1; } res = (res * a) % MOD; b--; } return res; } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); int n; cin>>n; vector<int> l(n); vector<int> r(n); Tree tree(2*n); vector<ll> inv(n+1); vector<ll> fact(n+1); fact[0] = 1; for(ll i = 1; i <= n; i++)fact[i] = (fact[i-1] * i) % MOD; ll x = power(fact[n], MOD - 2); for(ll i = n; i >= 1; i--){ inv[i] = (x * fact[i-1]) % MOD; x = (x * i) % MOD; } rep(i, n){ int a, b; cin>>a>>b; //debug(a, b); l[n-1-i] = tree.query(a); r[n-1-i] = tree.query(b); //debug(l[n-1-i], r[n-1-i]); tree.set(a, b, n-1-i); } //debug(l); //debug(r); vector<int> cnt(n, 0); vector<bitset<K>> v(n); for(int i =0; i < n; i+= K){ for(int j= i; j < i + K && j < n; j++){ v[j][j-i] = 1; } for(int j = i; j < n; j++){ if(l[j] != -1){ v[l[j]] |= v[j]; } if(r[j] != -1 && r[j] != l[j]){ v[r[j]] |= v[j]; } cnt[j] += v[j].count(); } for(int j= i; j < n; j++){ v[j].reset(); } } //debug(cnt); long long ans = 0; rep(i, n)ans = (ans + inv[cnt[i]]) % MOD; cout<<ans<<"\n"; return 0; } |