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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

using u32 = unsigned int;
using u64 = unsigned long long;

struct Shelf {
  u32 l, r;
};

struct TestCase {
  u32 n;
  vector<Shelf> shelves;
};

TestCase read_test_case() {
  TestCase tc;

  cin >> tc.n;
  tc.shelves.resize(tc.n);
  for (auto& [l, r] : tc.shelves) {
    cin >> l >> r;
    l--;
    r--;
  }
  return tc;
}

void solve_test_case(TestCase tc);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  solve_test_case(read_test_case());
}

class ShelfCoverage {
  struct Node {
    int value;
    u32 left_node, right_node;
    u32 begin, end;

    bool is_leaf() const { return begin + 1 == end; }
    u32 mid() const { return (begin + end) / 2; }
    u32 child(u32 i) {
      if (i < mid())
        return left_node;
      else
        return right_node;
    }
  };

  vector<Node> nodes;

 public:
  ShelfCoverage(u32 n) : nodes(4 * n) { init_node(0, 0, n); }

  void set(u32 begin, u32 end, int value) { _set(0, begin, end, value); }
  int get(u32 i) { return _get(0, i); }

 private:
  void init_node(u32 node, u32 begin, u32 end) {
    nodes[node] = {-1, 2 * node + 1, 2 * node + 2, begin, end};
    if (begin + 1 == end) return;

    u32 mid = (begin + end) / 2;
    init_node(2 * node + 1, begin, mid);
    init_node(2 * node + 2, mid, end);
  }

  int _get(u32 node, u32 i) {
    if (nodes[node].is_leaf()) return nodes[node].value;
    u32 child = nodes[node].child(i);
    return max(nodes[node].value, _get(child, i));
  }

  void _set(u32 node, u32 begin, u32 end, int value) {
    if (nodes[node].begin == begin && nodes[node].end == end) {
      nodes[node].value = value;
      return;
    }

    u32 mid = nodes[node].mid();
    if (end <= mid)
      _set(nodes[node].left_node, begin, end, value);
    else if (begin >= mid)
      _set(nodes[node].right_node, begin, end, value);
    else {
      _set(nodes[node].left_node, begin, mid, value);
      _set(nodes[node].right_node, mid, end, value);
    }
  }
};

const u64 M = 1000000007;

u64 fast_pow(u64 x, u64 p) {
  if (p == 0) return 1;
  if (p == 1) return x;

  u64 half = fast_pow(x, p / 2);
  u64 result = (half * half) % M;
  if (p % 2 == 1) result = (result * x) % M;
  return result;
}
u64 inv(u64 x) { return fast_pow(x, M - 2); }

struct DAG {
  vector<vector<int>> children;
  vector<vector<int>> parents;
};

DAG get_dag(const TestCase& tc) {
  ShelfCoverage coverage(2 * tc.n);
  vector<vector<int>> children(tc.n);
  vector<vector<int>> parents(tc.n);

  for (u32 i = 0; i < tc.n; i++) {
    u32 l = tc.shelves[i].l;
    u32 r = tc.shelves[i].r;

    int ls = coverage.get(l);
    int rs = coverage.get(r);

    if (ls != -1) {
      children[i].push_back(ls);
      parents[ls].push_back(i);
    }

    if (rs != -1 && ls != rs) {
      children[i].push_back(rs);
      parents[rs].push_back(i);
    }

    coverage.set(l, r + 1, i);
  }

  return {children, parents};
}

void solve_test_case(TestCase tc) {
  vector<int> ancestor_count(tc.n, 0);
  vector<set<int>> ancestors(tc.n);

  const auto [children, parents] = get_dag(tc);
  queue<int> q;
  vector<int> deg(tc.n);
  for (u32 i = 0; i < tc.n; i++) {
    deg[i] = parents[i].size();
    if (deg[i] == 0) q.push(i);
  }

  while (!q.empty()) {
    int current = q.front();
    q.pop();

    ancestor_count[current] = ancestors[current].size();

    for (int child : children[current]) {
      deg[child]--;
      ancestors[child].insert(current);
      for (auto x : ancestors[current]) ancestors[child].insert(x);
      if (deg[child] == 0) q.push(child);
    }
  }

  u64 result = 0;
  for (auto x : ancestor_count) {
    result += inv(x + 1);
    result %= M;
  }
  cout << result << "\n";
}