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
#include <iostream>
#include <algorithm>

using namespace std;

#define mod 1000000007
#define ll long long int

ll getFromFenwick(ll index, ll* fenwick)
{
    ll sum = 0;

    ll start = 0;
    ll toCheck = index + 1;

    while (toCheck != 0)
    {
        ll div = 1;
        while (toCheck >= (div * 2))
        {
            div *= 2;
        }

        sum += fenwick[start + div - 1];
        sum %= mod;
        if (sum < 0)
            sum += mod;

        start += div;
        toCheck -= div;
    }

    return sum;
}

void updateFenwick(ll index, ll* fenwick, ll max, ll change)
{
    index += 1;

    ll div = 1;

    ll prevUpdated = -1;
    while (true)
    {
        ll rest = (index % div);

        ll next = 0;
        if (rest == 0)
            next = index;
        else
            next = index + div - rest;

        //cout << '!' << index << ' ' << next << '\n';

        if (next - 1 >= max) break;

        if (next - div < index && next - 1 != prevUpdated)
        {
            fenwick[next - 1] += change;
            fenwick[next - 1] %= mod;
            if (fenwick[next - 1] < 0)
                fenwick[next - 1] += mod;
            prevUpdated = next - 1;
        }

        div *= 2;
    }
    //cout << '\n';
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    /*ll* temp = new ll[7]();
    updateFenwick(0, temp, 7, 3);
    updateFenwick(1, temp, 7, 1);
    updateFenwick(2, temp, 7, 5);
    updateFenwick(4, temp, 7, -4);
    updateFenwick(5, temp, 7, -2);
    updateFenwick(6, temp, 7, 10);

    for (int i = 0; i < 7; i++)
    {
        cout << getFromFenwick(i, temp) << ' ' << temp[i] << '\n';
    }*/


    ll n;
    cin >> n;

    ll* numbers = new ll[n]();
    pair<ll, ll>* prefixSum = new pair<ll, ll>[n](); //correct
    pair<ll, ll>* prefixSumSorted = new pair<ll, ll>[n]();

    ll totalSum = 0;
    for (ll i = 0; i < n; i++)
    {
        cin >> numbers[i];

        totalSum += numbers[i];
        totalSum %= mod;
        if (totalSum < 0)
            totalSum += mod;

        prefixSum[i].first = totalSum;
        prefixSumSorted[i] = { totalSum, i };
    }

    sort(prefixSumSorted, prefixSumSorted + n);
   
    for (ll i = 0; i < n; i++)
    {
        prefixSum[prefixSumSorted[i].second].second = i;
    }

    ll* mopFenwick = new ll[n]();
    ll* nonMopFenwick = new ll[n]();

    ll overAllCount = 0;
    for (ll i = n - 1; i >= 0; i--)
    {
        ll sumHere = prefixSum[i].first;
        ll fenwickIndex = prefixSum[i].second;

        ll count = 0;

        if (i == n - 1)
        {
            count = 1;
        }
        else
        {
            if (sumHere % 2 == 0)
            {
                //everything smaller or equal to fenwickIndex in mop Fenwick
                count = getFromFenwick(fenwickIndex, mopFenwick);
            }
            else
            {
                count = getFromFenwick(fenwickIndex, nonMopFenwick);
            }
        }
        //read the count from fenwick tree
        
        if (sumHere % 2 == 0)//is mopmodulo
        {
            overAllCount += count;
            overAllCount %= mod;
            if (overAllCount < 0)
                overAllCount += mod;

            //every mopmodulo smaller or equal fenwickIndex
            updateFenwick(0, mopFenwick, n, count);
            if (fenwickIndex != n - 1)
                updateFenwick(fenwickIndex + 1, mopFenwick, n, -count);
            //every non-modmodulo greater than fenwickIndex
            if (fenwickIndex != n - 1)
                updateFenwick(fenwickIndex + 1, nonMopFenwick, n, count);
        }
        else
        {
            //every modmopulo greater
            if (fenwickIndex != n - 1)
                updateFenwick(fenwickIndex + 1, mopFenwick, n, count);
            //every non-mopmodulo smaller or equal
            updateFenwick(0, nonMopFenwick, n, count);
            if (fenwickIndex != n - 1)
                updateFenwick(fenwickIndex + 1, nonMopFenwick, n, -count);
        }

        /*cout << count << '\n';
        for (ll i = 0; i < n; i++)
        {
            cout << mopFenwick[i] << ' ';
        }
        cout << '\n';

        for (ll i = 0; i < n; i++)
        {
            cout << nonMopFenwick[i] << ' ';
        }
        cout << '\n';
        cout << '\n';*/

    }

    cout << overAllCount << '\n';
}