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
// PA2026, @mjm, r1a-grm

#include <cstdio>
#include <vector>
using namespace std;
inline int nextInt() { int n; scanf("%d", &n); return n; }
using lol = long long;
inline lol myMax(lol a, lol b) { return a >= b ? a : b; }

const int p = 1000000000 + 7;
inline int addMod(int a, int b) {
	a += b;
	if (a >= p) a -= p;
	return a;
}
inline int mulMod(lol a, int b) {
	return (a * b) % p;
}

const int N = 500000 + 9;

int n;
int a[N];
int b[N];
int c[N];
int d[N];
lol ps[N]; // prefix sum of a
int nextAdvanced[N];
int revc[N];

int powMod(lol a, int n) {
	lol res = 1;
	while (n >= 2) {
		if (n % 2)
			res = mulMod(res, a);
		a = mulMod(a, a);
		n >>= 1;
	}
	if (n == 1)
		res = mulMod(res, a);
	return res;
}

int query() {
	lol x = nextInt();
	int be = nextInt();
	int en = nextInt();
	while (x < p && be < en) {
		if (nextAdvanced[be] == be) {
			lol x1 = x + a[be];
			lol x2 = x * b[be];
			x = myMax(x1, x2);
			++be;
			continue;
		}
		int step = nextAdvanced[be];
		if (step > en)
			step = en;
		x += ps[step] - ps[be];
		be = step;
	}
	x %= p;
	if (be == en)
		return x;
	// x == c[be] * x0 + d[be]
	// x0 := (x - d[be]) / c[be]
	x -= d[be];
	if (x < 0) x += p;
	if (0 == revc[be])
		revc[be] = powMod(c[be], p - 2);
	x = mulMod(x, revc[be]);
	// x := x0
	x = mulMod(x, c[en]);
	x = addMod(x, d[en]);
	return x;
}

int main() {
	n = nextInt();
	int q = nextInt();
	c[0] = 1;
	d[0] = 0;
	ps[0] = 0;
	for (int i = 0; i < n; ++i) {
		a[i] = nextInt();
		ps[i + 1] = ps[i] + a[i];
		b[i] = nextInt();
		if (1 == b[i]) {
			c[i + 1] = c[i];
			d[i + 1] = addMod(d[i], a[i]);
		} else {
			c[i + 1] = mulMod(c[i], b[i]);
			d[i + 1] = mulMod(d[i], b[i]);
		}
	}
	nextAdvanced[n] = n;
	for (int i = n - 1; i >= 0; --i) {
		if (1 == b[i]) {
			nextAdvanced[i] = nextAdvanced[i + 1];
		} else {
			nextAdvanced[i] = i;
		}
	}

	for (int i = 0; i < q; ++i) {
		int res = query();
		printf("%d\n", res);
	}

	return 0;
}