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

#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

using namespace std;

#ifdef LOCAL
template<typename A, typename B>
auto&operator<<(auto&o,pair<A, B>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<&","[!i++]<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

using i64 = long long;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<i64, i64>;
using vi = vector<int>;
using vll = vector<i64>;

const int MOD = 1000 * 1000 * 1000 + 7;

inline void add_mod(ll &a, ll b) {
	a += b;
	if (a >= MOD) {
		a -= MOD;
	}
}

inline ll mul_mod(ll a, ll b) {
	return (a * b) % MOD;
}

struct LinFuncTree {
	struct Node {
		ll a, b;  // x -> ax+b (mod MOD)
		Node() : a{1}, b{0} {}
		Node(ll a_, ll b_) : a{a_}, b{b_} {}

		Node Merge(const Node &rhs) const {
			// order: *this; rhs
			// x -> rhs.a * (ax + b) + rhs.b
			const ll ret_a = mul_mod(rhs.a, a);
			ll ret_b = mul_mod(rhs.a, b);
			add_mod(ret_b, rhs.b);
			return Node(ret_a, ret_b);
		}
	};

	vector<Node> nodes;
	int base;

	LinFuncTree(int n) : base{1} {
		while (base < n + 2) { base *= 2; }
		nodes.resize(base * 2);
	}

	void Set(int pos, ll a, ll b) {
		pos += base;
		nodes[pos] = Node(a, b);
		pos /= 2;
		while (pos) {
			nodes[pos] = nodes[pos * 2].Merge(nodes[pos * 2 + 1]);
			pos /= 2;
		}
	}

	Node ComposeInternal(ll L, ll R) const {
		if (L > R) {
			return Node{};
		}
		L += base; R += base;
		if (L == R) {
			return nodes[L];
		}
		Node res_l = nodes[L], res_r = nodes[R];
		while (L / 2 != R / 2) {
			if (L % 2 == 0) {
				res_l = res_l.Merge(nodes[L + 1]);
			}
			if (R % 2 == 1) {
				res_r = nodes[R - 1].Merge(res_r);
			}
			L /= 2;
			R /= 2;
		}
		return res_l.Merge(res_r);
	}

	pll Compose(ll L, ll R) const {
		const Node ans = ComposeInternal(L, R);
		return {ans.a, ans.b};
	}
};

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(14);
	cerr << fixed << setprecision(6);

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

	vll A(n), B(n);
	vll pref_A(n + 1);

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

	const ll max_A = *max_element(ALL(A));

	vi where_next_B(n + 1);
	where_next_B[n] = n;
	for (int i = n - 1; i >= 0; --i) {
		if (B[i] >= 2) {
			where_next_B[i] = i;
		} else {
			where_next_B[i] = where_next_B[i + 1];
		}
	}

	LinFuncTree lin_tree(n);
	for (int i = 0; i < n; ++i) {
		ll mul_by, inc_by;
		if (B[i] == 1) {
			mul_by = 1;
			inc_by = A[i];
		} else {
			mul_by = B[i];
			inc_by = 0;
		}
		lin_tree.Set(i, mul_by, inc_by);
	}

	auto SolveQuery = [&](int L, int R, ll x) {
		debug("start", L, R, x);
		while (L < R && x < max_A) {
			if (B[L] >= 2) {
				x = max(x + A[L], x * B[L]);
				++L;
			} else {
				const int skip_to = min(R, where_next_B[L]);
				assert(skip_to > L);
				x += (pref_A[skip_to] - pref_A[L]);
				L = skip_to;
			}
			debug(L, R, x);
		}

		x %= MOD;
		const auto [comp_mul, comp_inc] = lin_tree.Compose(L, R - 1);
		debug(comp_mul, comp_inc);
		x = mul_mod(x, comp_mul);
		add_mod(x, comp_inc);
		return x;
	};

	for (int qid = 0; qid < q; ++qid) {
		int L, R, x;
		cin >> x >> L >> R;

		cout << SolveQuery(L, R, x) << "\n";
	}
}