trains (BOI contest)
Time limit: 1 seconds | Memory limit: 512 MB | Task type: batch
Algorithms: sqrt decomposition, prefix sum, dynamic programming

![](_page_0_Picture_0.jpeg)

# Trains

You have arrived in Vilnius and want to visit various cities in Lithuania.

Cities in Lithuania are located on a straight line and numbered from 1 to *N* sequentially. Vilnius is assigned the number 1.

Each city has a train station with a single train route that operates from that station. You can only get on a train at the start of its route, though you can get off at any of its stops. Trains starting their route at the *i*-th city make a stop every *d<sub>i</sub>* cities, and their route consists of *x<sub>i</sub>* stops (not including the starting city). If *d<sub>i</sub>* = 0, the trains operating from the *i*-th city are currently out of service, so you cannot get on them.

To be more precise, if you get on a train in the *i*-th city, you can get off the train in any city numbered *i* + *t* ⋅ *d<sub>i</sub>*, where 1 ≤ *t* ≤ *x<sub>i</sub>*. Note that since you only want to visit cities in Lithuania, you won't ride further than the *N*-th city, even if the train has more stops on its route.

## Task

You are going to visit some cities, using trains to travel between them. As you plan your trip, you start to wonder how many different options you have for your journey that starts at Vilnius. Two journeys are different if they make stops at different sequences of cities.

Calculate this number and print the answer modulo 10<sup>9</sup> + 7.

## Input

The first line contains one integer *N* – the number of cities.

Then *N* lines follow. The *i*-th of them contains two integers *d<sub>i</sub>* and *x<sub>i</sub>* – the numbers describing the route starting in the *i*-th city.

## Output

Output a single integer – the number of ways you can visit some of the *N* cities taken modulo 10<sup>9</sup> + 7.

## Examples

| Input | Output | Explanation                                   |
|-------|--------|-----------------------------------------------|
| 5     | 7      | There are 7 possible journeys you could take: |
| 1 3   |        | • 1                                          |
| 2 1   |        | • 1 → 2                                      |
| 1 3   |        | • 1 → 2 → 4                                  |
| 0 10  |        | • 1 → 3                                      |
| 3 5   |        | • 1 → 3 → 4                                  |
|       |        | • 1 → 3 → 5                                  |
|       |        | • 1 → 4                                      |

## Constraints

- 1 ≤ *N* ≤ 10<sup>5</sup>
- 0 ≤ *d<sub>i</sub>* ≤ 10<sup>9</sup> (for each 1 ≤ *i* ≤ *N*)
- 0 ≤ *x<sub>i</sub>* ≤ 10<sup>9</sup> (for each 1 ≤ *i* ≤ *N*)

## Subtasks

| No. | Points | Additional constraints        |
|-----|--------|-------------------------------|
| 1   | 8      | $n \le 15.$                   |
| 2   | 13     | $n \le 10^4.$                 |
| 3   | 16     | For all trains, $d_i = 1.$    |
| 4   | 34     | For all trains, $x_i = 10^9.$ |
| 5   | 29     | No additional constraints.    |

Starter code:

Grader code:

Setup script:

Evaluation script: