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

#include <utility>

using namespace std;

#define endl "\n"
#define ll long long

constexpr int MOD = 1e9 + 7;

struct cc {
  ll mul;
  ll add;
  cc() : mul(1), add(0) {}
  cc(ll mul, ll add) : mul(mul), add(add) {}
};

const cc ID = {1, 0};

struct tree {
  int size = 1;
  vector<cc> arr;

  static cc combine(cc fst, cc snd) {
    cc ans(0,0);
    ans.mul = fst.mul * snd.mul % MOD;
    ans.add = fst.add * snd.mul + snd.add % MOD;
    return ans;
  }

  void pull(int root) {
    arr[root].mul = arr[2*root].mul * arr[2*root].mul % MOD;
    arr[root].add = arr[2*root].add * arr[2*root+1].mul + arr[2*root+1].add % MOD;
  }

  void build_tree(vector<cc> vec) {
    while(size <= vec.size()) {
      size *= 2;
    }
    arr.resize(2*size);
    for(int i = 0; i < vec.size(); i++) {
      arr[size + i] = vec[i];
    }

    for(int i = size - 1; i >= 1; i--) {
      pull(i);
    }
  }

  tree(vector<cc> vec) {
    build_tree(std::move(vec));
  }

  cc query(int root, int l, int r, const int L, const int R) {
    if(L <= l && r <= R) return arr[root];
    if(R <= l || r <= L) return ID;
    int mid = (l + r) / 2;
    cc fst = query(2*root, l, mid, L, R);
    cc snd = query(2*root + 1, mid + 1, r, L, R);

    return combine(fst, snd);
  }
};

void solve() {
  struct input {
    int a, b;
    input(int a, int b) : a(a), b(b) {}
  };

  vector<input> in;
  int n, q;
  cin >> n >> q;
  for(int i = 0; i < n; i++) {
    int a, b;
    cin >> b >> a;
    in.emplace_back(a, b);
  }

  struct choice {
    bool choose;
    int a, b, add;
    int pos;

    choice(int a, int b, int pos) : choose(true), a(a), b(b), add(0), pos(pos) {}
    choice(int add, int pos) : choose(false), a(0), b(0), add(add), pos(pos) {}
  };

  vector<choice> ch;
  // how many to add before first choice struct
  // when is next choice struct
  vector<int> find_start(n);
  vector<ll> add_left(n);
  vector<ll> add_right(n);
  int acc = 0;
  for(int i = 0; i < n; i++) {
    if(in[i].a <= 1) {
      if(i!=0) add_left[i] = add_left[i-1] + in[i].b;
      else add_left[i] = in[i].b;
      acc += in[i].b;
    }
    else {
      if(acc) ch.emplace_back(acc, i);
      ch.emplace_back(in[i].a, in[i].b, i);

      acc = 0;
      find_start[i] = i;

      add_right[i] = 0;
      add_left[i] = 0;

      int j = i-1;
      while(in[j].a <= 1) {
        find_start[j] = i;
        add_right[j] = in[j].b + add_right[j+1];
        j --;
      }
    }
  }

  // build a tree
  vector<cc> treearr(n);
  for(int i = 0; i < n; i++) {
    if(in[i].a >= 2) treearr[i] = {in[i].a, 0};
    else treearr[i] = {1, in[i].b};
  }

  tree TREE(treearr);


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

    x += add_right[l];
    l = find_start[l];

    while(x <= MOD) {
      ll poss_a = x * ch[l].a;
      ll poss_b = x + ch[l].b;
      x = max(poss_a, poss_b);
      if(l >= r) break;
      l++;

      int next = find_start[l];
      if(l > r) {
        x += add_left[r];
        break;
      }

      l = next;
      x += add_left[l - 1];

      x %= MOD;
    }

    x %= MOD;
    l++;
    cc rest = TREE.query(1, 0, TREE.size, l, r+1);
    x = x* rest.mul + rest.add % MOD;

    cout << x << endl;
  }

}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t = 1;
  //cin >> t;
  while (t--) {
    solve();
  }
}