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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <bits/stdc++.h>
#define REP(i,n) for(int _n=(n), i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define TRACE(x) std::cerr << "TRACE(" #x ")" << std::endl;
#define DEBUG(x) std::cerr << #x << " = " << (x) << std::endl;
typedef long long LL; typedef unsigned long long ULL;

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

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; }
private:
  unsigned v;
};

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

class Minimizer {
public:
  explicit Minimizer(int size);
  bool empty() const { return counts[1] == 0; }
  void insert(int x);
  void erase(int x);
  int first() const;
private:
  int m_size;
  std::vector<int> counts;
};

Minimizer::Minimizer(int size) {
  m_size = 1;
  while(m_size < size) m_size <<= 1;
  counts.assign(2*m_size, 0);
}

void Minimizer::insert(int x) {
  x += m_size;
  while(x) {
    counts[x]+=1;
    x >>= 1;
  }
}

void Minimizer::erase(int x) {
  x += m_size;
  if (!counts[x]) return;
  while(x) {
    counts[x]-=1;
    x >>= 1;
  }
}

int Minimizer::first() const {
  int p = 1;
  while (p < m_size) {
    p <<= 1;
    if (!counts[p]) ++p;
  }
  return p - m_size;
}

struct Mine {
  LL position = 0;
  LL radius = 0;
  int range_a = -1;
  int range_b = -1;
  // Only for left-most trigger mines.
  int chain_a = -1;
  int chain_b = -1;
};

struct Vertex {
  int dfs_index = -1;
  int lowlink = -1;
  int chain_a = -1;
  int chain_b = -1;
};

class Minefield {
public:
  Minefield();
  Mod calc_possibilities();
private:
  int find_mine_ge(LL pos);
  void strongly_connected_components();
  void strongly_connected_components_dfs(int p);

  template<typename F>
  void for_each_successor(int p, F f);

  void calc_next_chain_reaches();
  void calc_suffix_possibilities();

  int num_mines = 0;
  std::vector<Mine> mines;

  // SCC
  std::vector<Vertex> vertices;
  std::vector<int> scc_stack;
  int next_dfs_index = 0;

  std::vector<int> next_chain_reaches;
  std::vector<Mod> suffix_possibilities;
};

Minefield::Minefield() {
  std::cin >> num_mines;
  mines.reserve(num_mines);
  REP(i, num_mines) {
    Mine mine;
    std::cin >> mine.position >> mine.radius;
    mines.push_back(mine);
  }

  for (Mine &mine : mines) {
    mine.range_a = find_mine_ge(mine.position - mine.radius);
    mine.range_b = find_mine_ge(mine.position + mine.radius + 1);
  }
}

template<typename F>
void Minefield::for_each_successor(int p, F f) {
  if (p < num_mines) {
    for (int i=0;i<2;++i) {
      int q = 2*p+i;
      if (q < 2*num_mines) {
        f(q);
      }
    }
  } else {
    const Mine &mine = mines[p - num_mines];
    int a = num_mines + mine.range_a;
    int b = num_mines + mine.range_b;
    while (a!=b) {
      if (a&1) {
        f(a);
        ++a;
      }
      if (b&1) {
        --b;
        f(b);
      }
      a >>= 1;
      b >>= 1;
    }
  }
}

Mod Minefield::calc_possibilities() {
  strongly_connected_components();
  calc_next_chain_reaches();
  calc_suffix_possibilities();
  return suffix_possibilities[0];
}

int Minefield::find_mine_ge(LL pos) {
  auto it = std::lower_bound(mines.begin(), mines.end(), pos, [](const Mine &mine, LL x) {
    return mine.position < x;
  });
  return it - mines.begin();
}

void Minefield::strongly_connected_components() {
  vertices.resize(2 * num_mines);

  for (int i=num_mines; i<2*num_mines; ++i) {
    if (vertices[i].dfs_index == -1) {
      strongly_connected_components_dfs(i);
    }
  }
}

void Minefield::strongly_connected_components_dfs(int p) {
  Vertex &v = vertices[p];

  v.dfs_index = next_dfs_index++;
  v.lowlink = v.dfs_index;
  scc_stack.push_back(p);

  for_each_successor(p, [&](int q) {
    Vertex &w = vertices[q];
    if (w.dfs_index == -1) {
      strongly_connected_components_dfs(q);
      v.lowlink = std::min(v.lowlink, w.lowlink);
    } else if (w.chain_b == -1) {
      // on stack
      v.lowlink = std::min(v.lowlink, w.dfs_index);
    }
  });

  if (v.lowlink == v.dfs_index) {
    auto component_start = scc_stack.end();
    for(;;) {
      assert(component_start != scc_stack.begin());
      --component_start;
      if (*component_start == p) break;
    }
    int chain_a = num_mines;
    int chain_b = 0;
    int trigger_mine = num_mines;
    for (auto it = component_start; it != scc_stack.end(); ++it) {
      int q = *it;
      if (q >= num_mines) {
        int mine_idx = q - num_mines;
        trigger_mine = std::min(trigger_mine, mine_idx);
        chain_a = std::min(chain_a, mines[mine_idx].range_a);
        chain_b = std::max(chain_b, mines[mine_idx].range_b);
      }
      for_each_successor(q, [&](int r) {
        if (vertices[r].chain_a != -1) {
          chain_a = std::min(chain_a, vertices[r].chain_a);
          chain_b = std::max(chain_b, vertices[r].chain_b);
        }
      });
    }
    for (auto it = component_start; it != scc_stack.end(); ++it) {
      vertices[*it].chain_a = chain_a;
      vertices[*it].chain_b = chain_b;
    }
    if (trigger_mine != num_mines) {
      mines[trigger_mine].chain_a = chain_a;
      mines[trigger_mine].chain_b = chain_b;
    }
    scc_stack.erase(component_start, scc_stack.end());
  }
}

void Minefield::calc_next_chain_reaches() {
  std::vector<std::pair<int, int>> by_chain_a;
  by_chain_a.reserve(num_mines);
  REP(i, num_mines) if (mines[i].chain_a != -1) {
    by_chain_a.emplace_back(mines[i].chain_a, i);
  }
  std::sort(by_chain_a.begin(), by_chain_a.end());
  auto by_chain_a_it = by_chain_a.begin();

  Minimizer reach_here(num_mines);
  next_chain_reaches.assign(num_mines, -1);

  REP(i, num_mines) if (mines[i].chain_a != -1) {
    while (by_chain_a_it != by_chain_a.end() &&
           by_chain_a_it->first <= i) {
      reach_here.insert(by_chain_a_it->second);
      ++by_chain_a_it;
    }
    reach_here.erase(i);
    if (!reach_here.empty()) {
      next_chain_reaches[i] = reach_here.first();
    }
  }
}

void Minefield::calc_suffix_possibilities() {
  suffix_possibilities.resize(num_mines+1);
  suffix_possibilities[num_mines] = 1;
  FORD(i,num_mines-1,0) {
    suffix_possibilities[i] = 0;
    // Explode first mine.
    if (mines[i].chain_b != -1) {
      suffix_possibilities[i] += suffix_possibilities[mines[i].chain_b];
    }

    // Don't explode first mine.
    suffix_possibilities[i] += suffix_possibilities[i+1];
    // Have to subtract those cases that reach here, i.e. those that explode `next`.
    int next = next_chain_reaches[i];
    if (next != -1) {
      assert(mines[next].chain_b != -1);
      suffix_possibilities[i] -= suffix_possibilities[mines[next].chain_b];
    }
  }
}

int main() {
  init_io();

  Minefield minefield;
  const Mod res = minefield.calc_possibilities();
  std::cout << res.get() << "\n";
}