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
#include <bits/stdc++.h>
#define REP(i,n) for(int _n=(n), i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define DEBUG(x) std::cerr << #x << " = " << (x) << std::endl;
typedef long long LL; typedef unsigned long long ULL;

void init_io() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
}

template<unsigned MOD>
class Modulo {
public:
  constexpr Modulo(unsigned x=0):v(x) {}
  unsigned get() const { return v; }
  Modulo operator+(Modulo b) const {
    unsigned res = v+b.v;
    if (res >= MOD) res -= MOD;
    return res;
  }
  void operator+=(Modulo b) { *this = *this + b; }
  Modulo operator-(Modulo b) const { return *this + Modulo(MOD-b.v); }
  void operator-=(Modulo b) { *this = *this - b; }
  Modulo operator*(Modulo b) const { return Modulo(ULL(v) * ULL(b.v) % MOD); }
  void operator*=(Modulo b) { *this = *this * b; }

  friend inline Modulo operator+(unsigned a, Modulo b) { return Modulo(a) + b; }
  friend inline Modulo operator-(unsigned a, Modulo b) { return Modulo(a) - b; }
  friend inline Modulo operator*(unsigned a, Modulo b) { return Modulo(a) * b; }

  static Modulo from_int(int x) {
    unsigned xx = x;
    if (x<0) xx += MOD;
    return Modulo(xx);
  }
private:
  unsigned v;
};


using Mod = Modulo<2 * 1'000'000'007>;

// Tree depth. One lower than in problem statement.
int n;

int num_queries;

// k < n
// a[k] = degree at depth k,0
std::vector<int> a;

// 0 <= k <= n
// sum_ones[k] = sum(1)  for subtree at depth k = size of subtree
std::vector<Mod> sum_ones;

// 0 <= k <= n
// sum(depth) for subtree at depth k
std::vector<Mod> sum_depth;

// 0 <= k <= n+1: sum(i<k) sum_ones[i]
std::vector<Mod> sum_ones_prefix_sum;

// 0 <= k <= n: number of consecutive odd entries in a[k], a[k+1], ...
std::vector<int> consecutive_odd;

void read_tree() {
  std::cin >> n >> num_queries;
  --n;
  a.resize(n);
  REP(i, n) std::cin >> a[i];
}

void calc_sum_ones() {
  sum_ones.resize(n+1);
  sum_ones[n] = 1;
  FORD(i, n-1, 0) sum_ones[i] = a[i] * sum_ones[i+1] + 1;
}

void calc_sum_depth() {
  sum_depth.resize(n+1);
  sum_depth[n] = n;
  FORD(i, n-1, 0) sum_depth[i] = a[i] * sum_depth[i+1] + i;
}

void calc_sum_ones_prefix_sum() {
  sum_ones_prefix_sum.resize(n+2);
  sum_ones_prefix_sum[0] = 0;
  FOR(i, 0, n) sum_ones_prefix_sum[i+1] = sum_ones_prefix_sum[i] + sum_ones[i];
}

void calc_consecutive_odd() {
  consecutive_odd.resize(n+1);
  consecutive_odd[n] = 0;
  FORD(i, n-1, 0) {
    if (a[i] & 1) {
      consecutive_odd[i] = consecutive_odd[i+1] + 1;
    } else {
      consecutive_odd[i] = 0;
    }
  }
}

void process_tree() {
  calc_sum_ones();
  calc_sum_depth();
  calc_sum_ones_prefix_sum();
  calc_consecutive_odd();
}

Mod sum_distances(int A) {
  return (A+2) * sum_ones[0] + sum_depth[0] - 2 * sum_ones_prefix_sum[A+1];
}

Mod calc_bonuses2_brute(int A, int B, int L) {
  static std::vector<char> bonus;
  // First do flips.
  bonus.assign(2*n+2, 0);
  REP(i, L) {
    int max = (a[i]&1) ? 0 : 1 + consecutive_odd[i+1];
    bonus[n - i] ^= 1;
    bonus[n - i +1+max] ^= 1;
  }

  {
    int i=L;
    int max;
    if(B==L && A > L) {
      max = (a[i]&1) ? 0 : 1 + consecutive_odd[i+1];
    } else {
      max = consecutive_odd[i];
    }
    bonus[n-i] ^= 1;
    bonus[n - i + 1+max] ^= 1;
  }

  // L < i < B: no bonuses
  if (B > L) {
    int i=B;
    if (A > B) {
      int max = 1 + consecutive_odd[i+1];
      bonus[n-L + 1] ^= 1;
      bonus[n-L + 1+max] ^= 1;
    } else {
      // i=A=B>L: no bonuses
    }
  }

  FOR(i,B+1,A-1) {
    int max = (a[i]&1) ? 0 : 1 + consecutive_odd[i+1];
    bonus[n-L] ^= 1;
    bonus[n-L + 1+max] ^= 1;
  }

  {
    int i=A;
    if (A>B) {
      int max = consecutive_odd[i];
      bonus[n-L] ^= 1;
      bonus[n-L + 1+max] ^= 1;
    }
  }

  char on = 0;
  for (char &b : bonus) {
    on ^= b;
    b = on;
  }
  assert(!on);

  int sign = 1;
  int res = 0;
  FORD(i,n,-n) {
    if (bonus[n+i]) {
      res += sign * i;
      sign = -sign;
    }
  }
  return Mod::from_int(res);
}

// Each bonus is between -n and n. depth(X) - LCA(A,X) - LCA(B,X)
Mod calc_bonuses2(int A, int B, int L) {
  if (A<B) std::swap(A,B);
  return calc_bonuses2_brute(A, B, L);
}

Mod calc_bonuses(int A, int B, int L) {
  Mod res = 2 * calc_bonuses2(A, B, L);
  // bonus = A+B + 2 * bonus2
  // if there are odd number of moves, we get an extra A+B
  if (sum_ones[0].get() % 2u != 0) res += A + B;
  return res;
}

Mod calc_double_game_score(int A, int B, int L) {
  const Mod sum_distances_A = sum_distances(A);
  const Mod sum_distances_B = sum_distances(B);
  const Mod bonuses = calc_bonuses(A, B, L);
  return sum_distances_A - sum_distances_B + bonuses;
}

void read_and_process_queries() {
  REP(i, num_queries) {
    int A, B, L;
    std::cin >> A >> B >> L;
    --A; --B; --L;
    const unsigned double_game_score_remainder = calc_double_game_score(A, B, L).get();
    assert(double_game_score_remainder % 2u == 0);
    std::cout << (double_game_score_remainder / 2u) << "\n";
  }
}

int main() {
  init_io();
  read_tree();
  process_tree();
  read_and_process_queries();
}