#include<bits/stdc++.h> using namespace std; #ifdef DEBUG auto&operator <<(auto& o, pair<auto, auto> p) {return o<<"("<<p.first<<", "<<p.second<<")";} auto operator <<(auto& o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";} #define debug(X) cout<<"["#X"]"<<X<<endl; #else #define debug(X) {} #endif #define int long long const int MOD = (int)1e9 + (int)7; vector<int> sil; int fpow(int a, int b) { if(b == 0) return 1; int c = fpow(a, b/2); if(b%2 == 0) return (c*c)%MOD; return (((c*c)%MOD)*a)%MOD; } int newton(int n, int k) { if(k < 0) return 0; return (sil[n] * fpow((sil[k]*sil[n-k])%MOD, MOD-2))%MOD; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; sil.push_back(1); for(int i=1;i<n+10;i++) sil.push_back((sil[i-1]*i)%MOD); vector<pair<int, int> > shlfs(n); for(auto& v : shlfs) cin>>v.first>>v.second; vector<vector<int> > graph(n); for(int i=shlfs.size()-1;i>=0;i--) { for(int j = i-1;j>=0;j--) { if((shlfs[j].first < shlfs[i].first && shlfs[i].first < shlfs[j].second)) { shlfs[i].first = -1; graph[j].push_back(i); } if((shlfs[j].first < shlfs[i].second && shlfs[i].second < shlfs[j].second)) { shlfs[i].second = -1; graph[j].push_back(i); } } } debug(graph); int result = 0; vector<int> vis(n, 0); for(int i=0;i<n;i++) { for(auto& v : vis) v = 0; function<void(int, int&)> dfs = [&](int a, int& k) { vis[a] = 1; k++; for(auto v : graph[a]) if(vis[v] == 0) dfs(v, k); }; int p = 0; dfs(i, p); int q = n - p; p--; result += (sil[n] - (((sil[q]*((sil[p]*p)%MOD))%MOD)*newton(n, p+1))%MOD + MOD)%MOD; result %= MOD; /*debug(i); debug(p); debug(q); for(int j=0;j<n;j++) { result += (((sil[j]*newton(q, j-p))%MOD)*sil[n-j-1])%MOD; result %= MOD; } debug(result);*/ } cout<<(result*fpow(sil[n], MOD-2))%MOD; }
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 | #include<bits/stdc++.h> using namespace std; #ifdef DEBUG auto&operator <<(auto& o, pair<auto, auto> p) {return o<<"("<<p.first<<", "<<p.second<<")";} auto operator <<(auto& o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";} #define debug(X) cout<<"["#X"]"<<X<<endl; #else #define debug(X) {} #endif #define int long long const int MOD = (int)1e9 + (int)7; vector<int> sil; int fpow(int a, int b) { if(b == 0) return 1; int c = fpow(a, b/2); if(b%2 == 0) return (c*c)%MOD; return (((c*c)%MOD)*a)%MOD; } int newton(int n, int k) { if(k < 0) return 0; return (sil[n] * fpow((sil[k]*sil[n-k])%MOD, MOD-2))%MOD; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; sil.push_back(1); for(int i=1;i<n+10;i++) sil.push_back((sil[i-1]*i)%MOD); vector<pair<int, int> > shlfs(n); for(auto& v : shlfs) cin>>v.first>>v.second; vector<vector<int> > graph(n); for(int i=shlfs.size()-1;i>=0;i--) { for(int j = i-1;j>=0;j--) { if((shlfs[j].first < shlfs[i].first && shlfs[i].first < shlfs[j].second)) { shlfs[i].first = -1; graph[j].push_back(i); } if((shlfs[j].first < shlfs[i].second && shlfs[i].second < shlfs[j].second)) { shlfs[i].second = -1; graph[j].push_back(i); } } } debug(graph); int result = 0; vector<int> vis(n, 0); for(int i=0;i<n;i++) { for(auto& v : vis) v = 0; function<void(int, int&)> dfs = [&](int a, int& k) { vis[a] = 1; k++; for(auto v : graph[a]) if(vis[v] == 0) dfs(v, k); }; int p = 0; dfs(i, p); int q = n - p; p--; result += (sil[n] - (((sil[q]*((sil[p]*p)%MOD))%MOD)*newton(n, p+1))%MOD + MOD)%MOD; result %= MOD; /*debug(i); debug(p); debug(q); for(int j=0;j<n;j++) { result += (((sil[j]*newton(q, j-p))%MOD)*sil[n-j-1])%MOD; result %= MOD; } debug(result);*/ } cout<<(result*fpow(sil[n], MOD-2))%MOD; } |