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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)


#define VI vector<int>
#define PII pair<int,int>
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define lint long long int


#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; }
#define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; }
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }}


#define make( x) int (x); scanf("%d",&(x));
#define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y));
#define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z));
#define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t));
#define IOS ios_base::sync_with_stdio(0)
#define HEAP priority_queue


#define read( x) scanf("%d",&(x));
#define read2( x, y) scanf("%d%d",&(x),&(y));
#define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z));
#define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t));


using namespace std;

int mod = 1e9 + 7;

int pls[500005];
int razy[500005];
PII pref[500005];
int nastepny_dobry[500005];
lint suma_zlych[500005]; 

int powmod(int a, int n) {
	if (n == 0) return 1;
	if (a == 0) return 0;
	int sq = powmod(a,n/2);
	sq = (sq * 1LL * sq) % mod;
	if (n % 2 == 0) return sq; 
	return (sq * 1LL * a) % mod;
}

int inv(int a) {
	return powmod(a, mod - 2);
}

int doit(int x, int l, int r, bool duzy) {
	// cout << x << " " << l << " " << r << " " << duzy << endl;
	if (l > r) return x;
	if (duzy) {
		// już dostatecznie duży 
		// wykonaj [l,...r]
		int a = pref[r+1].st, b = pref[r+1].nd;
		int c = pref[l].st, d = pref[l].nd;
		int invc = inv(c);
		
		// cout << a << " " << b << " " << c <<" " << d << endl;
		// cout << invc << endl;

		lint m = (a*1LL*invc)%mod;
		lint p = ((b-m*d)%mod+mod)%mod;

		// cout << m << " " << p << endl;

		int ans = (x*1LL*m+p)%mod;
		return ans;
	} else {
		if (razy[l] != 1) {
			lint pos1 = x * 1LL * razy[l];
			lint pos2 = x + pls[l];
			lint ja = max(pos1, pos2);
			if (ja >= mod) return doit(ja%mod, l+1, r, true);
			else return doit(ja, l+1,r,false);
		} else {
			int nxt = min(nastepny_dobry[l],r+1);
			// wykonaj [l,l+1,...,nxt-1]
			lint ja = x + suma_zlych[nxt-1]-(l==0?0:suma_zlych[l-1]); 
			if (ja >= mod) return doit(ja%mod, nxt, r, true);
			else return doit(ja,nxt,r,false);
		}
	}
}

int main() {
	make2(n, q);
	FOR(i,0,n) {
		make2(a,b);
		pls[i] = a;
		razy[i] = b;
	}
	pref[0] = {1,0};
	FOR(i,0,n) {
		if (razy[i] == 1) {
			pref[i+1] = {pref[i].st, (pref[i].nd+pls[i]) % mod};
		} else {
			pref[i+1] = {(pref[i].st * 1LL * razy[i])%mod, (pref[i].nd * 1LL * razy[i])%mod};
		}
	}
	nastepny_dobry[n-1] = n;
	for (int i = n-2; i >= 0; i--) {
		if (razy[i+1] == 1) nastepny_dobry[i]=nastepny_dobry[i+1];
		else nastepny_dobry[i]=i+1;
	}
	suma_zlych[0] = ((razy[0] == 1) ? pls[0] : 0);
	FOR(i,1,n) {
		if (razy[i] != 1) {
			suma_zlych[i] = suma_zlych[i-1];
		} else {
			suma_zlych[i] = (suma_zlych[i-1] + pls[i]);
		}
	}
	
	while (q--) {
		make3(x,l,r);
		printf("%d\n", doit(x,l,r-1, false));
	}

}