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
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i = a; i < b;++i)
#define pb emplace_back
#define mp make_pair
#define f first
#define s second
typedef long long ll;
using namespace std;
int M = 1e9 + 7;
const int maxn = 10000;
vector<int> vis(maxn,0),tab(maxn,0);
vector<int> V[maxn],V1,wyn;
inline void dfs(int v){
    for(auto &y : V[v]){
        if(vis[y] == 0){
            vis[y] = 1;
            dfs(y);
        }
    }
}
inline ll fast_pow(ll a,ll b){
    ll x = 1;
    while(a > 0){
        if(a % 2){
            x*=b;
            x%=M;
        }
        b*=b;
        b%=M;
        a/=2;
    }
    return x;
}
int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int n;
    cin>>n;
    vector<pair<int,int>> in;
    FOR(i,0,n){
        int x,y;
        cin>>x >>y;
        in.pb(mp(x,y));
    }
    reverse(in.begin(),in.end());
    FOR(i,0,in.size()){
        int x = in[i].f,y = in[i].s;
        FOR(j,x,y + 1){
            if(tab[j] != 0){
                V[tab[j]].pb(i + 1);
            }
            tab[j] = 0;
        }
        tab[x] = i + 1;
        tab[y] = i + 1;
    }
    FOR(i,0,n){
        V1.pb(i + 1);
    }
    int k = 0,suma = 0;
    ++suma;
    FOR(i,1,n + 1){vis[i] = 0;}
    FOR(i,0,V1.size()){
        if(vis[V1[i]] == 0){
            ++k;
            dfs(V1[i]);
            vis[V1[i]] = 1;
        }
    }
    while(next_permutation(V1.begin(),V1.end())){
        ++suma;
        FOR(i,1,n + 1){vis[i] = 0;}
        FOR(i,0,V1.size()){
            if(vis[V1[i]] == 0){
                ++k;
                dfs(V1[i]);
                vis[V1[i]] = 1;
            }
        }
    }
    int z1 = gcd(k,suma);
    k/=z1;
    suma/=z1;
    //cout<<k <<" " <<suma <<"\n";
    ll z = k * fast_pow(M - 2,suma);
    z%=M;
    cout<<z;
}