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
#include <bits/stdc++.h>
#define rep(i, p, k) for (int i = (p); i < (k); ++i)
#define all(v) (v).begin(), (v).end()
#define ll long long
#ifndef DEBUG
#define debug(...)
#else
#include "debug.h"
#endif
using namespace std;

template <typename F>
struct SparseTable {
  vector<vector<F>> t;
  SparseTable(vector<F> v) {
    int n = (int)v.size();
    t.emplace_back(v);
    for (int i = 1; (1 << i) < n; i++) {
      int h = 1 << i;
      t.emplace_back(v);
      for (int j = h; j < n; j += 2 * h) {
        for (int k = j - 2; k >= j - h; k--)
          t[i][k] = F::merge(t[i][k], t[i][k + 1]);
        for (int k = j + 1; k < j + h && k < n; k++)
          t[i][k] = F::merge(t[i][k - 1], t[i][k]);
      }
    }
  }
  F query(int l, int r) {
    if (l == r) return t[0][l];
    int h = 31 - __builtin_clz(r ^ l);
    if (l == (1 << h)) return t[h][r];
    if (r == (1 << h) - 1) return t[h][l];
    return F::merge(t[h][l], t[h][r]);
  }
};

const int P = 1e9 + 7;

int add(int a, int b) { return a + b >= P ? a + b - P : a + b; }
int mul(int a, int b) { return int(1LL * a * b % P); }

struct C {
  int a, b;
  static C merge(C l, C r) { return {add(r.a, mul(l.a, r.b)), mul(l.b, r.b)}; }
  int apply(ll x) { return add(mul(b, int(x % P)), a); }
};

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n, q;
  cin >> n >> q;
  vector<int> A(n), B(n);
  vector<ll> pre(n + 1);
  rep(i, 0, n) cin >> A[i] >> B[i];
  rep(i, 0, n) pre[i + 1] = pre[i] + A[i];
  vector<int> nxt(n + 1, n);
  for (int i = n - 1, c = n; i >= 0; i--) {
    if (B[i] != 1) c = i;
    nxt[i] = c;
  }
  vector<C> d(n);
  rep(i, 0, n) d[i] = B[i] == 1 ? C{A[i], 1} : C{0, B[i]};
  SparseTable<C> st(d);
  while (q--) {
    ll x;
    int l, r;
    cin >> x >> l >> r;
    while (true) {
      if (nxt[l] >= r) {
        cout << (x + pre[r] - pre[l]) % P << '\n';
        break;
      }
      if (x >= P) {
        cout << st.query(l, r - 1).apply(x) << '\n';
        break;
      }
      x += pre[nxt[l]] - pre[l];
      l = nxt[l];
      if(x >= P) continue;
      x = max(x + A[l], x * B[l]);
      l++;
    }
  }
}