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

using namespace std;
#define fors(u, n, s) for(int u = (s); u<(n); u++)
#define foru(u, n) fors(u, n, 0)
#define f first
#define s second
#define vec vector
#define pb push_back
#define sz(x) ((int)x.size())
typedef long long ll;

#define debug(x) cout << __LINE__ << " | " << x << endl
// #define debug(x) {}

const int MOD = 1e9+7;
struct Modular {
  int value;

  Modular(long long v = 0) { value = v % MOD; }
  Modular(long long a, long long b) : value(0){ *this += a; *this /= b;}

  Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;}
  Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;}
  Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;}

  friend Modular mexp(Modular a, long long e) {
    Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; }
    return res;
  }
  friend Modular inverse(Modular a) { return mexp(a, MOD - 2); }

  Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
  friend Modular operator+(Modular a, Modular const b) { return a += b; }
  friend Modular operator-(Modular a, Modular const b) { return a -= b; }
  friend Modular operator-(Modular const a) { return 0 - a; }
  friend Modular operator*(Modular a, Modular const b) { return a *= b; }
  friend Modular operator/(Modular a, Modular const b) { return a /= b; }
  friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;}
  friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;}
  friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;}
};

struct linear{
	Modular a;
	Modular b;	

	linear() {}
	linear(int a_, int b_) : a(Modular(a_)), b(Modular(b_)) {}
	linear(Modular a_, Modular b_) : a(a_), b(b_) {}

	friend linear operator+(const linear &l, const linear &r){
		// l(x) = l.a*x + l.b
		// r(x) = r.a*x + r.b
		// r(l(x)) = r(l.a x + l.b) = r.a l.a x + r.a l.b + r.b
		return linear(r.a * l.a, r.a*l.b + r.b);
	}

	friend linear inv(const linear &f){
		// f(out(x)) = x
		// a * out x + b = x
		// a (out.a x + out.b) + b = x
		// a out.a x + a out.b + b = x
		// x (a out.a - 1) + (a out.b + b) = 0
		// out.a = inv(a), out.b = -b/a
		return linear(inverse(f.a), -f.b/f.a);
	}

	friend linear operator-(const linear &l, const linear &r){
		return inv(l)+r;
	}

	Modular operator[](Modular x){
		return a*x+b;
	}

	ll eval_ll(ll x){
		return ((ll)a.value)*x+((ll)b.value);
	}
};

struct fn{
	ll plus;
	linear a, b;
	bool type; // 0 - plus, 1 - bi	
};

const int N = 6e5;
fn v[N];
int next_bi[N];
ll pref[N];
linear pref_linear[N];

Modular querry(ll x, int l, int r){
	while(true){
		if(l == r) return Modular(x);

		if(next_bi[l] >= r){
			return Modular(x + pref[r] - pref[l]);
		}

		int jmp = next_bi[l];
		x += pref[jmp]-pref[l];
		l = jmp;
		
		if(x >= MOD) return Modular((pref_linear[l]-pref_linear[r])[x]);

		x = max(
			v[l].a.eval_ll(x),
			v[l].b.eval_ll(x)
		);

		l += 1;

		if(x >= MOD) return Modular((pref_linear[l]-pref_linear[r])[x]);
	}
}

void solve() {
	int n, q; cin >> n >> q;

	foru(i, n){
		int a, b; cin >> a >> b;
		if(b == 1){
			v[i].plus = a;
			v[i].a = linear(1, a);
			v[i].type = false;
		} else {
			v[i].plus = 0;
			v[i].a = linear(b, 0);
			v[i].b = linear(1, a);
			v[i].type = true;
		}
	}

	next_bi[n] = n;
	for(int i = n-1; i>=0; i--){
		if(v[i].type) next_bi[i] = i;
		else next_bi[i] = next_bi[i+1];
	}

	pref[0] = 0;
	foru(i, n) pref[i+1] = pref[i] + v[i].plus;

	pref_linear[0] = linear(1, 0);
	foru(i, n) pref_linear[i+1] = pref_linear[i] + v[i].a;

	foru(_i, q){
		ll x; int l, r;
		cin >> x >> l >> r;
		cout << querry(x, l, r) << endl;
	}
}

int main()
{
	cin.tie(0); cout.tie(0);
	ios_base::sync_with_stdio(0);

	srand(time(NULL));

	solve();

	return 0;
}