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
#include <iostream>
#include <map>
#include <unordered_set>
#include <vector>
using namespace std;

long long MOD = 1'000'000'007;

int inv(int x) {
    if (x <= 1) { return x; }
    return MOD - MOD / x * inv(MOD % x) % MOD;
}

struct Shelf {
    unordered_set<int> parents;
    int s, e;
    int8_t activeStreams = 2;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<Shelf> shelfs;
    shelfs.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> shelfs[i].s >> shelfs[i].e;
        shelfs[i].parents.insert(i);
    }
    map<int, int> inProgress;
    long long res{};
    for (int i = n - 1; i >= 0; --i) {
        auto &curr = shelfs[i];
        unordered_set<int> toRemove;
        for (auto pos = inProgress.lower_bound(shelfs[i].s); pos != end(inProgress) && pos->first < curr.e; ++pos) {
            auto &parentShelf = shelfs[pos->second];
            for (auto &p : parentShelf.parents) {
                curr.parents.insert(p);
            }
            --parentShelf.activeStreams;
            if (parentShelf.activeStreams == 0) {
                parentShelf.parents.clear();
            }
            toRemove.insert(pos->first);
        }
        for (auto r : toRemove) {
            inProgress.erase(r);
        }
        inProgress[curr.s] = i;
        inProgress[curr.e] = i;
        res += inv(size(curr.parents));
        res %= MOD;
    }
    cout << res << endl;
}