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
// Wiktor Cupiał
// V LO Kraków
 
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define eb emplace_back
#define st first
#define nd second
#define vt vector
#define VAR(__var)  #__var << ": " << __var << " "
#define PAIR(__var) #__var << ": " << __var.first << ", " << __var.second << " "
#define FOR(__var, __start, __end)  for(int __var=__start; __var<__end; ++__var)
#define FORB(__var, __start, __end) for(int __var=__start; __var>__end; --__var)
#define maxi(__x, __y) __x = (__x>__y?__x:__y)
#define mini(__x, __y) __x = (__x<__y?__x:__y)
#define all(__var)     (__var).begin(),(__var).end()
#define rall(__var)    (__var).rbegin(),(__var).rend()
#define sz(__var)      (int)(__var).size()
#define satori         int __test; cin>>__test; while(__test--)
 
#ifndef DEBUG
#define DEBUG 0
#endif
#define debug if(DEBUG)
 
using namespace std;
 
using namespace __gnu_pbds;
template <typename T>
using ord_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
 
// templeos <3
typedef     int16_t i16;
typedef     int32_t i32;
typedef     int64_t i64;
typedef        void  u0;
typedef        bool  u1;
typedef        char  u8;
typedef    uint16_t u16;
typedef    uint32_t u32;
typedef    uint64_t u64;
typedef       float f32;
typedef      double f64;
typedef long double f86;
 
typedef       long long ll;
typedef     long double ld;
typedef   pair<ll, ll> pll;
typedef pair<int, int> pii;
 
const ll INF = 1e18+2137;
const ll mod = 1'000'000'000+7;

inline ll add(ll a, ll b) {
	return (a+b)%mod;
}

inline ll sub(ll a, ll b) {
	return (a-b+mod)%mod;
}

inline ll mul(ll a, ll b) {
	return (a*b)%mod;
}

void kwadrat(int n, vt<ll> &a) {
	vt<ll> pref(n+1);
	pref[0] = 0;
	FOR(i, 1, n+1)
		pref[i] = add(pref[i-1], a[i]);
	
	function<ll(int, int)> get_pref = [&](int l, int r) {
		return sub(pref[r], pref[l-1]);
	};

	vt<ll> dp(n+1, 0);
	dp[0] = 1;
	FOR(i, 1, n+1)
		FOR(j, 1, i+1)
			if(get_pref(j, i)%2 == 0)
				dp[i] = add(dp[i], dp[j-1]);

	cout << dp[n] << '\n';
}

void male(int n, vt<ll> &a) {
	ll np_sum = 0;
	ll p_sum = 1;

	ll res = 0;
	FOR(i, 1, n+1) {
		if(a[i]%2 == 1)
			swap(p_sum, np_sum);
		res = p_sum;
		p_sum = add(p_sum, res);
	}

	cout << res << '\n';
}

struct Block {
	int cap, size;
	ll sum, to_add;
	int left_iter;
	vt<pll> a; // st -> suma, nd -> wynik
	pll nic = {-1, -1};
public:
	Block() = delete;

	Block(int _cap) {
		cap = _cap;
		size = 0;
		sum = 0;
		to_add = 0;
		left_iter = 0;
		a.resize(cap, nic);
		a.shrink_to_fit();
	}

	void prop() {
		FOR(i, 0, size) {
			if(a[i] != nic)
				a[i].st = add(a[i].st, to_add);
		}
		to_add = 0;
	}

	void insert(ll x, ll val) {
		if(to_add != 0)
			prop();
		a[size] = {x, val};
		sum = add(sum, val);
		++size;
	}

	bool empty() {
		return (size==0);
	}

	bool full() {
		return (size==cap);
	}

	pll back() {
		if(to_add != 0)
			prop();
		return a[size-1];
	}

	pll get_left() {
		pll to_ret = a[left_iter];
		a[left_iter] = nic;
		++left_iter;
		--size;
		return to_ret;
	}

	void fix() {
		left_iter = 0;
		FOR(i, 0, cap)
			if(a[i] != nic) {
				a[left_iter] = a[i];
				a[i] = nic;
				++left_iter;
			}
		size = left_iter;
		left_iter = 0;
	}

	void pop() {
		if(to_add != 0)
			prop();
		sum = sub(sum, a[size-1].nd);
		a[size-1] = nic;
		--size;
	}

	ll minimum() {
		return add(a[0].st, to_add);
	}

	void add_to(ll x) {
		to_add = add(to_add, x);
	}

	ll get_sum() {
		return sum;
	}
};
inline void wyjmij_bloki(deque<Block> &q, ll &sm, stack<Block> &tar, ll &diff) {
	while(!q.empty()) {
		if(q.back().empty()) {
			q.pop_back();
			continue;
		}
	   	if(q.back().minimum() < diff)
			break;
		tar.push(q.back());
		sm = add(sm, q.back().get_sum());
		q.pop_back();
	}
}

inline void wyjmij_elementy(deque<Block> &q, ll &sm, Block &tar, ll &diff) {
	stack<pll> s;
	while(!q.back().empty() && q.back().back().st >= diff) {
		s.push(q.back().back());
		sm = add(sm, q.back().back().nd);
	   	q.back().pop();
	}

	if(q.back().empty())
		q.pop_back();

	while(!s.empty()) {
		tar.insert(s.top().st, s.top().nd);
	   	s.pop();
	}
}

inline void merge(deque<Block> &q, stack<Block> &s) {
	while(!s.empty()) {
		q.pf(s.top());
		s.pop();
	}
}

inline void popraw(deque<Block> &q) {
	int iter = 0;
	FOR(i, 1, sz(q)) {
		// i -> iter
		while(!q[iter].full() && !q[i].empty())
			q[iter].insert(q[i].get_left().st, q[i].get_left().nd);
		while(iter < sz(q) && q[iter].full())
			++iter;
		q[i].fix();
	}
}

void printb(Block x) {
	cout << "blok:\n";
	FOR(i, 0, x.cap)
		cout << x.a[i].st << ' ' << x.a[i].nd << '\n';
	cout << VAR(x.sum) << VAR(x.size) << VAR(x.to_add) << VAR(x.left_iter) << '\n';
}

void printq(deque<Block> q) {
	cout << "---\n";
	for(auto x : q) {
		printb(x);
	}
	cout << "---\n";
}

void solve(int n, vt<ll> &a) {

	const int sq = sqrt(n);
	deque<Block> parzy, nie_parzy;

	Block _starter(sq);
	_starter.insert(0, 1);
	parzy.pb(_starter);

	ll res = 0;
	FOR(i, 1, n+1) {
		stack<Block> parzy_do_nie, nie_do_parzy;
		Block ndp_block(sq);
		Block pdn_block(sq);
		ll pdn_sum = 0;
		ll ndp_sum = 0;
		ll diff = sub(mod, a[i]);
		/*
		cout << "przed: " << '\n';
		cout << "parzy:\n";
		printq(parzy);
		cout << "nie_parzy:\n";
		printq(nie_parzy);
		cout << "p\n";
		*/
		wyjmij_bloki(parzy, pdn_sum, parzy_do_nie, diff);

		if(!parzy.empty())
			wyjmij_elementy(parzy, pdn_sum, pdn_block, diff);
	
		//cout << "np\n";
		wyjmij_bloki(nie_parzy, ndp_sum, nie_do_parzy, diff);

		if(!nie_parzy.empty())
			wyjmij_elementy(nie_parzy, ndp_sum, ndp_block, diff);

		if(a[i]%2 == 1) {
			swap(parzy, nie_parzy);
			swap(pdn_sum, ndp_sum);
			swap(pdn_block, ndp_block);
			swap(parzy_do_nie, nie_do_parzy);
		}

		res = ndp_sum;
		for(auto &b : parzy)
			res = add(res, b.get_sum());
		/*
		cout <<"aaa\n";
		cout << a[i] << ' ' << diff << '\n';
		cout << "wynik:\n";
		cout << i << ' ' << res << '\n';
		cout << pdn_sum << ' ' << ndp_sum << '\n';
		*/
	
		if(!nie_do_parzy.empty() && !nie_do_parzy.top().full())
			nie_do_parzy.top().insert(0, res);
		else {
			Block tmp(sq);
			tmp.insert(-a[i], res);
			parzy.push_front(tmp);
		}

		merge(parzy, nie_do_parzy);
		merge(nie_parzy, parzy_do_nie);
		/*
		cout << "parzy:\n";
		printq(parzy);
		cout << "nie_parzy:\n";
		printq(nie_parzy);
		*/
		//if(i%sq == 0) {
		if(0) {
			popraw(parzy);
			popraw(nie_parzy);
		}

		for(auto &b : parzy)
			b.add_to(a[i]);

		for(auto &b : nie_parzy)
			b.add_to(a[i]);
	}

	cout << res << '\n';
}
 
int32_t main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	
	bool mi = true;
	vt<ll> a(n+1);
	FOR(i, 1, n+1) {
		cin >> a[i];
		if(a[i] > 103)
			mi = false;
	}

	if(n <= 3003)
		kwadrat(n, a);
	else if(mi)
		male(n, a);
	else
		solve(n, a);

	return 0;
}