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

using namespace std;

constexpr long mod = 1000000007LL;

int main() {
  ios_base::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;

  vector<pair<long, long>> game(n);
  for(pair<long, long>& p : game) {
    cin >> p.first >> p.second;
  }

  while (q--) {
    long res;
    bool overflow = false;
    int l, r;
    cin >> res >> l >> r;
    for(int i = l; i < r; i++) {
      if(res > mod) {
        overflow = true;
        res %= mod;
      }
      if(overflow) {
        if (game[i].second > 1) {
          res = (res * game[i].second) % mod;
        } else {
          res += game[i].first;
        }
      } else {
        if(res + game[i].first > res * game[i].second) {
          res += game[i].first;
        } else {
          res *= game[i].second;
        }
      }
    }
    cout << res % mod << endl;
  }

}