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
#include<bits/stdc++.h>
using namespace std;
 
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define ST first
#define ND second
 
ostream& operator<<(ostream &out, string str) {
	for(char c : str) out << c;
	return out;
}
 
template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) {
	return out << "(" << p.ST << ", " << p.ND << ")";
}

template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
	out << '{';
	for(auto &e : x)
        	out << e << (&e == &*--x.end() ? "" : ", ");
	return out << '}';
}

template<class... Args> void dump(Args&&... args) {
	((cerr << args << ";  "), ...);
}
 
#ifdef DEBUG
#  define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << "\n"
#else
#  define debug(...) false
#endif
 
template<class T> int size(T && a) { return (int) a.size(); }
 
using LL = long long;
using PII = pair<int, int>;

template<int mod>
struct modular {
	int val;
	modular() { val = 0; }
	modular(const LL& v) {
		val = (-mod <= v && v <= mod) ? v : v % mod;
		if(val < 0) val += mod;
	}
	int to_int() { return val; }

	friend ostream& operator<<(ostream &os, const modular &a) {
		return os << a.val;
	}
	friend istream& operator>>(istream &is, modular &a) {
		return is >> a.val;
	}

	friend bool operator==(const modular &a, const modular &b) {
		return a.val == b.val;
	}
	friend bool operator!=(const modular &a, const modular &b) {
		return !(a == b);
	}
	friend bool operator<(const modular &a, const modular &b) {
		return a.val < b.val;
	}
	friend bool operator<=(const modular &a, const modular &b) {
		return a.val <= b.val;
	}

	modular operator-() const { return modular(-val); }
	modular& operator+=(const modular &m) {
		if((val += m.val) >= mod) val -= mod;
		return *this;
	}
	modular& operator-=(const modular &m) {
		if((val -= m.val) < 0) val += mod;
		return *this;
	}
	modular& operator*=(const modular &m) {
		val = (LL) val * m.val % mod;
		return *this;
	}
	friend modular qpow(modular a, LL n) {
		if(n == 0) return 1;
		if(n % 2 == 1) return qpow(a, n - 1) * a;
		return qpow(a * a, n / 2);
	}
	friend modular inv(const modular &a) {
		assert(a != 0); return qpow(a, mod - 2);
	}
	modular& operator /=(const modular &m) { 
		return (*this) *= inv(m); 
	}

	friend modular operator+(modular a, const modular &b) { return a += b; }
	friend modular operator-(modular a, const modular &b) { return a -= b; }
	friend modular operator*(modular a, const modular &b) { return a *= b; }
	friend modular operator/(modular a, const modular &b) { return a /= b; }
};
using mint = modular<int(1e9 + 7)>;
// using mint = modular<int(998244353)>;

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

	int n, q;
	cin >> n >> q;

	vector<int> f(n, 1);
	FOR(i, 1, n - 1)
		cin >> f[i];

	/*
	 * cnt[i] - ilość wierzchołków w poddrzewie wierzchołka warstwy i
	 * sub[i] - suma odległości w poddrzewie wierzchołka warstwy i
	 */
	vector<mint> cnt(n, 1), sub(n);
	for(int i = n - 2; i >= 0; i--)
		cnt[i] = cnt[i + 1] * f[i + 1] + 1;
	for(int i = n - 2; i >= 0; i--)
		sub[i] = (sub[i + 1] + cnt[i + 1]) * f[i + 1];
	
	vector<mint> dst(n);
	dst[0] = 0;
	FOR(i, 1, n - 1)
		dst[i] = dst[i - 1] + (f[i] - 1) * (cnt[i] + sub[i]) + (cnt[0] - cnt[i]);
	REP(i, n) 
		dst[i] += sub[i];

	debug(cnt);
	debug(sub);
	debug(dst);

	vector<int> odd(n, 1);
	for(int i = n - 2; i >= 0; i--) {
		if(f[i + 1] % 2 == 0)
			odd[i] = 1;
		else
			odd[i] = odd[i + 1] + 1;
	}
	debug(odd);

	REP(qq, q) {
		int a, b, lca;
		cin >> a >> b >> lca;
		a--, b--, lca--;

		mint ans = dst[a] - dst[b];

		vector<int> freq(2 * n + 1);
		auto add = [&](int l, int r) {
			freq[l]++, freq[r + 1]--;
		};

		auto side = [&](int dep) {
			add(0, 0);
			if(f[dep + 1] % 2 == 0) 
				add(1, odd[dep + 1]);
		};
		auto under = [&](int dep) {
			if(dep != lca)
				add(0, odd[dep] - 1);
		};
		auto mid = [&]() {
			add(0, 0);
			if(lca + 1 == n) return;
			int mul = f[lca + 1] - (a == lca) - (b == lca);
			if(mul % 2 == 1)
				add(1, odd[lca + 1]);
		};
		auto up = [&](int dep) {
			int offset = lca - dep;
			add(offset, offset);
			if(f[dep + 1] % 2 == 0)
				add(offset + 1, offset + odd[dep + 1]);
		};


		debug(a, b, lca);

		under(a);
		under(b);
		mid();
		FOR(i, lca + 1, a - 1)
			side(i);				
		FOR(i, lca + 1, b - 1)
			side(i);
		FOR(i, 0, lca - 1)
			up(i);

		REP(i, 2 * n)
			freq[i + 1] += freq[i];

		int base = a + b - 2 * lca, sign = 1;
		for(int i = 2 * n; i >= 0; i--) {
			if(freq[i] % 2) {
				debug(sign, base + 2 * i);
				ans += sign * (base + 2 * i);
				sign *= -1;
			}
		}

		debug(freq);

		cout << ans / 2 << "\n";
	}
}