// Author: Kajetan Ramsza #include "bits/stdc++.h" using namespace std; #define rep(i, a, b) for(int i = (a); i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; } template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it; for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; } void dbg_out() { cerr<<'\n'; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); } #ifdef DEBUG #define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif const ll mod = 1000000007; ll modpow(ll b, ll e) { ll ans = 1; for (; e; b = b * b % mod, e /= 2) if (e & 1) ans = ans * b % mod; return ans; } const int limit = int(5e5) + 7; ll fact[limit], inv_fact[limit]; void preprocess() { fact[0] = 1; rep(i,1,limit) fact[i] = fact[i-1] * i % mod; inv_fact[limit-1] = modpow(fact[limit-1], mod-2); for(ll i=limit-2;i>=0;i--) inv_fact[i] = inv_fact[i+1] * (i+1) % mod; } ll choose(int n, int k) { return (fact[n] * inv_fact[k] % mod) * inv_fact[n-k] % mod; } int n; vector<pii> vec; vector<bool> vis; vi siz; vector<vi> g; int dfs_siz(int v) { int res = 1; vis[v] = true; for(auto e : g[v]) if(!vis[e]) res += dfs_siz(e); return res; } ll calc_res(ll k) { if(k == 0) return fact[n]; ll res = 0; for(ll i=k;i<=n-1;i++) res = (res + fact[i-1] * inv_fact[i-k] % mod * (n-i)) % mod; res = (res * fact[n-1-k]) % mod; res = (res * k) % mod; return res; } ll result = 0; int main() { ios_base::sync_with_stdio(0); cin.tie(0); preprocess(); cin>>n; vec.resize(n); rep(i,0,n) cin>>vec[i].first>>vec[i].second; g.resize(n); rep(i,0,n) { for(int j=i-1;j>=0;j--) if(vec[j].first < vec[i].first && vec[i].first < vec[j].second) { g[j].push_back(i); break; } for(int j=i-1;j>=0;j--) if(vec[j].first < vec[i].second && vec[i].second < vec[j].second) { g[j].push_back(i); break; } } dbg(g); vis.resize(n); siz.resize(n); rep(i,0,n) { fill(all(vis), 0); siz[i] = dfs_siz(i); result = (result + calc_res(siz[i] - 1)) % mod; } dbg(result); cout<<(result * inv_fact[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 | // Author: Kajetan Ramsza #include "bits/stdc++.h" using namespace std; #define rep(i, a, b) for(int i = (a); i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; } template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it; for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; } void dbg_out() { cerr<<'\n'; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); } #ifdef DEBUG #define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif const ll mod = 1000000007; ll modpow(ll b, ll e) { ll ans = 1; for (; e; b = b * b % mod, e /= 2) if (e & 1) ans = ans * b % mod; return ans; } const int limit = int(5e5) + 7; ll fact[limit], inv_fact[limit]; void preprocess() { fact[0] = 1; rep(i,1,limit) fact[i] = fact[i-1] * i % mod; inv_fact[limit-1] = modpow(fact[limit-1], mod-2); for(ll i=limit-2;i>=0;i--) inv_fact[i] = inv_fact[i+1] * (i+1) % mod; } ll choose(int n, int k) { return (fact[n] * inv_fact[k] % mod) * inv_fact[n-k] % mod; } int n; vector<pii> vec; vector<bool> vis; vi siz; vector<vi> g; int dfs_siz(int v) { int res = 1; vis[v] = true; for(auto e : g[v]) if(!vis[e]) res += dfs_siz(e); return res; } ll calc_res(ll k) { if(k == 0) return fact[n]; ll res = 0; for(ll i=k;i<=n-1;i++) res = (res + fact[i-1] * inv_fact[i-k] % mod * (n-i)) % mod; res = (res * fact[n-1-k]) % mod; res = (res * k) % mod; return res; } ll result = 0; int main() { ios_base::sync_with_stdio(0); cin.tie(0); preprocess(); cin>>n; vec.resize(n); rep(i,0,n) cin>>vec[i].first>>vec[i].second; g.resize(n); rep(i,0,n) { for(int j=i-1;j>=0;j--) if(vec[j].first < vec[i].first && vec[i].first < vec[j].second) { g[j].push_back(i); break; } for(int j=i-1;j>=0;j--) if(vec[j].first < vec[i].second && vec[i].second < vec[j].second) { g[j].push_back(i); break; } } dbg(g); vis.resize(n); siz.resize(n); rep(i,0,n) { fill(all(vis), 0); siz[i] = dfs_siz(i); result = (result + calc_res(siz[i] - 1)) % mod; } dbg(result); cout<<(result * inv_fact[n] % mod)<<'\n'; } |