#P15979. [PA 2026] 手机游戏 / Gra mobilna

[PA 2026] 手机游戏 / Gra mobilna

Problem Description

Recently in Bajtocja, a mobile game called Potyczki Survival Shooter has become very popular. In this game, we control a squad of warriors moving along a straight path. From time to time, the squad encounters an event. In the ii-th event, the player has two options: go on the left side of the road and gain aia_i additional warriors; or go on the right side and multiply the current number of warriors by bib_i. Although everyone knows that having more warriors is better, the game’s ads clearly state that making the right decision can sometimes be very complicated.

After the first ll events, Bajtazar has xx warriors. (The number xx does not necessarily have to be obtainable from the first ll events according to the rules above—after all, these are warriors, not tourists, and a bad player may lose some of them in fights with zombies; those rules are not described in this problem.) He wants to know how many warriors he will have after event rr if he plays optimally. Please help him compute it.

Input Format

The first line contains integers nn and qq (1≤n≤500 0001 \leq n \leq 500\,000, 1≤q≤100 0001 \leq q \leq 100\,000).

The next nn lines describe the events; the ii-th line contains two integers aia_i and bib_i (0≤ai≤109+70 \leq a_i \leq 10^9 + 7, 1≤bi≤109+71 \leq b_i \leq 10^9 + 7).

The next qq lines describe the queries; the ii-th line contains three integers xix_i, lil_i, and rir_i (0≤xi≤109+70 \leq x_i \leq 10^9 + 7, 0≤li≤ri≤n0 \leq l_i \leq r_i \leq n).

Output Format

For each query, output one number: the number of warriors when using the optimal strategy. Output the result modulo 109+710^9 + 7.

10 2
3 2
3 2
3 2
0 1000
0 1000
0 1000
0 1000
0 1000
0 1000
123 1
1 0 3
1 3 9
16
49

Hint

Sample explanation: In the first query, Bajtazar starts with 11 warrior. In the first event, he can gain 33 additional warriors, or multiply the number of warriors by 22. Under the optimal strategy, he chooses the first option, so he will have 44 warriors. The next two events are the same, but this time doubling the number of warriors is more beneficial. In the end, Bajtazar will have 1616 warriors.

Translated by ChatGPT 5