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
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
//#define TEST
//#define int long long
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define __builtin_ctz __builtin_ctzll
#define __builtin_clz __builtin_clzll
#define __builtin_popcount __builtin_popcountll
using namespace std;
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
  while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
#define next ____next
#define prev ____prev
#define left ____left
#define hash ____hash
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;

template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third;
  bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
  vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
  for (T& v : vec) { ResizeVec(v, sz); }
}
typedef Triple<int, int, int> TIII;
template<class A, class B, class C>
ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }

typedef vector<int32_t> V32;
const int N = 4e5 + 5;
V32 Merge(V32& a, V32& b) {
  V32 res;
  int i = 0, j = 0;
  while (i < SZ(a) || j < SZ(b)) {
    if (i == SZ(a)) {
      res.PB(b[j]);
      j++;
    } else if (j == SZ(b)) {
      res.PB(a[i]);
      i++;
    } else if (a[i] == b[j]) {
      i++;
      j++;
    } else if (a[i] < b[j]) {
      res.PB(a[i]);
      i++;
    } else {
      res.PB(b[j]);
      j++;
    }
  }
  return res;
}
int64_t a[N];

const int P = 1e9 + 7;
struct Query {
  int wa, wb, ind;
//   bool operator<(const Query& oth) {
//     return wl < oth.wl;
//   }
};
int64_t sum_dis[N];
int64_t suf_prod[N];
int64_t suf_sum[N];
int64_t pref_prod[N];
int64_t in_layer[N];
int how_many_odd_from_now[N];
void Add1Self(V32& v, int wh) {
  if (v.empty() || v.back() < wh) {
    v.PB(wh);
    return;
  }
  int i = 0;
  while (v[i] < wh) {
    i++;
  }
  if (v[i] == wh) {
    v.erase(v.begin() + i);
    return;
  }
  v.insert(v.begin() + i, wh);
}
V32 ones[N];
V32 lower[N];
int ress[N];
// chce miec xory prefiksowe wewnatrz blokow
// chce miec sumy prefiksowe xorow prefiksowych
// chce miec
const int B = 500;
int32_t xor_pref_in_block[N][2];
int32_t xor_pref_of_blocks[N][2];
int32_t block_ind[N];
int32_t L[N];
int32_t R[N];
int32_t sum_pref_in_block[N][2];
int32_t sum_pref_of_blocks[N][2];
int32_t blocks_cnt;
int32_t Ask(int32_t tr, int32_t ind) {
  int32_t wh_block = block_ind[ind];
  if (xor_pref_of_blocks[wh_block - 1][tr] == 0) {
    return sum_pref_of_blocks[wh_block - 1][tr] + sum_pref_in_block[ind][tr];
  } else {
    return (ind - L[wh_block] + 1) - sum_pref_in_block[ind][tr] + sum_pref_of_blocks[wh_block - 1][tr];
  }
}
void Flip(int32_t tr, int32_t ind) {
  int32_t cur_ind = ind;
  int32_t bl = block_ind[ind];
  while (block_ind[cur_ind] == bl) {
    xor_pref_in_block[cur_ind][tr] ^= 1;
    sum_pref_in_block[cur_ind][tr] = sum_pref_in_block[cur_ind - 1][tr] * (cur_ind != L[bl]) + xor_pref_in_block[cur_ind][tr];
    cur_ind++;
  }
  int32_t cur_bl = bl;
  while (cur_bl <= blocks_cnt) {
    if (xor_pref_of_blocks[cur_bl - 1][tr]) {
      sum_pref_of_blocks[cur_bl][tr] = sum_pref_of_blocks[cur_bl - 1][tr] + (B - sum_pref_in_block[R[cur_bl]][tr]);
    } else {
      sum_pref_of_blocks[cur_bl][tr] = sum_pref_of_blocks[cur_bl - 1][tr] + sum_pref_in_block[R[cur_bl]][tr];
    }
    xor_pref_of_blocks[cur_bl][tr] ^= 1;
    cur_bl++;
  }
}
int32_t main() {

  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(10);
  cin.tie(0);
  //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
  
  srand(2137);
  int n, q;
#ifdef TEST
  n = 3e5;
  q = 3e5;
#else
  cin>>n>>q;
#endif
  int last = 0;
  while (last <= n) {
    blocks_cnt++;
    L[blocks_cnt] = last + 1;
    last += B;
    R[blocks_cnt] = last;
    FOR (i, L[blocks_cnt], R[blocks_cnt]) {
      block_ind[i] = blocks_cnt;
    }
  }
  debug(blocks_cnt);
  RE (i, n - 1) {
#ifdef TEST
    if (rand() % 500 == 0) {
      a[i] = 2;
    } else {
      a[i] = 3;
    }
#else
    cin>>a[i];
#endif
  }
  suf_prod[n] = 1;
  suf_sum[n] = 1;
  // suf_sum[i] = ile wierzcholkow w poddrzewie ukorzenionym w warstwie i
  FORD (i, n - 1, 1) {
    if (a[i] % 2 == 0) {
      how_many_odd_from_now[i] = 0;
    } else {
      how_many_odd_from_now[i] = how_many_odd_from_now[i + 1] + 1;
    }
    suf_prod[i] = suf_prod[i + 1] * a[i] % P;
    suf_sum[i] = (suf_sum[i + 1] * a[i] + 1) % (2 * P);
  }
  int total_odd = suf_sum[1] % 2;
  RE (i, n) {
    suf_sum[i] %= P;
  }
  int sz = suf_sum[1];
  pref_prod[0] = 1;
  RE (i, n) {
    pref_prod[i] = pref_prod[i - 1] * a[i] % P;
    in_layer[i] = pref_prod[i - 1];
    sum_dis[1] = (sum_dis[1] + in_layer[i] * (i - 1)) % P;
  }
  FOR (i, 2, n) {
    sum_dis[i] = (sum_dis[i - 1] - 2 * suf_sum[i] + sz + 2 * P) % P;
  }
  
  function<void(V32&, int, int, int)> AddSubtree = [&](V32& v, int i, int is_full, int padding) {
    if (i > n) { return; }
    Add1Self(v, padding);
    if (i == n) {
      Add1Self(v, padding + 1);
      return;
    }
    if (a[i] % 2 == is_full) {
      Add1Self(v, padding + 2 + how_many_odd_from_now[i + 1]);
    } else {
      Add1Self(v, padding + 1);
    }
  };
      
  FORD (i, n, 1) {
    ones[i] = ones[i + 1];
    AddSubtree(ones[i], i, 0, 0);
    //debug(ones[i].size());
  }
  
//   FOR (i, 2, n) {
//     lower[i] = lower[i - 1];
//     for (auto& x : lower[i]) {
//       x++;
//     }
//     AddSubtree(lower[i], i - 1, 0, 1);
//     debug(lower[i].size());
//   }
  
  vector<vector<Query>> by_wl(n + 2);
  RE (i, q) {
#ifdef TEST
    int wa = rand() % n + 1;
    int wb = rand() % n + 1;
    int wl = rand() % n + 1;
    if (wa < wl) { swap(wa, wl); }
    if (wb < wl) { swap(wb, wl); }
#else
    int wa, wb, wl;
    cin>>wa>>wb>>wl;
#endif
    by_wl[wl].PB({wa, wb, i});
  }
  
  int glob_odd = 0;
  RE (wl, n) {
    int32_t offset = n - wl;
    int32_t sw = 0;
    if (!by_wl[wl].empty() && glob_odd) {
      FOR (l, offset, offset + 1) {
        Flip(l % 2, l / 2);
        //by_par[l % 2].PB(l / 2);
      }
      assert(xor_pref_of_blocks[blocks_cnt][0] == 0);
      assert(xor_pref_of_blocks[blocks_cnt][1] == 0);
      
      //Flip(0, offset / 2);
      //Flip(1, offset / 2);
      sw = 1;
    }
    int32_t glob_res = sum_pref_of_blocks[blocks_cnt][0] + sum_pref_of_blocks[blocks_cnt][1];
    debug(wl, glob_res);
    for (auto query : by_wl[wl]) {
      int32_t wa = query.wa;
      int32_t wb = query.wb;
      int32_t ind = query.ind;
      int32_t res = (sum_dis[wa] - sum_dis[wb] + P) % P;
      int32_t path_len = wa + wb - 2 * wl;
      V32 acc;
      AddSubtree(acc, wa + 1, 1, 1);
      //debug("after wa+1", acc);
      AddSubtree(acc, wb + 1, 1, 1);
      //debug("after wb+1", acc);
      AddSubtree(acc, wl, 1, 0);
      //debug("after wl", acc);
      acc = Merge(ones[min(wa, wb) + 1], acc);
      acc = Merge(ones[max(wa, wb) + 1], acc);
      //debug("after sides", acc);
      int32_t odd = 0;
      assert(SZ(acc) % 2 == 0);
      REP (i, SZ(acc)) {
        acc[i] += offset;
        if (acc[i] % 2 == 1) {
          odd ^= 1;
        }
      }
      vector<V32> by_par(2);
      if (odd) {
        FOR (l, offset, offset + 1) {
          by_par[l % 2].PB(l / 2);
        }
      }
      for (auto x : acc) {
        by_par[x % 2].PB(x / 2);
      }
      int32_t local_res = glob_res;
      REP (tr, 2) {
        assert(SZ(by_par[tr]) % 2 == 0);
        for (int i = 0; i < SZ(by_par[tr]); i += 2) {
          int32_t a = by_par[tr][i];
          int32_t b = by_par[tr][i + 1] - 1;
          int32_t odd_prefs = Ask(tr, b) - Ask(tr, a - 1);
          assert(odd_prefs >= 0);
          int32_t len = b - a + 1;
          local_res = (local_res + len - 2 * odd_prefs + 2 * P) % P;
        }
      }
      
      
      
      res = (res + 2 * local_res) % P;
      if (total_odd) {
        res = (res + path_len) % P;
      }
      res = (res % P + P) % P;
      res = 1ll * res * ((P + 1) / 2) % P;
      ress[ind] = res;
    }
    if (sw) {
      FOR (l, offset, offset + 1) {
        Flip(l % 2, l / 2);
        //by_par[l % 2].PB(l / 2);
      }
    }
    int st = n - wl;
    int en = st;
    if (a[wl] % 2 == 0) {
      en += 1 + how_many_odd_from_now[wl + 1];
    }
    VI vals{st, en + 1};
    debug(vals);
    for (auto val : vals) {
      int tp = val % 2;
      glob_odd ^= tp;
      val /= 2;
      Flip(tp, val);
    }
    debug(glob_odd);
  }
  
  RE (i, q) {
    cout<<ress[i]<<"\n";
  }
  return 0;
}