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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include <bits/stdc++.h>
using namespace std;
#define PII pair<int, int>
#define VI vector<int>
#define VPII vector<PII>
#define LL long long
#define LD long double
#define f first
#define s second
#define MP make_pair
#define PB push_back
#define endl '\n'      // UWAGA
#define ALL(c) (c).begin(), (c).end()
#define SIZ(c) (int)(c).size()
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i)
#define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i)
#define random_shuffle(V) shuffle(V, rng)

#define sim template<class n
sim, class s> ostream & operator << (ostream &p, pair<n, s> x)
{return p << "<" << x.f << ", " << x.s << ">";}
sim> auto operator << (ostream &p, n y) ->
typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type 
{int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";}
void dor() {cerr << endl;}
sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);}
sim, class s> void mini(n &p, s y) {if(p>y) p = y;}
sim, class s> void maxi(n &p, s y) {if(p<y) p = y;}
#ifdef DEB
#define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__)
#else
#define debug(...)
#endif 

#define I(x) #x " =", (x), " "
#define A(a, i) #a "[" #i " =", i, "] =", a[i], " "

int mod = 2000 * 1000 * 1000 + 14;
int finish_mod = 1000 * 1000 * 1000 + 7;

const int MXN = 300 * 1000 + 100; // maksymalna liczba wierzchołków
int in[MXN];
int next2[MXN];
int n, q;

LL cnt[MXN];// ile wierzchołków pod tym poziomem razem z tym poziomem
LL dp[MXN]; // dp[i] = 1 * 0 + in[i] * 1 + in[i] * in[i+1] * 2, ...
LL pom1[MXN]; // pomocnicze tablice do obsłużenia sum_odl(lvl) w stałym
LL pom2[MXN];
LL sum_pom1[MXN];
LL sum_pom2[MXN];

void pre_sum_odl()
  {
  cnt[n] = 1;
  dp[n] = 0;
  FORD(i, n-1, 1)
    {
    cnt[i] = (1 + cnt[i+1] * in[i]) % mod;
    dp[i] = (dp[i+1] * in[i] + cnt[i] - 1) % mod;
    }

  FOR(i, 1, n-1)
    {
    LL tutaj = cnt[i] - cnt[i+1];
    if(tutaj < 0)tutaj += mod;
    if(tutaj >= mod)tutaj -= mod;
    pom1[i] = (dp[i] - (dp[i+1] + cnt[i+1]) - tutaj * i) % mod;
    if(pom1[i] < 0)pom1[i] += mod;
    pom2[i] = tutaj;

    sum_pom1[i] = sum_pom1[i-1] + pom1[i];
    if(sum_pom1[i] >= mod)sum_pom1[i] -= mod;
    sum_pom2[i] = sum_pom2[i-1] + pom2[i];
    if(sum_pom2[i] >= mod)sum_pom2[i] -= mod;
    }
  }

LL sum_odl(int lvl)
  {
  LL res = dp[lvl] + sum_pom1[lvl-1] + sum_pom2[lvl-1] * lvl;
  res %= mod;
  if(res < 0)res += mod;
  return res;
  }

int get_dep(int lvl) // zwraca liczbę nieparzysytch poziomów w dół
  {
  return next2[lvl] - lvl;
  }

VI inserts_on_path[MXN];
bool needed[MXN];
bool act[MXN];
void preprocess_on_path()
  {
  VI S;
  set<int> SS;
  auto modif0 = [&](int a)
    {
    SS.insert(a);
    };
  FOR(i, 1, n-1)
    {
    if(in[i] % 2 == 0)modif0(get_dep(i+1)+1);
    else modif0(0);
    }
  for(auto i: SS)S.PB(i);

  auto modif = [&](int a)
    {
    act[a] ^= 1;
    };
  FOR(i, 1, n-1)
    {
    if(in[i] % 2 == 0)modif(get_dep(i+1)+1);
    else modif(0);
    if(needed[i])
      {
      int siz = 0; for(auto j: S)if(act[j])siz++;
      inserts_on_path[i].reserve(siz);
      for(auto j: S)if(act[j])inserts_on_path[i].PB(j);
      }
    }
  }
void set_difference(VI &A, VI &B, VI &res)
  {
  res.clear();
  int i = 0;
  int j = 0;
  while(i < A.size() && j < B.size())
    {
    if(A[i] < B[j])res.PB(A[i++]);
    else if(A[i] > B[j])res.PB(B[j++]);
    else 
      {
      i++; j++;
      }
    }
  while(i < A.size()){res.PB(A[i]); i++;}
  while(j < B.size()){res.PB(B[j]); j++;}
  }

bool pref[MXN*2];
bool ones[MXN*2];
int res[2][MXN*2];
int actt[MXN*2];
int mx = 0;
void process_pref(VI &waiting, int lca)
  {
  for(auto i: waiting)pref[i] ^= 1;

  int LIM = 2 * n + 3;
  LIM = mx + 2;
  actt[LIM + 1] = 0;
  FORD(i, LIM, 1)
    {
    actt[i] = actt[i+1] ^ pref[i];
    }

  res[0][0] = res[1][0] = ones[0] = 0;
  FOR(i, 1, LIM)
    {
    ones[i] = ones[i-1];

    res[actt[i]][i] = i - res[actt[i]][i-1];
    res[actt[i]^1][i] = res[actt[i]^1][i-1];
    
    if(actt[i])ones[i] ^= 1;
    }
  }

inline int get_moves(int x, int a, int b)
  {
  if(x == 1)return (ones[b] ^ ones[a-1]);
  else return (b&1) ^ (a&1) ^ 1 ^ get_moves(x ^ 1, a, b);
  }
inline int get_seg(int x, int a, int b)
  {
  if(get_moves(x, a, b))
    return res[x][b] + res[x][a-1];
  else 
    return res[x][b] - res[x][a-1];
  }

void insert_sorted(VI &V, int val) // utrzymuje posortowane, jeśli są dwa takie same to wywala
  {
  int i = 0; while(i < V.size() && V[i] < val)i++;
  if(i == V.size()){V.PB(val);return;}
  if(V[i] == val)
    {
    V.erase(V.begin() + i);
    return;
    }
  V.insert(V.begin() + i, val);
  }

LL out[MXN];
VI diff, p1, p2, p3;
void result(int a, int b, int lca, int idx, VI &waiting)
  {
  int offset = n - lca + 1;

  diff.clear();
  auto add2 = [&](int a)
    {
    if(a >= offset)insert_sorted(diff, a);
    };

  if(a != lca)
    {
    add2(get_dep(a) + offset);
    }
  if(b != lca)
    {
    add2(get_dep(b) + offset);
    }
  if(lca != b && lca != a)
    {
    add2(get_dep(lca) + offset);
    }
  else
    {
    if(a != b)
      {
      add2(get_dep(lca) + offset);
      add2(get_dep(lca+1) + offset + 1);
      add2(offset);
      }
    else 
      add2(get_dep(lca) + offset);
    }
  if(b != lca)
    {
    set_difference(inserts_on_path[b-1], inserts_on_path[max(lca+1, a)-1], p1); // for path a-b
    for(auto &i: p1)i += offset;  
    set_difference(diff, p1, p2);
    }
  else swap(p2, diff);
  set_difference(p2, waiting, diff); // for path from lca to root
  
    int sum_all2 = 0;
    int move = 0;
    auto process = [&](int a, int b, int state)
      {
      if(move)
        {
        sum_all2 -= get_seg(state, a, b);
        move ^= get_moves(state, a, b);
        }
      else
        {
        sum_all2 += get_seg(state, a, b);
        move ^= get_moves(state, a, b);
        }
      };

  int prev_idx = mx;
  int prev_have = 1;
  FORD(j, (int)diff.size()-1, 0)
    {
    int i = diff[j];
    process(i+1, prev_idx, prev_have);

    prev_idx = i;
    prev_have ^= 1;
    }
  process(offset, prev_idx, prev_have);

  int odlab = b + a - lca * 2;
  if(move)  // move to parzystość wykonanych ruchów
    {
    out[idx] += odlab;
    sum_all2 -= offset;
    }
  out[idx] += sum_all2 * 2;
  out[idx] = out[idx] % mod;
  if(out[idx] < 0)out[idx] += mod;
  }
VI lazy[MXN];
int a[MXN];
int b[MXN];
int lca[MXN];
// #define WORST_CASE
void do_work()
  {
  VI waiting;
  process_pref(waiting, 0);

  auto add2 = [&](PII x)
    {
    if(x.f)insert_sorted(waiting, x.f-1);
    insert_sorted(waiting, x.s);
    };
  auto move = [&](int lca)
    {
    process_pref(waiting, lca);
    waiting.clear();
    };

  FOR(i, 1, n)
    {
                  // #ifdef WORST_CASE
                  //  waiting.PB(n+i);
                  //  waiting.PB(n+i);
                  // #endif
    if(waiting.size() > 651)
        {
        move(i);
        }
    for(auto j: lazy[i])
      {
      result(a[j], b[j], lca[j], j, waiting);
      }
    if(i == n)break;
    if(in[i] % 2 == 0)
      {
      add2(MP(n-i+1, get_dep(i+1)+1 + n - i+1));
      }
    else add2(MP(n-i+1, n-i+1));
    }
  }


int32_t main()
  {
  scanf("%d%d", &n, &q);
                  // #ifdef WORST_CASE
                  //   n = q = 300000;
                  //   int have = 1;
                  //   int sum = 1;
                  // #endif
  FOR(i, 1, n-1)
    {
    scanf("%d", &in[i]);

                  // #ifdef WORST_CASE
                  //     in[i] = rand() % 100000 + 2;
                      
                  //     if(i == sum)
                  //       {
                  //       in[i] -= in[i] & 1; // parzyste
                  //       have++;
                  //       sum += have+1;
                  //       }
                  //     else
                  //       {
                  //       in[i] |= 1;
                  //       }
                  // #endif
    }
  next2[n] = n;
  FORD(i, n-1, 1)
    {
    if(in[i] % 2 == 0)next2[i] = i;
    else next2[i] = next2[i+1];
    }
  FOR(i, 1, n-1)
    maxi(mx, n + get_dep(i)+4);

  pre_sum_odl();
  REP(i, q)
    {
    scanf("%d%d%d", &a[i], &b[i], &lca[i]);

                  // #ifdef WORST_CASE
                  //   a[i] = rand() % n + 1;
                  //   b[i] = rand() % n + 1;
                  //   lca[i] = rand() % min(a[i], b[i]) + 1;
                  //   lca[i] = min(a[i], min(b[i], 375));
                  // #endif

    out[i] = (sum_odl(a[i]) - sum_odl(b[i]) + mod) % mod;
    if(a[i] > b[i])swap(a[i], b[i]);

    needed[b[i]-1] = 1;
    needed[max(lca[i]+1, a[i])-1] = 1;

    lazy[lca[i]].PB(i);
    }
  preprocess_on_path();

  do_work();
  REP(i, q)
    {
    printf("%lld\n", (out[i] / 2 + finish_mod) % finish_mod);
    }
  }