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
#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
typedef double lf;
typedef long double llf;
typedef pair<int,int> pii;
typedef pair<lld,lld> pll;
#define For(i,s,a) for(int i = (int)s; i < (int)a; ++i)
#define rpt(s, it) for(auto it = s.begin(); it != s.end(); ++it)
#define brpt(s, it) for(auto it = s.rend(); it != s.rbegin(); --it)
#define sz size()
#define pb push_back
#define eb emplace_back
#define ff first
#define dd second
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ZAPS {int t; scanf("%i", &t); while(t--) solve();}
#define make_unique(x) (x).erase( unique(all(x)), (x).end())


template<typename Ta, typename Tb>
ostream & operator <<(ostream & os, pair<Ta, Tb> x){
	return os << x.ff << " " << x.dd;
}

const int N = 3e5 + 1;
const lld mod = 1e9 + 7;
const lld div2 = 5e8 + 4;
int n;

struct node{
	int xd;
	int yd;
	lld ile;
	bool par;
	node(){
		xd = yd = ile = par = 0;
	}
	node(int XD, int YD, lld ILE, bool PAR){
		xd = XD, yd = YD, ile = ILE, par = PAR;
	}
};

bool cmp(node a, node b){
	return (a.xd + a.yd > b.xd + b.yd);
}

vector<node>nodes;

lld deg[N];
lld silnia[N];

int x, y, r;

lld dfs(int level, int par, node info);

lld basic_step(int level, int par, node info, int decr_deg = 0){
	if(deg[level] - decr_deg == 0)
		return 0;
	node next_info = node(info.xd + 1, info.yd + 1, (info.ile * (deg[level] - decr_deg)) % mod, info.par & ((deg[level] - decr_deg) & 1ll));
	return dfs(level + 1, par, next_info);		
}

lld dfs(int level, int par, node info){
	if(par == 0 && !info.par){
	//	cout<<level<<" "<<info.xd<<" "<<info.yd<<" "<<info.ile<<"   "<<silnia[level]<<endl;
		return ((((info.ile * div2) % mod * (info.xd - info.yd + mod)) % mod) * silnia[level]) % mod;
	}
	/*
	par = 3 -> above both (lca or higher)
	par = 2 -> above y
	par = 1 -> above x
	par = 0 somewhere else
	*/
	//cout<<level<<" "<<par<<" "<<info.xd<<" "<<info.yd<<" "<<info.ile<<" "<<info.par<<endl;
	nodes.pb(info);
	if(level == n)
		return 0;
	lld ret = 0;
	switch(par){
		case 0:
			ret += basic_step(level, par, info);
			break;
		case 1:
			if(level == x){
				ret += basic_step(level, 0, info);
			}
			else{
				node next_info = node(info.xd - 1, info.yd + 1, info.ile, info.par);
				ret += dfs(level + 1, par, next_info);
				ret += basic_step(level, 0, info, 1);
			}
			break;
		case 2:
			if(level == y){
				ret += basic_step(level, 0, info);
			}
			else{
				node next_info = node(info.xd + 1, info.yd - 1, info.ile, info.par);
				ret += dfs(level + 1, par, next_info);
				ret += basic_step(level, 0, info, 1);
			}
			break;
		case 3:
			if(level == r){
				if(level == x){
					nodes.pop_back();
					ret += dfs(level, 2, info);
				}
				else
				if(level == y){
					nodes.pop_back();
					ret += dfs(level, 1, info);
				}
				else
				{
					node next_info = node(info.xd - 1, info.yd + 1, info.ile, info.par);
					ret += dfs(level + 1, 1, next_info);
					next_info = node(info.xd + 1, info.yd - 1, info.ile, info.par);
					ret += dfs(level + 1, 2, next_info);
					ret += basic_step(level, 0, info, 2);
				}
			}
			else{
				node next_info = node(info.xd - 1, info.yd - 1, info.ile, info.par);
				ret += dfs(level + 1, 3, next_info);
				ret += basic_step(level, 0, info, 1);
			}
			break;
	}
	return ret % mod;
}

void solve(){
	nodes.clear();
	scanf("%d%d%d", &x, &y, &r);
	lld wyn = dfs(1, 3, node(x - 1, y - 1, 1, 1));
	//cout<<wyn<<endl;
	//cout<<nodes.sz<<endl;
	//return;
	bool neq = 0;
	sort(all(nodes), cmp);
	for(auto s : nodes){
		lld ile = s.ile;
		bool pari = s.par;
		if(pari) ile = (ile - 1 + mod) % mod;		//boostme.cpp
		(ile *= div2) %= mod;
		(wyn += ((lld)(s.xd - s.yd) * ile) % mod + mod) %= mod;
		if(pari){
			neq ^= 1;
			if(neq)
				(wyn += s.xd) %= mod;
			else
				(wyn += mod - s.yd) %= mod;
		}
	}
	printf("%lld\n", wyn);
}

int32_t main(void){
	int q;
	scanf("%d%d", &n, &q);
	For(i, 1, n)
		scanf("%lld", &deg[i]);
	silnia[n] = 1;
	for(int i = n - 1;i; --i)
		silnia[i] = (silnia[i + 1] * deg[i] + 1) % mod;
		
	deg[n] = 0;
	while(q--)
		solve();
}

/*
3 3
3 2
3 2 1
1 1 1
2 3 2
*/