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
#include <bits/stdc++.h>

using namespace std;

#define int long long

constexpr int MOD = 1'000'000'000 + 7;

signed main(){

    ios::sync_with_stdio(0);
    cin.tie(nullptr);

    int n, q;
    cin>>n>>q;

    vector <pair<int, int>> event(n);

    for(auto& i : event)
        cin>>i.first>>i.second;

    for(int i = 0; i < q; i++){

        int x, l, r;
        cin>>x>>l>>r;

        bool overflow = 0;

        for(int j = l; j < r; j++){

            if(overflow && event[j].second > 1)
                x *= event[j].second;
            else if(x + event[j].first > x * event[j].second)
                x += event[j].first;
            else
                x *= event[j].second;

            if(x >= MOD)
                overflow = 1;

            x %= MOD;

        }

        cout<<x<<'\n';

    }

   
    return 0;

}