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
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9+7;
const int N = 3e5+5, BLOCK = 400;
int n, a[N], q, Wa[N], Wb[N], Wl[N], subt[N], Sa, Sb;

struct Rootfix {
	vector<int> suf, suf_on, score[2];
	int start, last;
	Rootfix() {
		start = n;
		last = 2*(n+2)-1;
		suf.resize(2*(n+2), 0);
		suf_on.resize(2*(n+2), 0);
		score[0].resize(2*(n+2), 0);
		score[1].resize(2*(n+2), 0);
		score[1][last] = last;
		for(int i=last-1; i>=0; --i) {
			score[1][i] = ( score[1][i+1] + (((last - i)%2==0)?i:-i) +mod)%mod;
		}
	}
	void upd(int i) {
		suf_on[i] = (suf_on[i+1]^suf[i]);
		if(suf[i] && suf_on[i]) {
			score[0][i] = (score[0][i+1] + i)%mod;
		}
		else if(suf[i]) {
			score[0][i] = (score[0][i+1] - i + mod)%mod;
		}
		else {
			score[0][i] = score[0][i+1];
		}
		if(!suf[i] && (((last-i+1)%2) ^ suf_on[i])) {
			score[1][i] = (score[1][i+1] + i)%mod;
		}
		else if(!suf[i]) {
			score[1][i] = (score[1][i+1] - i + mod)%mod;
		}
		else {
			score[1][i] = score[1][i+1];
		}
	}
	void shift() {
		start--;
		suf[start] = suf[start+1];
		upd(start);
	}
	void insert(int val) {
		for(int i=start+val; i>=start; --i) {
			suf[i] = !suf[i];	
			upd(i);
		}
	}
	int all() { return last - start - 1; }
	bool is_on(int x) { return suf[start+x]; }
	bool count_on(int pref, bool xored=0) {
		bool cnt_on = (suf_on[start] ^ suf_on[start+pref+1]);
		if(xored) {
			cnt_on = (((pref+1)%2) ^ cnt_on);
		}
		return cnt_on;
	}
	int get_score(int pref, bool xored=0, bool added=0) {
		int sum = (score[xored][start] - score[xored][start+pref+1] + mod)%mod;
		bool u = (last-(start+pref))%2;
		bool should_start = (added ^ suf_on[start+pref+1]);
		bool starts;
		if(!xored) {
			starts = suf_on[start+pref+1];
		}
		else {
			starts = (suf_on[start+pref+1]^u);
		}
		if(should_start != starts) {
			sum = (-sum+mod)%mod;
		}
		bool cnt_on = count_on(pref, xored);
		if(cnt_on) {
			if(should_start==0) sum = (sum-start+mod)%mod;
			else sum = (sum+start)%mod;	
		}
		return sum;
	}
	int brute() {
		int res = 0, turn = 1;
		for(int i=last; i>=start; --i) {
			if(suf[i]) {
				res += turn * (i-start);
				res %= mod;
				turn *= -1;
			}
		}
		return res;
	}
};
struct Subtrees {
	vector<int> vec;
	void insert(int len) { /// O(N) zamortyzowane
		vector<int> taken;
		while(vec.size() && vec.back() < len) {
			taken.push_back(vec.back());
			vec.pop_back();
		}
		bool skip=0;
		while((int)vec.size() && vec[vec.size()-1]==len) {
			skip=1;
			vec.pop_back();
		}
		if(!skip) vec.push_back(len);
		while(taken.size()) {
			vec.push_back(taken.back());
			taken.pop_back();
		}
	}
	vector<int> get_all() { /// O(SIZE)
		vector<int> res = vec;
		reverse(res.begin(), res.end());
		return res;
	}
};
vector<int> get_sum(vector<int> v1, vector<int> v2) {
	vector<int> nw;
	int i=0, j=0;
	while(i<(int)v1.size() || j<(int)v2.size()) {
		if(j==(int)v2.size()) {
			nw.push_back(v1[i]);
			i++; continue;
		}
		if(i==(int)v1.size()) {
			nw.push_back(v2[j]);
			j++; continue;
		}
		if(v1[i]==v2[j]) {
			i++; j++; continue;
		}
		if(v1[i] > v2[j]) {
			nw.push_back(v1[i]);
			i++;
		}
		else {
			nw.push_back(v2[j]);
			j++;
		}
	}
	return nw;
}
Subtrees mem[N/BLOCK + 5];
Subtrees get_prefix(int x) { /// O(SQRT) -> wśród BLOCK kolejnych wartości, suma z wyłączeniem jednego elementu nie będzie przekraczała O(SQRT)
	Subtrees res = mem[x/BLOCK];
	for(int i=x/BLOCK*BLOCK+1; i<=x; ++i) {
		if(a[i]%2==0) {
			res.insert(subt[i+1]+1);
		}
	}
	return res;
}

ll all_distances[N], nodes_in_subt[N], depths_sum[N];
void calculate_distances() {
	nodes_in_subt[n] = 1;
	depths_sum[n] = n;
	for(int i=n-1; i>=1; --i) {
		nodes_in_subt[i] = (nodes_in_subt[i+1]*a[i] + 1)%mod;
		depths_sum[i] = (depths_sum[i+1]*a[i] + i)%mod;
	}

	// all_distances tylko dla wierzchołków nad
	for(int i=2; i<=n; ++i) {
		all_distances[i] = ( all_distances[i-1] + (nodes_in_subt[1] - nodes_in_subt[i] + mod) + (depths_sum[i]-(i-1)*nodes_in_subt[i]%mod+mod)*(a[i-1]-1) )%mod; 	
	}
	// napraw all_distances
	for(int i=1; i<=n; ++i) {
		all_distances[i] = (all_distances[i] + (depths_sum[i] - i*nodes_in_subt[i]%mod +mod) )%mod;
	}
}
int32_t main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin>>n>>q;
	for(int i=1; i<n; ++i) {
		cin>>a[i];
	}
	
	calculate_distances();

	subt[n] = 0;
	for(int i=n-1; i>=1; --i) {
		if(a[i]%2==0) {
			subt[i] = 0;
		}
		else {
			subt[i] = subt[i+1]+1;
		}
	}

	vector<vector<int>> queries(n+1);	
	for(int i=0; i<q; ++i) {
		cin>>Wa[i]>>Wb[i]>>Wl[i];
		queries[Wl[i]].push_back(i);	
	}

	Subtrees current;	
	for(int i=1; i<=n; ++i) {
		if(a[i]%2==0) { 
			current.insert(subt[i+1]+1);
		}
		if(i%BLOCK==0) {
			mem[i/BLOCK] = current;
		}
	}

	vector<ll> answer(q);
	Rootfix cur;
	for(int i=1; i<=n; ++i) {
		for(int j: queries[i]) {
			// Wa[j], Wb[j]] - odpowiedz	
			int wa = Wa[j], wb = Wb[j];
			if(wa > wb) swap(wa, wb);
			Subtrees S;
			S.vec = get_sum(S.vec, get_prefix(wa-1).vec);
			S.vec = get_sum(S.vec, get_prefix(wb-1).vec);
			S.insert(subt[wb]);
			if(Wl[j]!=wa) {
				S.insert(subt[wa]);
				S.insert(subt[Wl[j]]);
			}
			// zmergeuj S i cur
			vector<int> changes = S.get_all(); /// O(SIZE * LOG(SIZE)) 
			ll ans = (all_distances[Wa[j]] - all_distances[Wb[j]] + ((nodes_in_subt[1]%2==1)?Wa[j]+Wb[j]-2*Wl[j]:0) + mod)%mod;
			ans = (ans + 2*cur.get_score(cur.all())) % mod;
			int added=0, calced=0;
			for(int k=(int)changes.size()-1; k>=0; --k) {
				if(k+1<(int)changes.size()) {
					added = (added + calced*(changes[k+1]-changes[k]))%2;
				}
				bool xored = (((int)changes.size()-k)%2);	
				ans = (ans + 2*cur.get_score(changes[k], xored, added) - 2*cur.get_score(changes[k], !xored, added) + 2*mod) % mod;	
				calced++;
				calced%=2;
			}
			//ans = (ans + 2*cur.get_score(cur.all())) % mod;
			answer[j] = ans;
		}
		if(a[i]%2==0) {
			cur.insert(subt[i+1]+1);
			if(i!=1) cur.insert(0);
		}
		else if(i==1){
			cur.insert(0);
		}
		cur.shift();
	}
	for(int i=0; i<q; ++i) {
		cout<<answer[i] * ((mod+1)/2) % mod<<'\n';
	}
}