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
#include <iostream>
#include <map>
#include <queue>
#include <vector>

constexpr int XD = 1'000'000'007;

int inv(int nr) {
    int t = 0, nt = 1, r = XD;

    while (nr != 0) {
        int q = r / nr;
        int nnt = t - q * nt;
        int nnr = r - q * nr;
        t = nt;
        r = nr;
        nt = nnt;
        nr = nnr;
    }

    return t < 0 ? t + XD : t;
}

int main() {
    int n;
    std::cin >> n;

    std::vector<std::pair<int, int>> in(n);
    for (auto &[l, r]: in) {
        std::cin >> l >> r;
    }

    std::vector<std::vector<int>> prev(n);
    std::map<int, int> sources;
    for (int i = 0; i < n; ++i) {
        auto [l, r] = in[n - i - 1];
        auto [it, _] = sources.emplace(l, i);
        ++it;
        while (it != sources.end() && it->first < r) {
            prev[i].push_back(it->second);
            sources.erase(it++);
        }
        sources.emplace_hint(it, r, i);
    }

    int ret = 0;
    for (int i = 0; i < n; ++i) {
        std::vector<bool> visited(n);
        visited[i] = true;
        int visited_count = 1;
        std::queue<int> next;
        next.push(i);
        while (!next.empty()) {
            int ii = next.front();
            next.pop();
            for (auto iii: prev[ii]) {
                if (!visited[iii]) {
                    visited[iii] = true;
                    visited_count += 1;
                    next.push(iii);
                }
            }
        }

        ret += inv(visited_count);
        ret %= XD;
    }
    std::cout << ret << "\n";
}