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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
#include<cassert>
using namespace std;
#define y0 y0z
#define y1 y1z
#define yn ynz
#define j0 j0z
#define j1 j1z
#define jn jnz
#define tm tmz
#define buli(x) (__builtin_popcountll(x))
#define bur0(x) (__builtin_ctzll(x))
#define bul2(x) (63-__builtin_clzll(x))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define per(i,a,b) for (int i=(b)-1,_ed=(a);i>=_ed;i--)
#define forg(i,gu) for (int i=gu;~i;i=e[i].next)
#define pw(x) ((ll(1))<<(x))
#define upmo(a,b) (((a)=((a)+(b))%mo)<0?(a)+=mo:(a))
#define mmo(a,b) (((a)=1ll*(a)*(b)%mo)<0?(a)+=mo:(a))
void getre(){int x=0;printf("%d\n",1/x);}
void gettle(){int res=1;while(1)res<<=1;printf("%d\n",res);}
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
template<typename T,typename S>inline bool upmin(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool upmax(T&a,const S&b){return a<b?a=b,1:0;}
template<typename N,typename PN>inline N flo(N a,PN b){return a>=0?a/b:-((-a-1)/b)-1;}
template<typename N,typename PN>inline N cei(N a,PN b){return a>0?(a-1)/b+1:-(-a/b);}
template<typename N>N gcd(N a,N b){return b?gcd(b,a%b):a;}
template<typename N>inline int sgn(N a){return a>0?1:(a<0?-1:0);}
#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)
#define lld "%I64d"
#else
#define lld "%lld"
#endif
inline void gn(long long&x){
	int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');
	while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;
}
inline void gn(int&x){long long t;gn(t);x=t;}
inline void gn(unsigned long long&x){long long t;gn(t);x=t;}
inline void gn(double&x){double t;scanf("%lf",&t);x=t;}
inline void gn(long double&x){double t;scanf("%lf",&t);x=t;}
inline void gs(char *s){scanf("%s",s);}
inline void gc(char &c){while((c=getchar())>126 || c<33);}
inline void pc(char c){putchar(c);}
#ifdef JCVB
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
typedef long long ll;
typedef double db;
inline ll sqr(ll a){return a*a;}
inline db sqrf(db a){return a*a;}
const int inf=0x3f3f3f3f;
//const ll inf=0x3f3f3f3f3f3f3f3fll;
const db pi=3.14159265358979323846264338327950288L;
const db eps=1e-6;
const int mo=1e9+7;
//int qp(int a,ll b){int n=1;do{if(b&1)n=1ll*n*a%mo;a=1ll*a*a%mo;}while(b>>=1);return n;}

const int N = 311111;
const int inv2 = (1+mo)/2;
int n,q;
int a[3*N]={0}; // a[1..n-1]
int nexteven[3*N];
int asubtreesize[3*N];
int totsizeparity=0;
int totdisheight[3*N];

int lastzero[N];
void inittotdis() {
	asubtreesize[n] = 1;
	totsizeparity = 1;
	per(i,1,n) {
		asubtreesize[i] = (1ll*a[i] * asubtreesize[i+1]+1)%mo;
		totsizeparity = (1ll*a[i]*totsizeparity + 1)%2;
	}
	int prefixprod=1;

	int distsum=0; // to root

	rep(i,1,n+1) {
		upmo(distsum, 1ll*(i-1)*prefixprod);
		mmo(prefixprod,a[i]);
	}
	totdisheight[1] = distsum;
	rep(i,2,n+1) {
		upmo(distsum,  (asubtreesize[1]-2ll*asubtreesize[i]));
		totdisheight[i] = distsum;
	}

}
struct query{
	int la,lb,lc;
	int id;
	int operator<(const query&a)const{
		return lc<a.lc;
	}
}queries[N];

int queryans[N];

int mineveni;


int leftds[2][N*3];
unsigned leftdsbit[N*3];

inline void bitupd(int* bit, int x,int del) {
	for(;x<=3*n;x+=x&-x)bit[x]+=del;
}
inline int bitque(int* bit,int x) {
	int ans=0;
	for(;x>0;x-=x&-x)ans+=bit[x];
	return ans;
}

void _leftdsupd(int x) { //x<=3n;

	int id = x/32;
	int wei = x%32;
	int del=0;
	if((leftdsbit[id]>>wei)&1) del=-1;
	else del = 1;
	leftdsbit[id]^=((unsigned)1)<<wei;

	bitupd(leftds[x&1], x,del);
}
void leftdsupdate(int h) { // h<=n, ed<=n
	int ed = nexteven[h];
	_leftdsupd(n+h);
	_leftdsupd(n+h-(ed-h)); // <=3n
	//leftds[n+h]^=1;
	//leftds[n+h - (ed-h)]^=1;  //ed<=n
}
unsigned int leftbitask(int x) {// x.. x+31;  x+31 is high bit
	int id = x/32;
	int wei = x%32;
	unsigned int lowbits = ((unsigned int)(leftdsbit[id]))>>wei;
	unsigned int highbits = ((unsigned int)(leftdsbit[id+1]))<<(32-wei);
	if(wei==0)highbits=0;
	return lowbits|highbits;


	/*unsigned int ret=0;
	unsigned int ret2=0;
	rep(j,0,32-wei) {
		unsigned int coun = (leftdsbit[id]>>(wei+j))&1;
		ret|=coun<<j;
	}
	assert(ret==lowbits);
	rep(j,0,wei) {
		unsigned int coun = (leftdsbit[id+1]>>(j))&1;
		ret2|=coun<<(j+32-wei);
	}
	if(highbits!=ret2) {
		cout<<leftdsbit[id+1]<<" "<<highbits<<" "<<ret2<<" ";
		unsigned int f = 1323246662;
		cout<<f<<" ";
		unsigned int g=f<<(31-wei);
		cout<<g<<" ";
		f=f<<(32-wei);
		cout<<f;
	}
	assert(highbits==ret2);
	return ret|ret2;*/
}


const int B=120;
const int stepsize =B*32;

/*int to_query_force(int la, int lb,int lc) { 
	int lef = min(la,lb)+1,rig = max(la,lb);

	int totparity=0;
	if((rig-lef)%2!=0)totparity++; // dis = 0;

	static int dieone[N]={0};
	rep(i,1,lc+n-2+1)dieone[i]=0;
	
	rep(i,lef,rig+1){
		int lev = nexteven[i]-i+1;
		dieone[lev]^=1;
	}
	int alivecnt = (rig-lef+1)%2;
	for (int lev = 1;lev<= lc+n-2;lev++) {
		int coun = leftds[n+lc-lev+1];
		alivecnt^=dieone[lev];
		coun^=alivecnt;
		totparity+=coun;
	}

	int D = la-lc+lb-lc;
	int retans=0;
	if(totsizeparity) {
		int num = lc+n-1;
		totparity = num-totparity;

		retans = ((1ll*(totdisheight[la]+totdisheight[lb])%mo*inv2  - totparity)%mo+mo)%mo;
		retans = ((retans-1ll*D*inv2)%mo+mo)%mo;
		retans = ((totdisheight[la] - retans)%mo+mo)%mo;
	}else {
		retans = ((1ll*(totdisheight[la]+totdisheight[lb])%mo*inv2  - totparity)%mo+mo)%mo;
		retans = ((totdisheight[la] - retans)%mo+mo)%mo;
	}
	return retans;
}
*/


unsigned int anex32[int(N*1.1)]={0};
unsigned int prefixanex[B+1][int(N*1.1)];

int canzero[N]; int canzerotot=0; // 300000 / 3200= 100


int segbound[2][N*3]; int segboundsize[2];
void addseg(int x,int up) {
	if (x>up)return;
	if((x&1)!=(up&1)) up-=1;
	segbound[x&1][++segboundsize[x&1]] = x;
	segbound[x&1][++segboundsize[x&1]] = up+2;
}
int to_query(int la,int lb,int lc) {
	int lef = min(la,lb)+1,rig = max(la,lb);

	int totparity=0;
	if((rig-lef)%2!=0)totparity++; // dis = 0;


	// suan 1<=lev <= stepsize

	int up_force = min(stepsize, lc+n-2); // >=1

	rep(b,0,B) {//b*32 + 1 .. (b+1)*32
		if((b+1)*32<=up_force) {
			unsigned int str = prefixanex[b][rig]^prefixanex[b][lef-1];
			int require_l = n+lc-((b+1)*32)+1;
			unsigned int leftqueans = leftbitask(require_l);
			//
			//unsigned int leftqueans=0;

			//for (int lev = b*32+1, jj=31;lev<= (b+1)*32;lev++,jj--) {
				//int toque = n+lc-lev+1;
				//unsigned int coun = (leftdsbit[toque/32]>>(toque%32))&1;
				//leftqueans|=coun<<jj;
				//totparity+=coun;
			//}
			totparity+=buli(leftqueans^str);

			/*unsigned int str = prefixanex[b][rig]^prefixanex[b][lef-1];
			for (int lev = b*32+1, jj=31;lev<= (b+1)*32;lev++,jj--) {
				int toque = n+lc-lev+1;
				int coun = (leftdsbit[toque/32]>>(toque%32))&1;
				coun^=((str>>jj)&1);
				totparity+=coun;
			}*/
		}else {
			unsigned int str = prefixanex[b][rig]^prefixanex[b][lef-1];
			//int require_l = n+lc-((b+1)*32)+1;
			//int require_r = n+lc - (b*32+1) +1;
			//unsigned int leftqueans = leftbitask(require_l);
			//coun^=buli(leftqueans^str);
			for (int lev = b*32+1, jj=31;lev<= up_force;lev++,jj--) {
				int toque = n+lc-lev+1;
				int coun = (leftdsbit[toque/32]>>(toque%32))&1;
				coun^=((str>>jj)&1);
				totparity+=coun;
				//printf("%d %d\n",coun, ((str>>jj)&1));
			}
		}
	}
	//  i .. i +stepsize - 1 all ok
	if (lc+n-2>stepsize) {
		// stepsize+1 <= lev <= lc+n-2

		segboundsize[0]=segboundsize[1]=0;
		rep(zid,1,canzerotot+1) {
			int z = canzero[zid];
			if(z<lef)continue;

			int prez = lastzero[z];

			int canleft = max(prez+1,lef);
			//canright + stepsize - 1 <= z -1
			int canright = min(rig, z - stepsize);
			if(canleft>canright) continue;
			int canleftdie = z - canleft + 1;
			int canrightdie = z - canright + 1;
			// canright die at lev = z - canright + 1     ( z - z + stepsize + 1)

			// from stepsize+1 to canrightdie-1 : all live

			//assert(0);
			if((canright-canleft+1)&1) {
				addseg(stepsize+1, canrightdie-1);
				addseg(stepsize+2, canrightdie-1);
				addseg(canrightdie+1, canleftdie-1);
			} else {
				addseg(canrightdie,canleftdie-1);
			}
			// at canrightdie: count = (canright-canleft + 1) - 1
			// at canleftdie: count = 0
		}
		rep(par,0,2) {
			sort(segbound[par] +1, segbound[par]+1+segboundsize[par]);

			int tosumleft = stepsize+1, tosumright = lc+n-2;
			if((tosumleft&1)!=par)tosumleft++;
			if((tosumright&1)!=par)tosumright--;

			int curxor = 0;
			segbound[par][segboundsize[par]+1] = tosumright+2;
			for (int j=1;j<=segboundsize[par]+1;j++) {
				if(j<segboundsize[par] && segbound[par][j+1] == segbound[par][j]) {
					j++;
					continue;
				}
				int sumr = min(tosumright, segbound[par][j]-2);
				if(tosumleft<=sumr) {
					int leftdsl = n+lc-(sumr)+1;
					int leftdsr = n+lc-(tosumleft)+1;
					int x = bitque(leftds[leftdsl&1],leftdsr) - bitque(leftds[leftdsl&1],leftdsl-1);
					if(curxor==1) {
						x = (sumr - tosumleft)/2+1 - x;
					}
					totparity += x;
				}
				curxor^=1;
				tosumleft= max(tosumleft,sumr+2);
			}
			// stepsize+1 <= lev <= lc+n-2
		}

	}
	//printf("%d\n",totparity);
	int D = la-lc+lb-lc;
	int retans=0;
	if(totsizeparity) {
		int num = lc+n-1;
		totparity = num-totparity;

		retans = ((1ll*(totdisheight[la]+totdisheight[lb])%mo*inv2  - totparity)%mo+mo)%mo;
		retans = ((retans-1ll*D*inv2)%mo+mo)%mo;
		retans = ((totdisheight[la] - retans)%mo+mo)%mo;
	}else {
		retans = ((1ll*(totdisheight[la]+totdisheight[lb])%mo*inv2  - totparity)%mo+mo)%mo;
		retans = ((totdisheight[la] - retans)%mo+mo)%mo;
	}
	return retans;
}
void prepare32() {
	rep(i,1,n+1) {
		int cur = a[i]%2;
		rep(j,0,32) {
			cur&=(a[i+j]%2);
			anex32[i]|=cur<<(31-j);
		}
		prefixanex[0][i] = anex32[i];
	}
	rep(j,1,B) {
		rep(i,1,n+1) {
			if(prefixanex[j-1][i]&1) {// continue;
				prefixanex[j][i] = prefixanex[j-1][i+32];
			} else {
				prefixanex[j][i] = 0;
			}
		}
	}
	rep(j,0,B) {
		rep(i,1,n+1) prefixanex[j][i]^=prefixanex[j][i-1];
	}
	
}
int main()
{
#ifdef JCVB
	///freopen("jcbig.in","r",stdin);
	//freopen("my.out","w",stdout);
	int _time_jc=clock();
#endif
	//printf("faf\n");
	gn(n);gn(q);
	//printf("faf\n");
	rep(i,1,n)gn(a[i]);
	//printf("faf\n");
	nexteven[3*n] = 3*n;
	per(i,1,3*n) {
		if(a[i]%2==0)nexteven[i]=i;
		else nexteven[i] = nexteven[i+1];
	}

	int last0=-inf;
	lastzero[0]=last0;
	rep(i,1,n+1) {
		lastzero[i] = last0;
		if(a[i]&1)continue;
		if(i-last0>=stepsize) canzero[++canzerotot] = i;
		last0 = i;
	}
	rep(i,1,q+1){
		int la,lb,lc;
		gn(la);gn(lb);gn(lc);
		queries[i]= (query){la,lb,lc,i};
	}
	inittotdis();
	mineveni = nexteven[1];
	sort(queries+1,queries+1+q);


	prepare32();
	int temp = 1;
	for (int i=n+1;i>n+2-mineveni;i--) {
		temp^=1;
		if(temp) _leftdsupd(i);
		//leftds[i] = temp;
	}
	for (int i = n+2-mineveni;i>=1;i--) {
		if(temp) _leftdsupd(i);
		//leftds[i]=temp;
	}

	int curquery=1;
	rep(lcah,1,n+1) {
		if(lcah>=2) {
			leftdsupdate(lcah);
		}
		while(curquery<=q && queries[curquery].lc==lcah) {
			queryans[queries[curquery].id] = to_query(queries[curquery].la,queries[curquery].lb, queries[curquery].lc);
			curquery++;
		}
	}
	rep(i,1,q+1)printf("%d\n",queryans[i]);
#ifdef JCVB
	debug("time: %d\n",int(clock()-_time_jc));
#endif
	return 0;
}