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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <bits/stdc++.h>
using namespace std;

constexpr int mod = 1e9 + 7;

inline int add(int a, int b) {
  a += b;
  return (a >= mod) ? a - mod : a;
}

inline int mul(int a, int b) {
  return static_cast<int>(a * static_cast<long long>(b) % mod);
}

class Points {

 public:
  Points(int res) : res_(res) {}

  Points(long long res) : res_(res % mod), big_(res >= mod) {}

  Points& operator+=(const Points& other) {
    big_ |= other.big_ | (res_ + other.res_ >= mod);
    res_ = add(res_, other.res_);
    return (*this);
  }

  friend Points operator+(Points lhs, const Points& rhs) {
    lhs += rhs;
    return lhs;
  }

  Points& operator*=(const Points& other) {
    big_ = (res_ > 0 && other.res_ > 0) && (big_ || other.big_ || res_ >= (mod + other.res_ - 1) / other.res_);
    res_ = mul(res_, other.res_);
    return (*this);
  }

  friend Points operator*(Points lhs, const Points& rhs) {
    lhs *= rhs;
    return lhs;
  }

  Points& apply(int a, int b) {
    // cerr << "[APPLY] " << res_ << ", " << a << ", " << b << endl;
    if (b == 1) {
      // cerr << "b == 1" << endl;
      return (*this) += a;
    }
    if (big_) {
      // cerr << "WEIRD" << endl;
      return (*this) *= b;
    }
    if ((mod + b - 1) / b <= res_) {
      // cerr << "BIG MOD" << endl;
      return (*this) *= b;
    }
    if (res_ + a >= res_ * b) {
      // cerr << "ADDITION?" << endl;
      return (*this) += a;
    }
    else {
      // cerr << "REALLY WEIRD" << endl;
      return (*this) *= b;
    }
  }

  friend ostream& operator<<(ostream& os, const Points& pts) {
    return os << "[DEBUG]: Points(" << pts.res_ << ", " << pts.big_ << "), ";
  }


  bool nice_to_mul() const {
    return res_ != 1 || big_;
  }

  bool is_big() const {
    return big_;
  }

  int operator()() const {
    return res_;
  }

 private:
  int res_;
  bool big_ = false;
};

class Function {

 public:
  static Function from_add(const Points& val) {
    return Function(1, val);
  }

  static Function from_mul(const Points& val) {
    return Function(val, 0);
  }

  Function() : a_(1), b_(0) {}

  friend Function combine(const Function& lhs, const Function& rhs) {
    // cerr << "combine(" << lhs << ", " << rhs << ")" << endl;
    auto func = Function(lhs.a_ * rhs.a_, lhs.b_ * rhs.a_ + rhs.b_);
    // cerr << "result = " << func << endl;
    return func;
  }

  Points operator()(const Points& x) {
    return a_ * x + b_;
  }

  friend ostream& operator<<(ostream& os, const Function& func) {
    return os << "[DEBUG] Function(" << func.a_ << ", " << func.b_ << "), ";
  }

 private:
  Points a_, b_;

  explicit Function(const Points& a, const Points& b) : a_(a), b_(b) {}
};

class FastEvaluator {

 public:
  explicit FastEvaluator(const vector<pair<int, int>>& arr) {
    ss_ = 1;
    while (ss_ < (int) arr.size()) {
      ss_ *= 2;
    }
    tree_.resize(2 * ss_);
    for (int i = 0; i < (int) arr.size(); i++) {
      if (arr[i].second > 1) {
        tree_[ss_ + i] = Function::from_mul(arr[i].second);
      }
      else {
        tree_[ss_ + i] = Function::from_add(arr[i].first);
      }
      // cerr << "PUTTING " << i << " " << tree_[ss_ + i] << endl;
    }
    for (int i = ss_-1; i > 0; i--) {
      tree_[i] = combine(tree_[i*2], tree_[i*2+1]);
      // cerr << "COMBINED " << i << " " << tree_[i] << endl;
    }
  }

  Function aggregate(int l, int r) const {
    return aggregate(l, r, 0, ss_-1, 1);
  }

 private:
  Function aggregate(int a, int b, int l, int r, int x) const {
    if (a <= l && r <= b) {
      // cerr << "HEY! I have been there " << a << " " << b << " " << l << " " << r << " " << tree_[x] << endl;
      return tree_[x];
    }

    int mid = (l + r) / 2;
    Function res;
    if (a <= mid) {
      res = combine(res, aggregate(a, b, l, mid, x*2));
    }
    if (mid < b) {
      res = combine(res, aggregate(a, b, mid+1, r, x*2+1));
    }
    // cerr << "AGGREGATE(" << l << ", " << r << ") = " << res << endl;
    return res;
  }

 private:
  int ss_;
  vector<Function> tree_;
};

class SafeWalker {

 public:
  explicit SafeWalker(const vector<pair<int, int>>& arr) : ptr_(arr.size(), arr.size()), pref_(arr.size() + 1) {
    int wh = arr.size();
    for (int i = (int)arr.size()-1; i >= 0; i--) {
      if (arr[i].second == 1) {
        ptr_[i] = wh;
        pref_[i+1] = arr[i].first;
      }
      else {
        ptr_[i] = i;
        wh = i;
      }
    }
    for (int i = 1; i <= arr.size(); i++) {
      pref_[i] += pref_[i-1];
    }
  }

  Points get_points(int l, int r) const {
    return pref_[r + 1] - pref_[l];
  }

  pair<Points, int> operator()(int wh, int bound) const {
    int dst = min(ptr_[wh], bound+1);
    return make_pair(get_points(wh, dst-1), dst) ;
  }

 private:
  vector<int> ptr_;
  vector<long long> pref_;
};

class UselessAvoider {

 public:
  explicit UselessAvoider(const vector<pair<int, int>>& arr) : ptr_(arr.size(), arr.size()) {
    int wh = arr.size();
    for (int i = (int)arr.size()-1; i >= 0; i--) {
      if (arr[i].second == 1 && arr[i].first == 0) {
        ptr_[i] = wh;
      }
      else {
        ptr_[i] = i;
        wh = i;
      }
    }
  }

  int operator()(int wh, int bound) const {
    return min(ptr_[wh], bound+1);
  }

 private:
  vector<int> ptr_;
  vector<long long> pref_;
};

void solve(int x, int l, int r, const vector<pair<int, int>>& arr, const SafeWalker& safe_walker,
           const FastEvaluator& fast_evaluator, const UselessAvoider& useless_avoider) {

  Points points(x);
  // cerr << "STARTING " << l << " " << r << endl;
  l = useless_avoider(l, r);
  // cerr << "AVOIDED " << l << " " << r << endl;

  if (l <= r) {
    points.apply(arr[l].first, arr[l].second);
    // cerr << points << endl;
    l++;
  }

  while (l <= r && !points.is_big()) {
    auto [add, wh] = safe_walker(l, r);
    points += add;
    l = wh;
    // cerr << points << l << endl;
    if (l <= r) {
      points.apply(arr[l].first, arr[l].second);
      // cerr << points << endl;
      l++;
    }
  }
  if (l <= r) {
    // cerr << "ALL " << l << " " << r << endl;
    Function func = fast_evaluator.aggregate(l, r);
    // cerr << func << endl;
    points = func(points);
    // cerr << points << endl;
  }
  cout << points() << "\n";
}





int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n, q;
  cin >> n >> q;
  vector<pair<int, int>> arr(n);
  for (auto& [a, b] : arr) {
    cin >> a >> b;
  }

  const SafeWalker safe_walker(arr);
  const FastEvaluator fast_evaluator(arr);
  const UselessAvoider useless_avoider(arr);

  while (q--) {
    int x, l, r;
    cin >> x >> l >> r;
    r--;
    solve(x, l, r, arr, safe_walker, fast_evaluator, useless_avoider);
  }
}