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
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define maxn 500010
#define maxa 30
#define maxv 20000
#define result_t pair<bool, pair<int,char>>
#define mod 1000000007

vector<int> permutation;
vector<pair<int,int>> kranik;
vector<int> kraw[maxn];
bool odw[maxn];

void conditional_push(int x, int y) {
    if (y >= 0) {
        kraw[x].push_back(y);
    }
}

int find (int pos, int maxi) {
    for (int i = maxi - 1; i >= 0; --i) {
        if (kranik[i].first <= pos && kranik[i].second >= pos) {
            return i;
        }
    }
    return -1;
}

void dfs(int x) {
    odw[x] = true;
    for (int neighbour : kraw[x]) {
        if (!odw[neighbour]) {
            dfs(neighbour);
        }
    }
}

ll solve_one () {
    ll res = 0;
    for (int i = 0; i < (int)permutation.size(); ++i) {
        odw[i] = false;
    }
    for (int el : permutation) {
        if (!odw[el]) {
            res++;
            dfs(el);
        }
    }
    return res;
}

ll quick_pow(ll x, ll n) {
    ll res = 1;
    while (n != 0) {
        if (n % 2 == 1) {
            res = (res * x)%mod;
        }
        n /= 2;
        x = (x * x) % mod;
    }
    return res;
}

int main() {
    int n,a,b;
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    ll silnia = 1;
    for (int i = 0; i < n; ++i) {
        permutation.push_back(i);
        cin >> a >> b;
        kranik.push_back({a,b});
        conditional_push(i, find(b, i));
        conditional_push(i, find(a, i));
        silnia = (silnia * (ll)(i+1)) % mod;
    }
    ll sum = 0;
    do {
       sum = (sum + solve_one()) % mod;
    } while (next_permutation(permutation.begin(), permutation.begin() + n));
    //cout << sum << " " << silnia << endl;
    if (__gcd(sum, silnia) != 1) {
        int gcd = __gcd(sum, silnia);
        sum /= gcd;
        silnia /= gcd;
    }
    //cout << sum << " " << silnia << endl;
    cout << (sum * quick_pow(silnia, mod - 2)) % mod;
}