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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * ZADANIE: GRM (Gra Mobilna)
 * Algorytm: Drzewo przedziałowe z leniwą propagacją logarytmicznego wzrostu.
 * Złożoność: O(N + Q * (log N + log MOD))
 */

const long long MOD = 1000000007;

int n, q;
long long A[500005];
long long B[500005];

struct Node {
    long long mul;   // Złożony mnożnik w przedziale (modulo MOD)
    long long add;   // Złożony składnik w przedziale (modulo MOD)
    long long sum_a; // Suma a_i (używana, gdy b_i są same jedynki)
    bool has_big_b;  // Czy w przedziale jest jakiekolwiek b_i >= 2
} tree[2000005];

// Składanie dwóch funkcji liniowych: f2(f1(x))
// f1(x) = x*m1 + a1, f2(x) = x*m2 + a2
// f2(f1(x)) = (x*m1 + a1)*m2 + a2 = x*(m1*m2) + (a1*m2 + a2)
Node combine(const Node& L, const Node& R) {
    Node res;
    res.mul = (L.mul * R.mul) % MOD;
    res.add = (L.add * R.mul + R.add) % MOD;
    res.sum_a = L.sum_a + R.sum_a;
    res.has_big_b = L.has_big_b || R.has_big_b;
    return res;
}

void build(int v, int tl, int tr) {
    if (tl == tr) {
        tree[v].sum_a = A[tl];
        if (B[tl] >= 2) {
            tree[v].has_big_b = true;
            tree[v].mul = B[tl] % MOD;
            tree[v].add = 0;
        } else {
            tree[v].has_big_b = false;
            tree[v].mul = 1;
            tree[v].add = A[tl] % MOD;
        }
    } else {
        int tm = (tl + tr) / 2;
        build(2 * v, tl, tm);
        build(2 * v + 1, tm + 1, tr);
        tree[v] = combine(tree[2 * v], tree[2 * v + 1]);
    }
}

// x - obecna liczba wojowników, is_large - czy x przekroczyło MOD
void query(int v, int tl, int tr, int ql, int qr, long long &x, bool &is_large) {
    if (tl > qr || tr < ql) return;

    // PRZYPADEK OPTYMALNY:
    // Jeśli liczba jest już duża, zawsze wybieramy mnożenie dla b >= 2.
    // Jeśli w przedziale nie ma żadnego b >= 2, zawsze tylko dodajemy.
    if (tl >= ql && tr <= qr) {
        if (is_large || !tree[v].has_big_b) {
            if (is_large) {
                x = (x * tree[v].mul + tree[v].add) % MOD;
            } else {
                x += tree[v].sum_a;
                if (x >= MOD) {
                    is_large = true;
                    x %= MOD;
                }
            }
            return;
        }
    }

    // PRZYPADEK DIALOGOWY:
    // Jeśli x jest małe i są mnożniki, musimy zejść niżej.
    // Ale x rośnie co najmniej podwójnie przy każdym b >= 2, więc 
    // zejść do liści będzie tylko ~30 razy na całe zapytanie.
    if (tl == tr) {
        long long opt_add = x + A[tl];
        long long opt_mul = x * B[tl];
        
        if (is_large) {
            // Po przekroczeniu MOD wybieramy mnożenie (dla b >= 2)
            if (B[tl] >= 2) x = (x * (B[tl] % MOD)) % MOD;
            else x = (x + (A[tl] % MOD)) % MOD;
        } else {
            // Przed przekroczeniem MOD porównujemy bezpośrednio
            x = max(opt_add, opt_mul);
            if (x >= MOD) {
                is_large = true;
                x %= MOD;
            }
        }
        return;
    }

    int tm = (tl + tr) / 2;
    query(2 * v, tl, tm, ql, qr, x, is_large);
    query(2 * v + 1, tm + 1, tr, ql, qr, x, is_large);
}

int main() {
    // Bardzo ważne dla wydajności przy 500k wejść
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    if (!(cin >> n >> q)) return 0;

    for (int i = 0; i < n; i++) {
        cin >> A[i] >> B[i];
    }

    build(1, 0, n - 1);

    while (q--) {
        long long x;
        int l, r;
        cin >> x >> l >> r;

        if (l >= r) {
            cout << x % MOD << "\n";
            continue;
        }

        bool is_large = (x >= MOD);
        if (is_large) x %= MOD;

        // Zapytanie o przedział [l, r-1]
        query(1, 0, n - 1, l, r - 1, x, is_large);

        cout << x % MOD << "\n";
    }

    return 0;
}