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

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<long long, long long> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

#define Size(x) (int)x.size()
#define VAR(v,n) auto v = (n)
#define FOR(i,a,b) for(VAR(i,a); i < (b); ++i)
#define FORE(i,a,b) for(VAR(i,a); i <= (b); ++i)
#define FORREV(i,a,b) for(VAR(i,b); i >= (a); --i)
#define FORSTEP(i,a,b,step) for(VAR(i,a); i < (b); i += (step))
#define FOREACH(i,c) for(auto i : (c))
#define FOREACHS(i,c,n) for(VAR(i,&(c)[0]); i-(c)<(n); ++i)
#define ALL(x) x.begin(),x.end()
#define CLEAR(t) memset(t, 0, sizeof(t))
#define F first
#define S second
#define MP make_pair
#define PUB push_back
#define POB pop_back
#define pieprzu ios_base::sync_with_stdio(0);

#define debugOn 0
#define debug(a,b) if (debugOn) cout << a << ": " << b << endl;

const int    INF = 1000000001;
const double EPS = 10e-9;

const int MOD = 1000000007;

const int MAXN = 300000;
const int TREE_SIZE = (1 << 20);
int n;

int t[MAXN + 5];

pii positions[MAXN + 5];

vpii tempEven;
int treeEven[TREE_SIZE + 5];
int treeEvenBegin;
set<pii> valuesEvenSet;

vpii tempOdd;
int treeOdd[TREE_SIZE + 5];
int treeOddBegin;
set<pii> valuesOddSet;

void read() {
    scanf("%d", &n);
    FOR (i, 0, n) {
        scanf("%d", &t[i]);
    }
}

void assignPositions() {
    pii valueToAdd;
    int add = 0;
    FOR (i, 0, n) {
        valueToAdd = MP((MOD - add) % MOD, i);
        debug("valueToAdd", valueToAdd.F);
        if (valueToAdd.F & 1) {
            debug("adding to temp odd", i);
            tempOdd.PUB(valueToAdd);
        } else {
            debug("adding to temp even", i);
            tempEven.PUB(valueToAdd);
        }
        add = (add + t[i]) % MOD;
        debug("add", add);
    }

    sort(ALL(tempEven));
    sort(ALL(tempOdd));

    int idx = 0;
    FOREACH (value, tempEven) {
        positions[value.S] = MP(0, idx++);
    }
    for (treeEvenBegin = 1; treeEvenBegin < Size(tempEven); treeEvenBegin <<= 1);
    treeEvenBegin = max(treeEvenBegin, 2);

    idx = 0;
    FOREACH (value, tempOdd) {
        positions[value.S] = MP(1, idx++);
    }
    for (treeOddBegin = 1; treeOddBegin < Size(tempOdd); treeOddBegin <<= 1);
    treeOddBegin = max(treeOddBegin, 2);
}

void insertIntoTree(int *tree, int treeBegin, int position, int cnt) {
    if (debugOn) cout << "insert into tree on position " << position << " value " << cnt << endl;
    for (int i = treeBegin + position; i >= 1; i >>= 1) {
        tree[i] = (tree[i] + cnt) % MOD;
    }

    FOR (i, 1, treeBegin << 1) {
        if (debugOn) cout << tree[i] << " ";
    }
    if (debugOn) cout << endl;
}

int getSumToTheRightIncluding(int *tree, int treeBegin, int position) {
    debug("search right from position", position);
    debug("search right start", treeBegin + position);
    if (position == n) {
        return 0;
    }

    int result = tree[treeBegin + position];
    for (int i = treeBegin + position; i > 1; i >>= 1) {
        if (!(i & 1)) {
            result = (result + tree[i + 1]) % MOD;
        }
    }

    debug("right result", result);
    return result;
}

int getSumToTheLeftIncluding(int *tree, int treeBegin, int position) {
    debug("search left from position", position);
    debug("search left start", treeBegin + position);
    FOR (i, 1, treeBegin << 1) {
        if (debugOn) cout << "(" << i << ") " << tree[i] << " ";
    }
    if (debugOn) cout << endl;
    if (position < 0) {
        return 0;
    }

    int result = tree[treeBegin + position];
    debug("rLeft", result);
    for (int i = treeBegin + position; i > 1; i >>= 1) {
        if (i & 1) {
            result = (result + tree[i - 1]) % MOD;
        }
        debug("rLeft", result);
    }

    debug("left", result);
    return result;
}

void solve() {
    int valueToInsert;
    pii valueToSearch;
    int add = 0;
    int prevResult = 1;
    FOR (i, 0, n) {
        valueToInsert = (MOD - add) % MOD;
        add = (add + t[i]) % MOD;
        debug("element", i);
        debug("valueToInsert", valueToInsert);
        debug("add", add);

        if (positions[i].F & 1) {
            debug("insert odd", prevResult);
            insertIntoTree(treeOdd, treeOddBegin, positions[i].S, prevResult);
            valuesOddSet.insert(MP(valueToInsert, positions[i].S));
        } else {
            debug("insert even", prevResult);
            insertIntoTree(treeEven, treeEvenBegin, positions[i].S, prevResult);
            valuesEvenSet.insert(MP(valueToInsert, positions[i].S));
        }

        valueToSearch = MP(MOD - add, 0);
        debug("valueToSearch", valueToSearch.F);
        auto itEven = valuesEvenSet.lower_bound(valueToSearch);
        auto itOdd = valuesOddSet.lower_bound(valueToSearch);
        debug("itEven.F", itEven->F);
        debug("itEven.S", itEven->S);
        debug("itOdd.F", itOdd->F);
        debug("itOdd.S", itOdd->S);

        if (add & 1) {
            debug("search odd", -1);
            prevResult = getSumToTheRightIncluding(treeEven, treeEvenBegin, itEven == valuesEvenSet.end() ? n : itEven->S);
            prevResult = (prevResult + getSumToTheLeftIncluding(treeOdd, treeOddBegin, itOdd == valuesOddSet.end() ? treeOddBegin - 1 : itOdd->S - 1)) % MOD;
        } else {
            debug("search even", -1);
            prevResult = getSumToTheLeftIncluding(treeEven, treeEvenBegin, itEven == valuesEvenSet.end() ? treeEvenBegin - 1 : itEven->S - 1);
            prevResult = (prevResult + getSumToTheRightIncluding(treeOdd, treeOddBegin, itOdd == valuesOddSet.end() ? n : itOdd->S)) % MOD;
        }

        debug("prevResult", prevResult);
        if (debugOn) cout << endl;
    }

    printf("%d\n", prevResult);
}

void rob(int test) {
    read();
    assignPositions();
    solve();
}

int main() {
    int test = 1;
    //scanf("%d", &test);

    FORE (i, 1, test) rob(i);

    return 0;
}