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
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
//#include <bits/stdc++.h>
#include <bit>
#include <print>
#include <iostream>
#include <cassert>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <algorithm>
#include <functional>
#include <ranges>
#include <numeric>
#define FOR(i,p,k) for(int i=(p); i<=(k); ++i)
#define REP(i,k) FOR(i,0,(k)-1)
#define RFOR(i,p,n) for(int i=(p); i>=(n); --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define ssize(x) int((x).size())
#define fi first
#define se second
#define V vector
#define pb push_back
#define eb emplace_back
#define C const
#define pn printf("\n")
using namespace std;
typedef long long ll;
typedef V <int> vi;
typedef V <ll> vll;
typedef C int ci;
typedef C ll cll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
void chmin(auto &a, auto b){a=min(a,b);}
void chmax(auto &a, auto b){a=max(a,b);}
ci inf=2.1e9;
cll infll=4.5e18;
int I(){
    int z;
    while ((z=getchar_unlocked())<'-'||z>'9');
    C bool czymin=z=='-';
    int r=czymin ? 0 : z-'0';
    while ((z=getchar_unlocked())>='0'&&z<='9')
        r=r*10+z-'0';
    return czymin ? -r : r;
}
const int mod=1e9+7;
struct mozl {
    ll a,b;
};
struct agr {
    ll plus=0,razy=1;
    agr(){}
    agr(C mozl &i) {
        if (i.b > 1)
            razy = i.b;
        else
            plus = i.a;
    }
    void operator^=(const agr &i) {
        plus = (plus * i.razy + i.plus) % mod;
        razy = (razy * i.razy) % mod;
    }
    agr operator^(const agr &i) C{
        agr nowy=*this;
        nowy ^= i;
        return nowy;
    }
};
struct wart {
    ll x;
    bool czyinf=0;
    wart(ci i) {
        x=i;
    }
    void operator^=(const mozl &i) {
        if (czyinf) {
            if (i.b > 1)
                x = (x * i.b) % mod;
            else
                x = (x + i.a) % mod;
            return;
        }
        x = max(x + i.a, x * i.b);
        if (x >= mod) {
            czyinf = 1;
            x %= mod;
        }
    }
    void operator^=(const agr &i) {
        assert(czyinf);
        x = (x * i.razy + i.plus) % mod;
    }
    void operator+=(cll l) {
        *this^=mozl(l, 1);
    }
};

struct stt{
    V <agr> t;
    int comp;
    stt(C V <mozl> &v) {
        ci n=ssize(v);
        comp=std::bit_ceil(unsigned(n));
        t.resize(comp<<1);
        REP(i, n)
            t[comp+i]=agr(v[i]);
        RFOR(i, comp-1, 1)
            t[i] = t[i<<1] ^ t[i<<1|1];
    }
    agr que(int p, int k) C {
        agr pw,kw;
        for (p += comp, k += comp+1; p<k; p>>=1,k>>=1) {
            if (p&1)
                pw ^= t[p++];
            if (k&1)
                kw = t[--k] ^ kw;
        }
        return pw^kw;
    }
};
void ans(){
    ci n=I(),q=I();
    V <mozl> vm(n);
    vll aprefsum(n);
    vi niezera;
    // mozl, pozycja.
    V <pair <mozl, int>> vniejedb;
    struct idk {
        int p,vind;
        bool operator<(C idk &i) C{
            return p < i.p;
        }
        bool operator<(ci i) C{
            return p < i;
        }
    };
    V <idk> mapaniejedb;
    REP(i, n) {
        vm[i]={I(), I()};
        if (i)
            aprefsum[i]+=aprefsum[i-1];
        aprefsum[i]+=vm[i].a;
        if (vm[i].a)
            niezera.eb(i);
        if (vm[i].b>1) {
            mapaniejedb.eb(i, ssize(vniejedb));
            vniejedb.eb(vm[i], i);
        }
    }
    niezera.eb(n);
    mapaniejedb.eb(n, ssize(vniejedb));
    vniejedb.eb(vm.back(), n);
    auto dajaprefsum=[&](ci p, ci k) {
        assert(p<=k+1);
        if (p>k)
            return 0ll;
        if (!p)
            return aprefsum[k];
        return aprefsum[k]-aprefsum[p-1];
    };
    const stt st(vm);
    REP(_, q) {
        wart x(I());
        int p=I();
        ci k=I()-1;
        if (x.x == 0)
            p=*lower_bound(all(niezera), p);
        int popp=p;
        int vind;
        if (p>k)
            goto fin;
        {
            auto itr=lower_bound(all(mapaniejedb), p);
            vind = itr->vind;
            p = itr->p;
        }
        do {
            x += dajaprefsum(popp, min(p-1, k));
            if (p>k)
                goto fin;
            x ^= vniejedb[vind].fi;
            ++vind;
            popp=p+1;
            p=vniejedb[vind].se;
        } while (!x.czyinf);
        x ^= st.que(popp, k);
fin:
        println("{}", x.x);
    }
}
int main(){
    ios_base::sync_with_stdio(0),cin.tie(0);
    int tt=1;
    //tt=I();
    while (tt--)ans();
}