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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for(int i=a; i>=b; --i)
#define pb push_back
#define ssize(v) (int)(v.size())

constexpr int MAX_N=1e4+14, MOD=1e9+7;// !! small N
struct lefRigS{
    int l,r;
};
int N, nrShlf[2*MAX_N];
ll rres=0, nFact=1;
lefRigS flood[MAX_N];
bitset<MAX_N> used, flooded;
vector<int> cCbn;

ll QPow(ll a, ll b){
    ll res=1, cMno=a;
    while(b){
        if(b%2) res*=cMno;
        b/=2;
        res%=MOD;
        cMno*=cMno;
        cMno%=MOD;
    }
    return res;
}
void Input(){
    int a, b;
    cin>>N;
    FOR(i,1,N){
        nFact*=i;
        nFact%=MOD;
        cin>>a>>b;
        flood[i]={nrShlf[a-1],nrShlf[b]};
        FOR(j,a,b-1) nrShlf[j]=i;
    }
}
void Flod(int v){
    if(flooded[v]) return;
    flooded[v]=1;
    Flod(flood[v].l), Flod(flood[v].r);
}
void GetQt(){
    FOR(i,1,N) flooded[i]=0;
    FOR(i,1,N){
        if(!flooded[cCbn[i]]) ++rres;
        Flod(cCbn[i]);
    }
    rres%=MOD;
}
void DFS(int cV){
    used[cV]=1;
    cCbn.pb(cV);
    if(ssize(cCbn)==N+1){
        GetQt();
        used[cV]=0;
        cCbn.pop_back();
        return;
    }
    FOR(nV,1,N) if(!used[nV]) DFS(nV);
    
    used[cV]=0;
    cCbn.pop_back();
}
void Solve(){
    ll revNFact, res;
    DFS(0);
    revNFact=QPow(nFact,MOD-2);
    res=(rres*revNFact)%MOD;
    cout<<res<<"\n";
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    Input();
    Solve();
}