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
#pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#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 PPC(x) __builtin_popcount(x)
#define MSB(x) (63 - __builtin_clzll(x))
#define SZ(x) ((int)(x).size())
#define HASK(S, x) (S.find(x) != S.end())
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define ithBit(m, i) ((m) >> (i) & 1)
#define ft first
#define sd second
#define kw(a) ((a) * (a))
#ifdef DEBUG
//#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace std; 

const int maxN = 1 << 19, mod = 1000000007, half = (mod+1) / 2;	

template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = max(a, (T1)b);	}

template <typename T1, typename T2> inline void addMod(T1& a, T2 b) { a = (a + b) % mod; }
template <typename T1, typename T2> inline void multMod(T1& a, T2 b) { a = a * b % mod; }
template <typename T1, typename T2> inline void subMod(T1& a, T2 b) { a = (a - b%mod + mod) % mod; }

long long A[maxN], sz[maxN], din[maxN], dout[maxN], tnx[maxN];
int n;

void prelim()
{
	FORD(v, n, 0)
	{
		sz[v] = (sz[v+1] * A[v] + 1) % mod;
		din[v] = din[v+1] + sz[v+1];
		multMod(din[v], A[v]);
	}
	FOR(v, 1, n+1)
	{
		dout[v] = dout[v-1];					// od góry do ojca
		addMod(dout[v], din[v-1]);				// plus od dołu
		subMod(dout[v], din[v] + sz[v]);		// odjąć mnie
		addMod(dout[v], sz[1] - sz[v] + mod);	// krok od ojca do mnie
	}
	tnx[n] = 1;
	FORD(i, n-1, 0)
		tnx[i] = A[i+1] & 1ll ? tnx[i+1] + 1 : 1;
	FOR(i, 1, n)
		A[i] &= 1ll;
}

long long sumdist(int v)
{	return (din[v] + dout[v]) % mod;	}

int T[maxN * 2], mx;

void add(int a, int b)
{	T[a] ^= 1, T[b+1] ^= 1, remax(mx, b+1);	}

void extract(vector <int>& od)
{
	int pres = 0;
	FOR(i, 0, mx+1)
	{
		pres ^= T[i];
		if (pres)
			od.pb(i);
		T[i] = 0;
	}
	mx = 0;
}

long long alterSum(vector <int>& odists)
{
	int s = SZ(odists);
	long long ret = 0;
	FOR(i, 0, s)
		ret += (s&1) == (i&1) ? -odists[i] : odists[i];
	return ret;
}

long long query(int a, int b, int c)
{
	FOR(i, min(a, b), max(a, b))
		if (A[i] == 0)
			add(1, tnx[i]);
	for (int i : {a, b, c})
		if (A[i] == 1)
			add(1, tnx[i]);
	add(1, c-1);
	FOR(i, 1, c)
		if (A[i] == 0)
			add(c-i + 1, c-i + tnx[i]);
			
	int main = a-c + b-c + 1;
	if (main % 2 == 1)
		add(0, 0);
			
	vector <int> odists;
	extract(odists);
	
	long long dx = alterSum(odists) * 2 % mod;
	if (SZ(odists) & 1)	addMod(dx, main-1);
		
	long long sum = (sumdist(a) + sumdist(b)) % mod;
	subMod(sum, dx);
	multMod(sum, half);
	
	long long res = sumdist(a);
	subMod(res, sum);
	return res;
}	

void solve()
{
	int q;
	scanf("%d%d", &n, &q);
	FOR(i, 1, n)
		scanf ("%lld", A+i);
	prelim();
	while (q--)
	{
		int a, b, c;
		scanf ("%d%d%d", &a, &b, &c);
		printf("%lld\n", query(a, b, c));
	}
}
 
int main()
{
	int t = 1;
	//scanf ("%d", &t);
	FOR(tid, 1, t+1)
	{
		//printf("Case #%d: ", tid);
		solve();
	}
	return 0;
}