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

const long long M = 1'000'000'007;
long long prefsum[500009], prefilo[500009], prefwei[500009];
long long taa[500009], tab[500009], nextb[500009];

long long fastpow(long long a, long long b)
{
	long long c = a % M, wyn = 1;
	while(b > 0) {
		if(b % 2) wyn = (wyn * c) % M;
		c = (c * c) % M;
		b /= 2;
	}
	return wyn;
}

long long invmod(long long x)
{
	return fastpow(x, M - 2);
}

long long interv_ilo(long long x, long long y)
{
	return (prefilo[y] * invmod(prefilo[x-1])) % M;
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	long long n, q, x, l, r, c, a, b;
	cin>>n>>q;
	prefilo[0] = 1;
	for(int i=1; i<=n; i++) {
		cin>>taa[i]>>tab[i];
		prefsum[i] = prefsum[i-1] + taa[i];
		prefilo[i] = (prefilo[i-1] * tab[i]) % M;
		if(tab[i] == 1) prefwei[i] = (prefwei[i-1] + taa[i]) % M;
		else prefwei[i] = (prefwei[i-1] * tab[i]) % M;
	}
	x = n + 1;
	for(int i=n; i>=0; i--) {
		nextb[i] = x;
		if(tab[i] > 1) x = i;
	}
	
	while(q--) {
		cin>>x>>l>>r;
		c = 0;
		a = nextb[l];
		while(a <= r && c == 0) {
			x += (prefsum[a-1] - prefsum[l]);
			if(x > M) {
				x %= M;
				c = 1;
			}
			if(x + taa[a] > x * tab[a]) x += taa[a];
			else x *= tab[a];
			if(x > M) {
				c = 1;
				x %= M;
			}
			l = a;
			a = nextb[l];
		}
		if(a > r) x = (x + prefsum[r] - prefsum[l]) % M;
		else {
			a = interv_ilo(l+1, r);
			x = (x * a) % M;
			b = (prefwei[r] - ((prefwei[l] * a) % M)) % M;
			while(b < 0) b += M;
			x = (x + b) % M;
		}
		cout<<x<<'\n';
	}
	return 0;
}