Trains
Time limit2sMemory limit1024 MB
Count the journeys from city 1 where a train from city i stops every d_i cities, at most x_i times, without passing city N. Print the count modulo 10^9+7.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Prefix sum
- Solved
- No attempts yet
Problem
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 to in order. Vilnius is city number .
Each city has a train station with one train route that departs from it. You can only board a train at the start of its route, but you can get off at any of its stops. Trains starting at the -th city stop every cities, and their route has stops (not counting the starting city). If , the trains departing from the -th city are out of service, so you cannot board them.
More precisely, if you board a train in city , you can get off in any city numbered where . Since you only want to visit cities in Lithuania, you will not ride past city , even if the train has more stops on its route.
You are going to visit some cities by taking trains between them. While planning the trip, you wonder how many different options you have for a journey that starts in Vilnius. Two journeys are different if they stop at different sequences of cities.
Calculate this number and print it modulo .
Input
The first line contains one integer , the number of cities. Then lines follow. The -th of them contains two integers and , describing the route that starts in the -th city.
Output
Print a single integer, the number of ways to visit some of the cities, modulo .
Constraints
- for each
- for each