#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
// #define st first
#define nd second
#define fi first
#define se second
#define vt vector
#define FOR(a, b, c) for(int a=b; a<c; ++a)
#define all(a) (a).begin(),(a).end()
#define sz(a) (int)(a).size()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pii PII;
typedef pll PLL;
constexpr ll nax = 5e5+6969, INF = 1e9+2137, mod = 1e9 + 7 ;
constexpr ld eps = 1e-9;
mt19937_64 rng(6969);
// mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
inline ll rand(ll l, ll r) {
return uniform_int_distribution<ll>(l, r)(rng);
}
int n, nr = 1, vis[nax], lef[nax], rig[nax];
ll ans;
vector<pii> v;
ll Pow(ll a, ll b) {
ll res = 1;
while(b) {
if(b&1) {
res = (res * a) % mod;
}
a = (a * a) % mod;
b >>= 1;
}
return res;
}
void flood(int x) {
vis[x] = nr;
// ans++;
if(lef[x] != -1 && vis[lef[x]] < nr) {
flood(lef[x]);
}
if(rig[x] != -1 && vis[rig[x]] < nr) {
flood(rig[x]);
}
}
int32_t main() {
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
scanf("%d", &n);
for(int i = 0; i <= n; i++) {
lef[i] = rig[i] = -1;
}
v.resize(n);
for(auto &e: v) {
scanf("%d%d", &e.fi, &e.se);
}
for(int i = n - 1; i >= 0; i--) {
for(int j = i - 1; j >= 0; j--) {
if(v[j].fi <= v[i].fi && v[i].fi <= v[j].se) {
lef[i] = j;
break;
}
}
for(int j = i - 1; j >= 0; j--) {
if(v[j].fi <= v[i].se && v[i].se <= v[j].se) {
rig[i] = j;
break;
}
}
}
// for(int i = 0; i < n; i++) {
// printf("%d %d\n", lef[i], rig[i]);
// }
vi perm;
for(int i = 0; i < n; i++) {
perm.pb(i);
}
ll fact = 0;
do{
// cout << "\n";
// for(auto e: perm)
// cout << e+1 << " ";
// cout << "\n";
for(auto ord: perm) {
if(vis[ord] < nr) {
ans++;
flood(ord);
}
}
nr++;
fact++;
// cout << "ans = " << ans << "\n";
}while(next_permutation(all(perm)));
// cout << ans << " " << fact << "\n";
ans = (ans * Pow(fact, mod - 2)) % mod;
printf("%lld\n", ans);
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 | #include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back // #define st first #define nd second #define fi first #define se second #define vt vector #define FOR(a, b, c) for(int a=b; a<c; ++a) #define all(a) (a).begin(),(a).end() #define sz(a) (int)(a).size() typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<ll> vll; typedef pii PII; typedef pll PLL; constexpr ll nax = 5e5+6969, INF = 1e9+2137, mod = 1e9 + 7 ; constexpr ld eps = 1e-9; mt19937_64 rng(6969); // mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); inline ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); } int n, nr = 1, vis[nax], lef[nax], rig[nax]; ll ans; vector<pii> v; ll Pow(ll a, ll b) { ll res = 1; while(b) { if(b&1) { res = (res * a) % mod; } a = (a * a) % mod; b >>= 1; } return res; } void flood(int x) { vis[x] = nr; // ans++; if(lef[x] != -1 && vis[lef[x]] < nr) { flood(lef[x]); } if(rig[x] != -1 && vis[rig[x]] < nr) { flood(rig[x]); } } int32_t main() { // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); scanf("%d", &n); for(int i = 0; i <= n; i++) { lef[i] = rig[i] = -1; } v.resize(n); for(auto &e: v) { scanf("%d%d", &e.fi, &e.se); } for(int i = n - 1; i >= 0; i--) { for(int j = i - 1; j >= 0; j--) { if(v[j].fi <= v[i].fi && v[i].fi <= v[j].se) { lef[i] = j; break; } } for(int j = i - 1; j >= 0; j--) { if(v[j].fi <= v[i].se && v[i].se <= v[j].se) { rig[i] = j; break; } } } // for(int i = 0; i < n; i++) { // printf("%d %d\n", lef[i], rig[i]); // } vi perm; for(int i = 0; i < n; i++) { perm.pb(i); } ll fact = 0; do{ // cout << "\n"; // for(auto e: perm) // cout << e+1 << " "; // cout << "\n"; for(auto ord: perm) { if(vis[ord] < nr) { ans++; flood(ord); } } nr++; fact++; // cout << "ans = " << ans << "\n"; }while(next_permutation(all(perm))); // cout << ans << " " << fact << "\n"; ans = (ans * Pow(fact, mod - 2)) % mod; printf("%lld\n", ans); return 0; } |
English