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
#include "bits/stdc++.h" // Tomasz Nowak
using namespace std;     // XIII LO Szczecin
using LL = long long;    // Poland
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
template<class T> int size(T &&x) {
	return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
	return out << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
	out << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		out << *it << (it == prev(x.end()) ? "" : ", ");
	return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
	cerr << x << ";  ";
	dump(args...);
}
#ifdef DEBUG
  struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (strcmp(#x, "") ? #x ":  " : ""), dump(x), Nl(), cerr << ""
#else
# define debug(...) 0 && cerr
#endif
mt19937_64 rng(0);
int rd(int l, int r) {
	return uniform_int_distribution<int>(l, r)(rng);
}
// end of templates

constexpr int mod = int(1e9) + 7;

int add(int a, int b) {
	a += b;
	if(a >= mod)
		a -= mod;
	return a;
}
int mul(int a, int b) {
	return int((a * LL(b)) % mod);
}
int sub(int a, int b) {
	return add(a, mod - b);
}
int pot(int a, int b) {
	if(b == 0)
		return 1;
	int x = pot(a, b / 2);
	x = mul(x, x);
	if(b & 1)
		x = mul(x, a);
	return x;
}
int inv2 = pot(2, mod - 2);

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n, q;
	cin >> n >> q;
	vector<int> a(n);
	REP(i, n - 1)
		cin >> a[i];
	debug(a);

	vector<int> sum_dist_down(n), vertices_down(n, 1);
	for(int d = n - 2; d >= 0; --d) {
		vertices_down[d] = add(mul(vertices_down[d + 1], a[d]), 1);
		sum_dist_down[d] = add(mul(sum_dist_down[d + 1], a[d]), sub(vertices_down[d], 1));
	}
	debug(vertices_down, sum_dist_down);
	int all_vertices = vertices_down[0];
	debug(all_vertices);

	vector<int> sum_dist_up(n);
	for(int d = 1; d < n; ++d) {
		int besides = mul(a[d - 1] - 1, add(sum_dist_down[d], vertices_down[d]));
		int strict_up =  sum_dist_up[d - 1];
		int delta = sub(all_vertices, vertices_down[d]);
		sum_dist_up[d] = add(add(besides, strict_up), delta);
	}
	debug(sum_dist_up);

	vector<int> sum_dist(n);
	REP(d, n)
		sum_dist[d] = add(sum_dist_down[d], sum_dist_up[d]);
	debug(sum_dist);

	vector<pair<int, int>> range_down(n);
	range_down.back() = {0, 0};

	for(int d = n - 2; d >= 0; --d) {
		if(a[d] % 2 == 0)
			range_down[d] = {0, 0};
		else {
			range_down[d] = range_down[d + 1];
			++range_down[d].second;
		}
	}
	debug(range_down);

	while(q --> 0) {
		int wa, wb, wlca;
		cin >> wa >> wb >> wlca;
		--wa, --wb, --wlca;
		debug(wa, wb, wlca);
		
		int dist_ab = wa + wb - 2 * wlca;

		vector<pair<int, int>> intervals;
		auto add_subtree = [&](int dist, int d, int cnt) {
			if(cnt % 2 == 0)
				return;
			auto [l, r] = range_down[d];
			l += dist;
			r += dist;
			debug(dist, d, l, r);
			intervals.emplace_back(l, r);
		};

		for(int wlow : {wa, wb}) {
			debug() << "path " << make_pair(wlca, wlow);
			if(wlow != wlca)
				add_subtree(1, wlow + 1, a[wlow]);
			for(int d = wlow - 1; d > wlca; --d)
				add_subtree(1, d + 1, a[d] - 1);
		}
		int paths_down = int(wa != wlca) + int(wb != wlca);
		debug(paths_down);
		add_subtree(1, wlca + 1, a[wlca] - paths_down);

		debug() << "path up";
		for(int d = wlca - 1; d >= 0; --d) {
			debug(d, wlca);
			add_subtree((wlca - d) + 1, d + 1, a[d] - 1);
		}

		if((dist_ab + 1) % 2 == 1)
			intervals.emplace_back(0, 0);
		if(wlca != 0)
			intervals.emplace_back(1, wlca);
		debug(intervals);

		int mx_r = 2 * n;
		vector<int> prefix(mx_r);
		for(auto [l, r] : intervals) {
			debug(l, r);
			assert(l <= r);
			++prefix[l];
			--prefix[r + 1];
		}
		FOR(i, 1, mx_r - 1)
			prefix[i] = (prefix[i - 1] + prefix[i]) % 2;
		debug(prefix);

		vector<int> values;
		REP(i, mx_r)
			if(prefix[i])
				values.emplace_back(dist_ab + 2 * i);
		debug(values);

		int answer = sub(sum_dist[wa], sum_dist[wb]);
		debug(answer);
		REP(i, size(values)) {
			int delta = values[size(values) - 1 - i],
				sign = (i % 2 == 0 ? 1 : mod - 1);
			answer = add(answer, mul(delta, sign));
		}
		cout << mul(answer, inv2) << '\n';
	}
}