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
//Konrad Paluszek,University of Warsaw(former XIV LO Staszic)
//#STAY AT HOME
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#define TIME (chrono::steady_clock::now().time_since_epoch().count())
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define xfm(a,b)a##b
#define xwb(a,b)xfm(a,b)
#define _ xwb(nvj,__LINE__)
#define __ xwb(kjy,__LINE__)
#define ___ xwb(cjm,__LINE__)
#define REP(i,n)for(urs(n)i=0;i<(n);++i)
#define PER(r...)for(bool _=1;_||next_permutation(r);_=false)
#define FS(r)r.first,r.second
#define ALL(r)(r).begin(),(r).end()
#define M0(r) memset(r,0,sizeof(r))
#define sim template<class c
#define forbits(i,m)if(m)for(urs(m)i=ctz(m),i##nvj=m;i##nvj;i##nvj^=((urs(m))1<<i),i=i##nvj?ctz(i##nvj):0)
#define fordbits(i,m)if(m)for(urs(m)i=8*sizeof(m)-clz(m)-1,i##nxd=m;i##nxd;i##nxd^=((urs(m))1<<i),i=i##nxd?8*sizeof(m)-clz(i##nxd)-1:0)
#define ksets(t, m, k, n)for(t m=(((t)1<<(k))-1);m<((t)1<<(n));m=nux(m))
#define urs(r...)typename decay<decltype(r)>::type
#define hur(f,g)sim>int f(c a){if(sizeof(c)==8)return g##ll(a);return g(a);}
using namespace __gnu_pbds;using namespace std;using ll=long long;using ld=long double;using ull=unsigned long long;using vi=vector<int>;using vll=vector<ll>;using pii=pair<int,int>;using pll=pair<ll,ll>;using vpii=vector<pii>;using unt=unsigned int;sim>using min_queue=priority_queue<c,vector<c>,greater<c>>;sim,class b,class cmp=less<c>>using ordered_map=tree<c,b,cmp,rb_tree_tag,tree_order_statistics_node_update>;sim,class cmp=less<c>>using ordered_set=ordered_map<c,null_type,cmp>;hur(popc,__builtin_popcount)hur(ctz,__builtin_ctz)hur(clz,__builtin_clz)sim,class N>bool mini(c&o,const N&h){if(o>h)return o=h,1;return 0;}sim,class N>bool maxi(c&o,const N&h){if(o<h)return o=h,1;return 0;}
#ifdef LOCAL
#include </home/kjp/headers/debuglib.hpp>
#else
#define loc(...)
#define onl(r...)r
#define debug(...)
#define print_stack(...)
#define mark_stack(...)
#define set_pre(...)
#define reg_it(...)
#define def_op(...) struct _{};
#define mask_set(...)
#define exit my_exit
void my_exit(int x) {fflush(stdout);_Exit(x);}
#endif
#define next nexT
#define prev preV
#define tree trEE
#define left lefT
#define right righT
#define div diV
#define y1 y_1
#define pow don't'
ull mix(ull o){o+=0x9e3779b97f4a7c15;o=(o^(o>>30))*0xbf58476d1ce4e5b9;o=(o^(o>>27))*0x94d049bb133111eb;return o^(o>>31);}ull SALT=0x7a14a4b0881ebf9,tqu=0x7a14a4b0881ebf9;ull my_rand(){return tqu=mix(tqu);}void my_srand(ull x){SALT=tqu=x;}const int inf=1023400000;const ll llinf=1234567890000000000ll;ll fix(ll o, ll m){o%=m;if(o<0)o+=m;return o;}
#define rand my_rand
#define srand my_srand
#define random_shuffle(r...)random_shuffle(r,[](int _){return my_rand()%_;})
sim>inline c nux(c m){if(!m)return numeric_limits<c>::max();c A=m&-m;c B=~((A-1)^m);c C=B&-B;c D=(C>>(1+ctz(A)))-1;return C|(m&~(C-1))|D;}sim>void unq(c&x){x.resize(unique(ALL(x))-x.begin());}
//#STAY AT HOME
const int nax = 1 << 19, mod = 1e9 + 7, half = (mod + 1) / 2;
int a[nax];
int n, q;
ll dist_sum[nax];
ll subtree_size[nax];
ll layer_size[nax];
struct sub_tree {
	int sub[nax * 2], flipped[2 * nax];
	void flip(int l, int r, int wh = 1, int lb = 0, int rb = nax) {
		if (wh == 1) {
			// debug(this, imie(l), imie(r));
		}
		if (l >= rb || r <= lb) return;
		if (l <= lb && r >= rb) {
			flipped[wh] ^= 1;
			return;
		}
		int sb = (lb + rb) / 2;
		flip(l, r, wh * 2, lb, sb);
		flip(l, r, wh * 2 + 1, sb, rb);
		sub[wh] = read(wh * 2, lb, sb) + read(wh * 2 + 1, sb, rb);
	}
	int read(int node = 1, int l = 0, int r = nax) {
		return flipped[node] ? (r - l) - sub[node] : sub[node];
	}
	sub_tree() {
		memset(this, 0, sizeof(*this));
	}
};
struct full_tree {
	sub_tree by_par[2];
	void flip(int l, int r) {
		if ((l ^ r) & 1) {
			flip(l);
			flip(r);
		}
		else by_par[l & 1].flip((l + 1) / 2, (r + 1) / 2);
	}
	void flip(int x) { 
		by_par[x & 1].flip(0, (x + 1) / 2);
	}
	int read() {
		return by_par[0].read() + by_par[1].read();
	}
};
full_tree f;
int cou[nax * 2];
vi todo;
void touch_end(int l) {
	// assert(l >= 0);
	if (cou[l] == 0) todo.push_back(l);
	cou[l]++;
}
void schedule_flip(int a, int b) {
	touch_end(a);
	touch_end(b);
}
void commit() {
	static vi real;
	for (int x : todo) {
		if (cou[x] & 1) real.push_back(x);
		cou[x] = 0;
	}
	sort(real.begin(), real.end());
	for (int i = 0; i < (int) real.size(); i += 2) {
		f.flip(real[i], real[i + 1]);
	}
	todo.clear();
	real.clear();
}
ll ans[nax];
vector <tuple <int, int, int> > queries[nax];
int next_even[nax];
vi odds[nax];
int where[nax];
int main() {
	scanf("%d%d", &n, &q);
	REP(i, n - 1) scanf("%d", a + i);
	next_even[n] = n;
	for (int i = n - 1; i >= 0; --i)
		if (a[i] % 2) next_even[i] = next_even[i + 1];
		else next_even[i] = i;
	REP(i, n - 1) {
		if (a[i] % 2 == 0) {
			int jump = next_even[i + 1] - i;
			debug(arr(a, i), imie(jump), arr(a, imie(i) + imie(jump)));
			where[i] = i;
			odds[i] = odds[where[max(0, i - 1)]];
			auto it = lower_bound(ALL(odds[i]), jump);
			if (it != odds[i].end() && *it == jump) odds[i].erase(it);
			else odds[i].insert(it, jump);
		}
		else {
			where[i] = where[max(0, i - 1)];
		}
	}
	bool is_total_odd = true, is_layer_odd = true;
	REP(i, n - 1) {
		if (a[i] % 2 == 0) is_layer_odd = false;
		is_total_odd ^= is_layer_odd;
	}
	debug(imie(is_total_odd));
	// cerr << "is_total_odd = " << boolalpha << is_total_odd << endl;
	layer_size[0] = 1;
	REP(i, n - 1) layer_size[i + 1] = layer_size[i] * a[i] % mod;
	subtree_size[n - 1] = 1;
	for (int i = n - 2; i >= 0; --i) subtree_size[i] = (1 + subtree_size[i + 1] * a[i]) % mod;
	ll depth_sum = 0;
	REP(i, n) (depth_sum += i * layer_size[i]) %= mod;
	dist_sum[0] = depth_sum;
	REP(i, n)
		dist_sum[i + 1] = (dist_sum[i] - 2 * subtree_size[i + 1] + subtree_size[0]) % mod;
	debug(range(dist_sum, 0, n));
	debug(range(subtree_size, 0, n));
	debug(range(layer_size, 0, n));
	auto touch = [&](int x) {
		schedule_flip(x, x + 1);
		debug("touch", imie(x));
		// assert(x <= 2 * n && x >= 0);
	};
	auto go_from = [&](int t, int off) {
		debug("go from", imie(t), imie(off));
		int last = next_even[t + 1];
		debug(imie(t), imie(last));
		schedule_flip(off, (last - t) + off + 1);
	};
	REP(query_id, q) {
		int da, db, dl;
		scanf("%d%d%d", &da, &db, &dl);
		da--;
		db--;
		dl--;
		int closest = (da + db - 2 * dl);
		debug(imie(da), imie(db), imie(dl), imie(closest));
		ll loc_ans = dist_sum[da] - dist_sum[db];
		if (is_total_odd) loc_ans += closest;
		(loc_ans %= mod) *= half;
		loc_ans = fix(loc_ans, mod);
		ans[query_id] = loc_ans;
		queries[dl].emplace_back(da, db, query_id);
	}
	REP(dl, n) {
		if (is_total_odd) touch(n - dl);
		for (auto [da, db, query_id] : queries[dl]) {
			auto do_curr = [&](){
				if ((a[dl] + (da != dl) + (db != dl)) % 2)
					go_from(dl, n - dl);
				else
					touch(n - dl);
				auto sub_bot = [&](int d) {
					if (d > dl) {
						if (a[d] % 2) go_from(d, n - dl);
						else touch(n - dl);
					}
				};
				sub_bot(da);
				sub_bot(db);
				int go1 = max(dl, da - 1), go2 = max(dl, db - 1);
				#if 0 //a little faster
					if (go1 != go2) {
						sub_whole_path_sqrt(go1);
						sub_whole_path_sqrt(go2);
					}
				#else
					int wh1 = where[go1], wh2 = where[go2];
					auto &v1 = odds[wh1], &v2 = odds[wh2];
					if (wh1 != wh2) {
						auto touch_seg = [&](int len) {
							schedule_flip(n - dl, n - dl + len + 1);
						};
						int i1 = 0, i2 = 0;
						while (i1 < (int) v1.size() && i2 < (int) v2.size()) {
							if (v1[i1] == v2[i2]) {
								i1++;
								i2++;
							}
							else if (v1[i1] < v2[i2])
								touch_seg(v1[i1++]);
							else
								touch_seg(v2[i2++]);
						}
						while (i1 < (int) v1.size()) touch_seg(v1[i1++]);
						while (i2 < (int) v2.size()) touch_seg(v2[i2++]);
					}
					if ((v1.size() ^ v2.size() ^ go1 ^ go2) & 1)
						touch(n - dl);
				#endif
			};
			do_curr();
			commit();
			int inc = f.read();
			debug(imie(query_id), imie(da), imie(db), imie(dl), imie(inc));
			ans[query_id] += inc;
			do_curr();
		}
		if (a[dl] % 2 == 0) go_from(dl, n - dl);
		if (is_total_odd ^ (a[dl] % 2)) touch(n - dl);
	}
	REP(i, q) printf("%lld\n", fix(ans[i], mod));
}
//#STAY AT HOME