#include <bits/stdc++.h> using namespace std; #define int long long #define ll long long #define ld long double #define pb push_back #define nd second #define st first #define sz size #define forr(i, n) for(int i=1;i<=n;i++) const ll infl=1e18+90; const int inf=1e9+90; const int roz=2e5+93; const int mod=1e9+7; int sil[roz], odwrot[roz]; long long binpow(long long a, long long b, long long mod) { a %= mod; long long res = 1; while (b > 0) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } void silnie() { sil[0]=1; odwrot[0]=1; for(int i=1;i<=roz-60;i++) { sil[i]=sil[i-1]*i; sil[i]%=mod; odwrot[i]=binpow(sil[i], mod-2, mod); } } int fun(int a, int b) { return ((sil[a]*odwrot[b])%mod*odwrot[a-b])%mod; } set<pair<int,int> > S; set<int> graf[roz]; vector<pair<int,int> > wek, do_wyj; int ile[roz], odw[roz]; void dfs(int u, int x) { //cerr<<"wchodze dla u = "<<u<<" x = "<<x<<"\n"; ile[u]++; odw[u]=x; for(auto v:graf[u]) { if(odw[v]!=x) dfs(v, x); } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin>>n; silnie(); for(int i=1;i<=n;i++) { int a, b; cin>>a>>b; wek.pb({a, b}); } reverse(wek.begin(), wek.end()); for(int i=1;i<=n;i++) { int a=wek[i-1].st, b=wek[i-1].nd; for(auto [c, d]:S) { if(c>a&&c<b) { graf[d].insert(i); do_wyj.pb({c, d}); } } for(auto x:do_wyj) { S.erase(x); } do_wyj.clear(); S.insert({a, i}); S.insert({b, i}); } for(int i=1;i<=n;i++) { dfs(i, i); } int wyn=0; for(int i=1;i<=n;i++) { int y=ile[i]-1; for(int j=1;j<=n-y;j++) { wyn+=((sil[n-1-y]*sil[n-j])%mod*odwrot[n-y-j])%mod; wyn%=mod; } } wyn*=odwrot[n]; wyn%=mod; cout<<wyn<<"\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 | #include <bits/stdc++.h> using namespace std; #define int long long #define ll long long #define ld long double #define pb push_back #define nd second #define st first #define sz size #define forr(i, n) for(int i=1;i<=n;i++) const ll infl=1e18+90; const int inf=1e9+90; const int roz=2e5+93; const int mod=1e9+7; int sil[roz], odwrot[roz]; long long binpow(long long a, long long b, long long mod) { a %= mod; long long res = 1; while (b > 0) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } void silnie() { sil[0]=1; odwrot[0]=1; for(int i=1;i<=roz-60;i++) { sil[i]=sil[i-1]*i; sil[i]%=mod; odwrot[i]=binpow(sil[i], mod-2, mod); } } int fun(int a, int b) { return ((sil[a]*odwrot[b])%mod*odwrot[a-b])%mod; } set<pair<int,int> > S; set<int> graf[roz]; vector<pair<int,int> > wek, do_wyj; int ile[roz], odw[roz]; void dfs(int u, int x) { //cerr<<"wchodze dla u = "<<u<<" x = "<<x<<"\n"; ile[u]++; odw[u]=x; for(auto v:graf[u]) { if(odw[v]!=x) dfs(v, x); } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin>>n; silnie(); for(int i=1;i<=n;i++) { int a, b; cin>>a>>b; wek.pb({a, b}); } reverse(wek.begin(), wek.end()); for(int i=1;i<=n;i++) { int a=wek[i-1].st, b=wek[i-1].nd; for(auto [c, d]:S) { if(c>a&&c<b) { graf[d].insert(i); do_wyj.pb({c, d}); } } for(auto x:do_wyj) { S.erase(x); } do_wyj.clear(); S.insert({a, i}); S.insert({b, i}); } for(int i=1;i<=n;i++) { dfs(i, i); } int wyn=0; for(int i=1;i<=n;i++) { int y=ile[i]-1; for(int j=1;j<=n-y;j++) { wyn+=((sil[n-1-y]*sil[n-j])%mod*odwrot[n-y-j])%mod; wyn%=mod; } } wyn*=odwrot[n]; wyn%=mod; cout<<wyn<<"\n"; } |