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
#include <bits/stdc++.h>

using namespace std;

#define ll long long

#define DEBUGg

const int MOD = 1'000'000'007;

class node
{
public:
    int sum_suf, shift, t, sum_t; // klucz == sum_suf
    node *left, *right, *parent;
};


void recursive_draw_tree(node * x, int shift)
{
    if (x == nullptr)
        return;

    cout << "(";
    recursive_draw_tree(x->left, (shift + x->shift) % MOD);
    cout << "<suf=" << (((x->sum_suf + x->shift) % MOD) + shift) % MOD << " t=" << x->t << " sum_t=" << x->sum_t << ">";
    recursive_draw_tree(x->right, (shift + x->shift) % MOD);
    cout << ")";
}

void draw_tree(node * x)
{
#ifdef DEBUG
    recursive_draw_tree(x, 0);
    cout << endl;
#endif
}


void push_down_shift (node* x)
{
    if (x->left)
        x->left->shift = (x->left->shift + x->shift) % MOD;
    if (x->right)
        x->right->shift = (x->right->shift + x->shift) % MOD;

    x->sum_suf = (x->sum_suf + x->shift) % MOD;
    x->shift = 0;
}

//updates data in x based on children
void update(node* x)
{
    x->sum_t = x->t;
    if (x->left)
        x->sum_t = (x->sum_t + x->left->sum_t) % MOD;
    if (x->right)
        x->sum_t = (x->sum_t + x->right->sum_t) % MOD;
}

// right-rotates a subtree rooted at x.
void rightRotate(node *x)
{
    node *y = x->left;
    node *p = x->parent;
    node *b = y->right;

    // link x's parent to y
    if (p) {
        if (x == p->left)
            p->left = y;
        else
            p->right = y;
    }
    y->parent = p;

    // link the b subtree to x
    if (b)
        b->parent = x;
    x->left = b;

    // swap x and y
    x->parent = y;
    y->right = x;

    //update data, bottom-up
    update(x);
    update(y);
}

// left-rotates a subtree rooted at x.
void leftRotate(node *x)
{
    node *y = x->right;
    node *p = x->parent;
    node *b = y->left;

    // link x's parent to y
    if (p) {
        if (x == p->left)
            p->left = y;
        else
            p->right = y;
    }
    y->parent = p;

    // link the b subtree to x
    if (b)
        b->parent = x;
    x->right = b;

    // swap x and y
    x->parent = y;
    y->left = x;

    //update data, bottom-up
    update(x);
    update(y);
}

void splay(node *x)
{
    while(x->parent)
    {
        node* p = x->parent;
        node* pp = p->parent;
        if (pp == nullptr) //Zig or zag
        {
            if(p->left == x)
                rightRotate(p);
            else
                leftRotate(p);
        }
        else if(pp->left == p)
        {
            if(p->left == x)
            {   //ZigZig
                rightRotate(pp);
                rightRotate(p);
            }
            else
            {    //ZigZag
                leftRotate(p);
                rightRotate(pp);
            }
        }
        else // pp->right == p
        {
            if(p->left == x)
            {   //ZagZig
                rightRotate(p);
                leftRotate(pp);
            }
            else
            {   //ZagZag
                leftRotate(pp);
                leftRotate(p);
            }
        }
    }
}

node* newNode()
{
    node* x = new node();
    x->t = x->sum_t = 0;
    x->left = x->right = x->parent = nullptr;
    x->shift = 0;
    x->sum_suf = 0;
    return (x);
}

// inserts to tree, if node with key is found, adds t.
// side effect: all nodes from root to the element get shifted
node* insert_key(node* x, int key, int t)
{
    if (x == nullptr) {
        node * y = newNode();
        y->t = t;
        y->sum_t = t;
        y->sum_suf = key;
        return y;
    }

    while (x)
    {
        push_down_shift(x);

        if (key == x->sum_suf) {
            x->t = (x->t + t) % MOD;
            x->sum_t = (x->sum_t + t) % MOD;
            break;
        } else if (key < x->sum_suf) {
            if (x->left)
                x = x->left;
            else {
                node * y = newNode();
                y->t = t;
                y->sum_t = t;
                y->sum_suf = key;
                x->left = y;
                y->parent = x;
                x = y;
                break;
            }
        } else { // (key > x->sum_suf)
            if (x->right)
                x = x->right;
            else {
                node * y = newNode();
                y->t = t;
                y->sum_t = t;
                y->sum_suf = key;
                x->right = y;
                y->parent = x;
                x = y;
                break;
            }
        }
    }

    splay(x);
    return x;
}

// splits tree to elements < a and >= a.
pair<node*,node*> split(node* root, int a) {
    if (root == nullptr)
        return {nullptr, nullptr};

    node* x = insert_key(root, a, 0);

    node* l = x->left;
    if (l) {
        l->parent = nullptr;
        x->sum_t = (MOD + x->sum_t - l->sum_t) % MOD;
        x->left = nullptr;
    }

    node* r;

    if (x->t > 0)
        r = x;
    else {
        r = x->right;
        if (r)
            r->parent = nullptr;
        delete x;
    }

    return {l,r};
}

node* tree_max(node* r)
{
    node* x = r;
    push_down_shift(x);
    while (x->right)
    {
        x = x->right;
        push_down_shift(x);
    }
    return x;
}

// assumes all keys in a are <= all keys in b.
// joins a and b
// returns the root of a new tree
node* join(node* a, node* b)
{
    if (a == nullptr)
        return b;

    if (b == nullptr)
        return a;

    node* x = tree_max(a);
    splay(x);
    x->right = b;
    b->parent = x;
    update(x);
    return x;
}

void shift_trees(node* &odd, node* &even, int a) {
    auto [e1, e2] = split(even, MOD - a);
    auto [o1, o2] = split(odd, MOD - a);

#ifdef DEBUG
    cout << "SHIFT(odd,even,"<<a<<")" << endl;
    cout << "e1: ";
    draw_tree(e1);
    cout << "e2: ";
    draw_tree(e2);
    cout << "o1: ";
    draw_tree(o1);
    cout << "o2: ";
    draw_tree(o2);
#endif

    for (auto x : {e1, e2, o1, o2})
        if (x)
            x->shift = (x->shift + a) % MOD;

    if (a % 2 == 0) {
        even = join(o2, e1);
        odd = join(e2, o1);
    } else {
        even = join(e2, o1);
        odd = join(o2, e1);
    }

#ifdef DEBUG
    cout << "AFTER SHIFT:" << endl;
    cout << "even: ";
    draw_tree(even);
    cout << "odd: ";
    draw_tree(odd);
#endif
}

int main() {
    std::ios::sync_with_stdio(0);

    int n;
    cin >> n;
    vector<int> a(n);

    a[0] = 0;
    for (int i=0; i<n; ++i)
        cin >> a[i];

    node* odd = nullptr;
    node* even = nullptr;

    int t = 1;

    for (int i=0; i<n; ++i) {
#ifdef DEBUG
        cout << "------------------------" << endl;
        cout << "i=" << i << endl;
#endif

        shift_trees(odd, even, a[i]);

        if (a[i] % 2)
            odd = insert_key(odd, a[i], t);
        else
            even = insert_key(even, a[i], t);

#ifdef DEBUG
        cout << "after i=" << i << endl;
        cout << "odd tree:" << endl;
        draw_tree(odd);
        cout << "even tree:" << endl;
        draw_tree(even);
#endif

        t = even ? even->sum_t : 0;
    }

    cout << (even ? even->sum_t : 0) << endl;

    return 0;
}