#include <iostream> #include <vector> #include <algorithm> #define ll long long using namespace std; const ll N = 1<<19; const ll MOD = 1e9+7; vector<vector<ll>> g(N); bool visited[N]; ll tree[2*N+1]; ll fastpow(ll a, ll w) { ll res=1; while(w) { if(w%2==1) { res*=a; if(res>=MOD)res%=MOD; } a*=a; if(a>=MOD) a%=MOD; w/=2; } return res; } void dfs(ll s) { visited[s]=1; for(auto u : g[s]) { if(!visited[u]) dfs(u); } } void insert(ll p, ll k , ll v) { p+=N-1; k+=N+1; while(p/2!=k/2) { if(p%2==0) tree[p+1]=v; if(k%2==1) tree[k-1]=v; p/=2; k/=2; } } ll query(ll x) { ll res=0; x+=N; while(x) { res=max(res,tree[x]); x/=2; } return res; } int main() { ll n; ll odw=1,wynik=0; cin>>n; for(ll i = 1 ;i <= n ;i ++) { ll a,b; cin>>a>>b; g[i].push_back(query(a)); g[i].push_back(query(b)); insert(a,b,i); } vector<ll> P; for(ll i = 1 ;i <= n ;i ++) { P.push_back(i); odw*=i; if(odw>=MOD)odw%=MOD; } do { for(ll i = 0 ;i <=n ;i ++) visited[i]=0; for(ll i = 0 ;i < n ;i ++) { if(!visited[P[i]]) { wynik++; dfs(P[i]); } } } while (next_permutation(P.begin(),P.end())); cout<<(wynik*(fastpow(odw,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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | #include <iostream> #include <vector> #include <algorithm> #define ll long long using namespace std; const ll N = 1<<19; const ll MOD = 1e9+7; vector<vector<ll>> g(N); bool visited[N]; ll tree[2*N+1]; ll fastpow(ll a, ll w) { ll res=1; while(w) { if(w%2==1) { res*=a; if(res>=MOD)res%=MOD; } a*=a; if(a>=MOD) a%=MOD; w/=2; } return res; } void dfs(ll s) { visited[s]=1; for(auto u : g[s]) { if(!visited[u]) dfs(u); } } void insert(ll p, ll k , ll v) { p+=N-1; k+=N+1; while(p/2!=k/2) { if(p%2==0) tree[p+1]=v; if(k%2==1) tree[k-1]=v; p/=2; k/=2; } } ll query(ll x) { ll res=0; x+=N; while(x) { res=max(res,tree[x]); x/=2; } return res; } int main() { ll n; ll odw=1,wynik=0; cin>>n; for(ll i = 1 ;i <= n ;i ++) { ll a,b; cin>>a>>b; g[i].push_back(query(a)); g[i].push_back(query(b)); insert(a,b,i); } vector<ll> P; for(ll i = 1 ;i <= n ;i ++) { P.push_back(i); odw*=i; if(odw>=MOD)odw%=MOD; } do { for(ll i = 0 ;i <=n ;i ++) visited[i]=0; for(ll i = 0 ;i < n ;i ++) { if(!visited[P[i]]) { wynik++; dfs(P[i]); } } } while (next_permutation(P.begin(),P.end())); cout<<(wynik*(fastpow(odw,MOD-2)))%MOD; } |