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
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <iostream>
#include <algorithm>
#include <list>
#include <stack>

constexpr long MOD = 1000000007;

int n;
int* rs;
int* as;
bool* visited;

std::vector<int> cc_sorted;
long * counts;
long long result = 1;

void read() {
  scanf("%d\n", &n);
  rs = new int[n];
  as = new int[n];
  for (int i=0; i<n; i++) {
    scanf("%d %d\n", as + i, rs + i);
  }
}

int* node_to_cc_index;
int cc_index = 0;

class Graph {
  public:
  int V;
  std::set<int> *adj;
  void fillOrder(int s, bool visitedV[], std::stack<int> &Stack);
  void DFS(int s, bool visitedV[]);
  Graph(int V);
  void addEdge(int s, int d);
  void printSCC();
  Graph transpose();
};

Graph::Graph(int V) {
  this->V = V;
  adj = new std::set<int>[V];
}

// DFS
void Graph::DFS(int s, bool visitedV[]) {
  visitedV[s] = true;
  node_to_cc_index[s] = cc_index;

  for (auto i = adj[s].begin(); i != adj[s].end(); ++i)
    if (!visitedV[*i])
      DFS(*i, visitedV);
}

// Transpose
Graph Graph::transpose() {
  Graph g(V);
  for (int s = 0; s < V; s++) {
    for (auto i = adj[s].begin(); i != adj[s].end(); ++i) {
      g.adj[*i].emplace(s);
    }
  }
  return g;
}

// Add edge into the graph
void Graph::addEdge(int s, int d) {
  adj[s].emplace(d);
}

void Graph::fillOrder(int s, bool visitedV[], std::stack<int> &Stack) {
  visitedV[s] = true;

  for (auto i = adj[s].begin(); i != adj[s].end(); ++i)
    if (!visitedV[*i])
      fillOrder(*i, visitedV, Stack);

  Stack.push(s);
}

// Print strongly connected component
void Graph::printSCC() {
  std::stack<int> Stack;

  bool *visitedV = new bool[V];
  for (int i = 0; i < V; i++)
    visitedV[i] = false;

  for (int i = 0; i < V; i++)
    if (visitedV[i] == false)
      fillOrder(i, visitedV, Stack);

  Graph gr = transpose();

  for (int i = 0; i < V; i++)
    visitedV[i] = false;

  while (Stack.empty() == false) {
    int s = Stack.top();
    Stack.pop();

    if (visitedV[s] == false) {
      gr.DFS(s, visitedV);
      cc_index ++;
    }
  }
}

void topologicalSortUtil(int v, Graph & graph)  { 
    visited[v] = true; 
    for (auto j :graph.adj[v]) {
        if (!visited[j]) {
            topologicalSortUtil(j, graph); 
        }
    }
  
    cc_sorted.emplace_back(v); 
} 

int main() {
  read();
  Graph g(n);
  for (int i=0; i < n; i++) {
    int j = i+1;
    while(j < n && as[j] <= as[i] + rs[i]) {
      g.addEdge(i, j);
      j ++;
    }
    j = i - 1;
    while(j > 0 && as[j] >= as[i] - rs[i]) {
      g.addEdge(i, j);
      j--;
    }
  }
  node_to_cc_index = new int[n];
  g.printSCC();
  Graph ccg = Graph(cc_index);
  for (int i=0;i<n; i++) {
    //printf("%d: ", i);
    int c1 = node_to_cc_index[i];
    for (auto j : g.adj[i]) {
      int c2 = node_to_cc_index[j];
      ccg.addEdge(c1, c2);
    }
    //printf("\n");
  }
  // for (int i=0; i < ccg.V; i++) {
  //   printf("%d: ", i);
  //   for (auto j: ccg.adj[i]) {
  //     printf("%d ", j);
  //   }
  //   printf("\n");
  // }
  Graph ccgTr = ccg.transpose();
  // for (int i=0; i < ccgTr.V; i++) {
  //   printf("%d: ", i);
  //   for (auto j: ccgTr.adj[i]) {
  //     printf("%d ", j);
  //   }
  //   printf("\n");
  // }

  visited = new bool[ccg.V];
  for (int i = 0; i < ccg.V; i++) 
      visited[i] = false; 

  for (int i = 0; i < ccg.V; i++) {
      if (visited[i] == false) {
          topologicalSortUtil(i, ccg); 
      }
  }
  std::reverse(cc_sorted.begin(),cc_sorted.end());
  // for (int i=0; i<cc_sorted.size(); i++) {
  //   printf("%d ", cc_sorted[i]);
  // }
  // printf("\n");
  // printf("counts\n");
  counts = new long[cc_index];

  for (int i=0; i<cc_index; i++) {
    int ix = cc_sorted[i];
    int m = ccgTr.adj[ix].size();
    if (m == 0) {
      counts[ix] = 1;
    } else if (m == 1) {
      counts[ix] = counts[*(ccgTr.adj[ix].begin())] % MOD;
      counts[ix] = (counts[ix] + 1) % MOD;
    } else if (m == 2) {
      counts[ix] = 1;
      std::vector<long> v(ccgTr.adj[ix].size());
      std::copy(ccgTr.adj[ix].begin(), ccgTr.adj[ix].end(), v.begin());
      for (auto it1 = 0; it1 < v.size(); it1++) {
        for (auto it2 = it1 + 1; it2 < v.size(); it2++) {
          counts[ix] = (counts[ix] + counts[v[it1]] * counts[v[it2]]) % MOD;
        }
      }
      for (auto it1 = 0; it1 < v.size(); it1++) {
        counts[ix] = (counts[ix] + counts[v[it1]]) % MOD;
      }
    }
  //  printf("%d, %ld: %ld\n", i, ix, counts[ix]);
    if (ccg.adj[ix].size() == 0) {
      result = (result * ((long long) counts[ix])) % MOD;
    }
  }
  //printf("result\n");
  printf("%lld\n", result + 1);

  return 0;
}