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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#ifdef LOC
#include "debuglib.h"
#else
#define deb(...)
#define DBP(...)
#endif
using namespace std;
using   ll         = long long;
using   Vi         = vector<int>;
using   Pii        = pair<int, int>;
#define pb           push_back
#define mp           make_pair
#define x            first
#define y            second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x)   for (auto& a : (x))
#define all(x)       (x).begin(), (x).end()
#define sz(x)        int((x).size())

#ifndef LOC
#ifdef assert
#undef assert
#endif
#define assert(...)
#endif

constexpr int MAX_N = 3e5+100;
constexpr int BLOCK = 2048;
constexpr int SMALL_RUN = 256;
constexpr int MOD = 1e9+7;

ll modPow(ll a, ll e, ll m) {
	ll t = 1 % m;
	while (e) {
		if (e % 2) t = t*a % m;
		e /= 2; a = a*a % m;
	}
	return t;
}

struct Zp {
	ll x;
	Zp() : x(0) {}
	Zp(ll a) : x(a%MOD) { if (x < 0) x += MOD; }
	#define OP(c,d) Zp& operator c##=(Zp r) { x = x d; return *this; } Zp operator c(Zp r) const { Zp t = *this; return t c##= r; }
	OP(+, +r.x - MOD*(x+r.x >= MOD));
	OP(-, -r.x + MOD*(x-r.x < 0));
	OP(*, *r.x % MOD);
	OP(/, *r.inv().x % MOD);
	Zp inv() const { return pow(MOD-2); }
	Zp pow(ll e) const{ return modPow(x,e,MOD); }
	void print() { cerr << x; }
};

struct WeirdStruct {
	struct Block {
		bool bits[BLOCK];
		ll pref[BLOCK+1];
		int ones[BLOCK+1];

		Block(int pos, bool b) {
			fill(bits, bits+BLOCK, b);
			pref[0] = 0;
			ones[0] = 0;
			updatePref(pos, 0);
		}

		void updatePref(int pos, int from) {
			int sign = (ones[from]%2 ? -1 : 1);
			rep(i, from, BLOCK) {
				ones[i+1] = ones[i] + bits[i];
				pref[i+1] = pref[i];
				if (bits[i]) {
					pref[i+1] += (pos+i) * sign;
					sign *= -1;
				}
			}
		}
	};

	vector<Block*> segs[2];
	vector<ll> pref[2];
	Vi ones[2];
	int minBlock, maxBlock;

	WeirdStruct() {}

	WeirdStruct(int n) {
		int k = n/BLOCK + 2;
		rep(i, 0, 2) {
			segs[i].resize(k);
			rep(j, 0, k) {
				segs[i][j] = new Block(j*BLOCK, i);
			}
		}
		minBlock = 0;
		maxBlock = k;
		updatePref();
	}

	void setWindow(int begin, int end) {
		minBlock = max(begin/BLOCK - 1, 0);
		maxBlock = min(end/BLOCK + 1, sz(segs[0]));
	}

	void updatePref() {
		rep(i, 0, 2) {
			auto& cSegs = segs[i];
			auto& cPref = pref[i];
			auto& cOnes = ones[i];
			cPref.resize(sz(cSegs)+1);
			cOnes.resize(sz(cSegs)+1);
			cPref[minBlock] = cOnes[minBlock] = 0;
			rep(j, minBlock, maxBlock) {
				cOnes[j+1] = cOnes[j] + cSegs[j]->ones[BLOCK];
				cPref[j+1] = cPref[j] + cSegs[j]->pref[BLOCK] * (cOnes[j]%2 ? -1 : 1);
			}
		}
	}

	void flip(int from, int to) {
		int full1 = from / BLOCK, rem1 = from % BLOCK;
		int full2 = to / BLOCK, rem2 = to % BLOCK;
		if (full1 == full2) {
			rep(i, 0, 2) {
				Block* seg = segs[i][full1];
				rep(j, rem1, rem2) {
					seg->bits[j] = !seg->bits[j];
				}
				seg->updatePref(full1*BLOCK, rem1);
			}
		} else {
			rep(i, full1+1, full2) {
				swap(segs[0][i], segs[1][i]);
			}
			rep(i, 0, 2) {
				Block* seg = segs[i][full1];
				rep(j, rem1, BLOCK) {
					seg->bits[j] = !seg->bits[j];
				}
				seg->updatePref(full1*BLOCK, rem1);
				seg = segs[i][full2];
				rep(j, 0, rem2) {
					seg->bits[j] = !seg->bits[j];
				}
				seg->updatePref(full2*BLOCK, 0);
			}
		}
	}

	pair<ll, int> query(int k, int b) {
		int full = k / BLOCK;
		int rem = k % BLOCK;
		ll p = pref[b][full] + segs[b][full]->pref[rem] * (ones[b][full]%2 ? -1 : 1);
		int o = ones[b][full] + segs[b][full]->ones[rem];
		return {p, o};
	}

	pair<ll, int> query(int k, int l, int b) {
		auto p = query(k, b), q = query(l, b);
		return {(q.x-p.x) * (p.y%2 ? -1 : 1), q.y-p.y};
	}
};

int n, q;
Vi branch, distEven;
vector<Zp> siz, distSum;
WeirdStruct ds;
int lastC;

bitset<MAX_N> smallRuns[SMALL_RUN];
vector<Pii> bigRuns;

struct Query {
	int a, b, c, i;
	bool operator<(const Query& r) const {
		return c < r.c;
	}
};

int distEvenPartial(int i) {
	if (branch[i] % 2 == 1) {
		return 0;
	}
	return distEven[i+1]+1;
}

Vi add;

Zp get(int a, int b, int c) {
	if (a < b) swap(a, b);
	assert(a >= b && b >= c);
	assert(lastC <= c);

	while (lastC < c) {
		ds.flip(n-lastC, n-lastC+distEvenPartial(lastC)+1);
		lastC++;
	}

	ds.setWindow(n-c, n*2+10);
	ds.updatePref();

	add.clear();

	rep(i, 0, SMALL_RUN) {
		if (smallRuns[i][a] != smallRuns[i][b]) {
			add.pb(n-c+i);
		}
	}

	each(r, bigRuns) {
		if (r.y >= b && r.y < a) {
			if (!add.empty() && add.back() == n-c+r.x) {
				add.pop_back();
			} else {
				add.pb(n-c+r.x);
			}
		}
	}

	auto insertAdd = [&](int k) {
		rep(i, 0, sz(add)) {
			if (add[i] >= k) {
				if (add[i] == k) {
					add.erase(add.begin()+i);
				} else {
					add.insert(add.begin()+i, k);
				}
				return;
			}
		}
		add.pb(k);
	};

	if (a != b || a == c) {
		insertAdd(n-c+distEven[a]);
	}

	if (a != b && b != c) {
		insertAdd(n-c+distEven[b]);
	}

	if (a != c && b != c) {
		insertAdd(n-c+distEven[c]);
	}

	ll ret = 0;
	int ones = 0;
	int cur = n*2+5;
	bool flip = 0;

	auto moveCur = [&](int newCur) {
		assert(newCur < cur);
		auto p = ds.query(newCur+1, cur+1, flip);
		ones += p.y;
		ret += (ones%2 ? p.x : -p.x);
		cur = newCur;
	};

	for (int i = sz(add)-1; i >= 0; i--) {
		moveCur(add[i]);
		flip ^= 1;
	}
	moveCur(n-c-1);

	ret *= 2;
	if (ones % 2) {
		ret += a + b - n*2;
	}
	return ret;
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cout << fixed << setprecision(10);

	cin >> n >> q;
	branch.resize(n-1);
	each(k, branch) cin >> k;

	distEven.resize(n);
	branch.pb(0);
	for (int i = n-1; i >= 0; i--) {
		distEven[i] = (branch[i]%2 ? distEven[i+1]+1 : 0);
	}

	siz.resize(n, 1);
	distSum.resize(n);

	for (int i = n-2; i >= 0; i--) {
		distSum[i] = (distSum[i+1]+siz[i+1]) * branch[i];
		siz[i] = siz[i+1]*branch[i] + 1;
	}

	rep(i, 1, n) {
		distSum[i] = distSum[i-1] + siz[0] - siz[i]*2;
	}

	rep(i, 0, n-1) {
		int dist = distEvenPartial(i);
		if (dist < SMALL_RUN) {
			smallRuns[dist].flip(i+1);
		} else {
			bigRuns.pb({dist, i});
		}
	}

	each(vec, smallRuns) {
		rep(i, 1, n+1) {
			vec[i] = vec[i-1] ^ vec[i];
		}
	}

	sort(all(bigRuns));
	ds = {n*2+20};

	vector<Query> queries(q);

	rep(i, 0, q) {
		auto& e = queries[i];
		cin >> e.a >> e.b >> e.c;
		e.a--; e.b--; e.c--;
		e.i = i;
	}

	vector<Zp> ans(q);
	sort(all(queries));

	each(e, queries) {
		ans[e.i] = distSum[e.a] - distSum[e.b];
		ans[e.i] += get(e.a, e.b, e.c);
		ans[e.i] /= 2;
	}

	each(a, ans) {
		cout << a.x << '\n';
	}
	return 0;
}