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
#include <iostream>
#include <vector>
using namespace std;
vector<pair<int, int>> T;
const int INF = 1'000'000'007;
long long symulacja(long long x, int l, int r){
    long long res = x;
    for(int i=l; i<r; i++){
        pair<int,int> decyzja = T[i];
        res = res + decyzja.first > res * decyzja.second ? res + decyzja.first : res * decyzja.second;
        //cout<<"Debug "<<res + decyzja.first<<", "<<res * decyzja.second<<endl;
        res%=INF;
        //cout<<"Debug "<<i<<": "<<res<<endl;
    }
    return res;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, q;
    cin>>n>>q;
    for(int i=0; i<n; i++){
        int a, b;
        cin>>a>>b;
        T.push_back({a, b});
    }
    
    for(int i=0; i<q; i++){
        long long int x;
        int l, r;
        cin>>x>>l>>r;
        cout<<symulacja(x, l, r)<<'\n';
    }
    
}