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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <bits/stdc++.h>
using std::cin, std::cout, std::vector;
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
#define REP(i, n) for (u32 i=0; i<(n); ++i)
#define REPD(i, n) for (u32 i=(n)-1; i!=-1u; --i)
#define DEBUG(x) std::cerr << #x << " = " << x << "\n"

void init_io() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
}

template<class T>
T power(T a, unsigned b) {
  T res = T(1);
  while(b) {
    if(b&1u) res *= a;
    b >>= 1;
    a = a*a;
  }
  return res;
}

template<unsigned MOD>
class Modulo {
public:
  Modulo(unsigned x=0):v(x) {}
  unsigned get() const { return v; }
  Modulo operator+(Modulo b) const {
    unsigned res = v+b.v;
    if (res >= MOD) res -= MOD;
    return res;
  }
  void operator+=(Modulo b) { *this = *this + b; }
  Modulo operator-(Modulo b) const { return *this + Modulo(MOD-b.v); }
  void operator-=(Modulo b) { *this = *this - b; }
  Modulo operator*(Modulo b) const { return Modulo(std::uint64_t(v) * b.v % MOD); }
  void operator*=(Modulo b) { *this = *this * b; }
  Modulo operator/(Modulo b) const { return *this * b.inverse(); }
  void operator/=(Modulo b) { *this = *this / b; }
  Modulo inverse() const { return power(*this, MOD-2); }
private:
  unsigned v;
};

// =================

using Mod = Modulo<1'000'000'007>;

constexpr u32 none = -1u;

class Segments {
public:
  explicit Segments(const u32 len) {
    n = 1;
    while (n < len) n *= 2;
    vec.assign(2 * n, 0);
  }

  u32 get(u32 x) const {
    u32 res = 0;
    x += n;
    while (x != 0) {
      if (vec[x] != 0) res = vec[x];
      x >>= 1;
    }
    return res;
  }

  void set(const u32 a, const u32 b, const u32 val) {
    set_rec(1, n, a, b, val);
  }

  void set_rec(const u32 p, const u32 len, const u32 a, const u32 b, const u32 val) {
    if (a == 0 && b == len) {
      vec[p] = val;
      return;
    }
    const u32 len2 = len / 2;
    if (vec[p] != 0) {
      vec[2 * p] = vec[p];
      vec[2 * p + 1] = vec[p];
      vec[p] = 0;
    }
    if (a < len2) {
      set_rec(2*p, len2, a, std::min(b, len2), val);
    }
    if (b > len2) {
      set_rec(2*p+1, len2, std::max(a, len2)-len2, b-len2, val);
    }
  }

private:
  u32 n;
  vector<u32> vec;
};

struct Endpoint {
  u32 side = none;
  u32 height = none;
};

struct Node {
  u32 x[2] = {none, none};
  u32 children[2] = {none, none};
  Endpoint extreme_parents[2];
  // Only calculated for nodes that can walk to the top on that side.
  u32 top[2] = { none, none };
  u32 bottom = none;
  u32 num_reachable = 1;
};

class Graph {
public:
  Graph() {
    cin >> num_nodes;
    ++num_nodes;
    nodes.resize(num_nodes);
    nodes[0].x[0] = 0;
    nodes[0].x[1] = 2 * num_nodes - 1;
    for (u32 h=1; h < num_nodes; ++h) {
      REP(side, 2) {
        cin >> nodes[h].x[side];
      }
    }
  }

  Mod solve() {
    calc_endpoints();
    calc_children();
    calc_parents();
    calc_top();
    calc_bottom();
    calc_num_reachable();
    return final_calculation();
  }

  void calc_endpoints() {
    endpoints.assign(2 * num_nodes, Endpoint{0, none});
    REP(h, num_nodes) {
      const Node &node = nodes[h];
      REP(side, 2) {
        endpoints[node.x[side]] = Endpoint{side, h};
      }
    }
  }

  void calc_children() {
    Segments segments(2 * num_nodes);
    for (u32 h=1; h < num_nodes; ++h) {
      Node &node = nodes[h];
      REP(side, 2) {
        node.children[side] = segments.get(node.x[side]);
      }
      segments.set(node.x[0], node.x[1], h);
    }
  }

  void calc_parents() {
    for (u32 x=1; x<=2*num_nodes-2; ++x) {
      const Endpoint &endpoint = endpoints[x];
      const Node &parent = nodes[endpoint.height];
      Node &child = nodes[parent.children[endpoint.side]];
      if (child.extreme_parents[0].height == none) {
        child.extreme_parents[0] = endpoint;
      }
      child.extreme_parents[1] = endpoint;
    }
  }

  void calc_top() {
    for (u32 h=num_nodes-1; h>=1; --h) {
      Node &node = nodes[h];
      REP(side, 2) {
        const Endpoint &endpoint = node.extreme_parents[side];
        if (endpoint.height == none) continue; // no parent: no need to calculate top
        if (endpoint.side == side) {
          node.top[side] = nodes[endpoint.height].top[side];
        } else {
          node.top[side] = endpoint.height;
        }
      }
    }
  }

  void calc_bottom() {
    for (u32 top_h=1; top_h < num_nodes; ++top_h) {
      Node &node_top = nodes[top_h];
      u32 h[2] = {node_top.children[1], node_top.children[0]};
      while (h[0] != h[1]) {
        const u32 side = h[0] > h[1] ? 0 : 1;
        const Node &node = nodes[h[side]];
        const u32 t = node.top[side];
        if (t == none || t == top_h) {
          h[side] = node.children[side];
        } else {
          h[side] = nodes[t].bottom;
        }
      }
      node_top.bottom = h[0];
    }
  }

  void calc_num_reachable() {
    for (u32 h=num_nodes-1; h >= 1; --h) {
      const Node &node = nodes[h];
      REP(side, 2) {
        nodes[node.children[side]].num_reachable += node.num_reachable;
      }
      nodes[node.bottom].num_reachable -= node.num_reachable;
    }
  }

  Mod final_calculation() const {
    Mod p = 0;
    Mod q = 1;
    for (u32 h=1; h < num_nodes; ++h) {
      const Node &node = nodes[h];
      const Mod r = node.num_reachable;
      // p / q + 1 / r = (p * r + q) / (q * r)
      p = p * r + q;
      q *= r;
    }
    return p / q;
  }

private:
  u32 num_nodes;
  vector<Node> nodes;
  vector<Endpoint> endpoints;
};

int main() {
  init_io();

  Graph graph;
  const Mod result = graph.solve();
  cout << result.get() << "\n";
}