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

using namespace std;

typedef long long ll;

const ll INF = 1000000000000000;
const int p = 1e9 + 7;

int f(int i) {
    return i & (i - 1);
}

int g(int i) {
    return (i | (i - 1)) + 1;
}

int rsq(int ft[], int i) {
    int res = 0;
    int ind = i;
    while (ind > 0) {
        res += ft[ind];
        res %= p;
        ind = f(ind);
    }

    return res;
}

int RSQ(int ft[], int i, int j) {
    int res;
    if (i <= j) {
        res = rsq(ft, j) - rsq(ft, i - 1);
        if (res < 0) {
            res += p;
        }
    }
    else {
        res = 0;
    }

    return res;
}

void add(int ft[], int N, int ind, int val) {
    int index = ind;
    while (index <= N) {
        ft[index] = (ft[index] + val) % p;
        index = g(index);
    }

    return;
}

//first element >= val
int bin_first(ll a[], int n, int val) {
    int poc = 1;
    int k = n;
    int m;

    while (poc < k) {
        m = (poc + k) / 2;
        if (a[m] >= val) {
            k = m;
        }
        else {
            poc = m + 1;
        }
    }

    if (a[k] < val) {
        k++;
    }

    return k;
}

//last element <= val
int bin_last(ll a[], int n, int val) {
    int poc = 1;
    int k = n;
    int m;

    while (poc < k) {
        m = (poc + k + 1) / 2;
        if (a[m] <= val) {
            poc = m;
        }
        else {
            k = m - 1;
        }
    }

    return k;
}

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

    int n;
    cin >> n;

    ll *a = new ll[n + 1];
    ll *S = new ll[n + 1];
    S[0] = 0;
    set<ll> *L = new set<ll>;
    L->insert(S[0]);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        a[i] = (a[i] * (p + 1) / 2) % p;
        S[i] = (S[i - 1] + a[i]) % p;
        L->insert(S[i]);
    }

    int N = L->size();
    ll *lset = new ll[N + 1];
    auto it = L->begin();
    for (int i = 1; i <= N; i++) {
        lset[i] = *it;
        it++;
    }

    delete a;
    delete L;

    int ft[N + 1];
    ft[1] = 1;
    for (int i = 2; i <= N; i++) {
        if (f(i) == 0) {
            ft[i] = 1;
        }
        else {
            ft[i] = 0;
        }
    }

    int d[n + 1];
    for (int m = 1; m <= n; m++) {
        if (S[m] >= (p - 1) / 2) {
            int i = bin_first(lset, N, S[m] - (p - 1) / 2);
            int j = bin_last(lset, N, S[m]);
            d[m] = RSQ(ft, i, j);
            add(ft, N, j, d[m]);
        }
        else {
            int i = bin_last(lset, N, S[m]);
            int j = bin_first(lset, N, S[m] + (p + 1) / 2);
            d[m] = (RSQ(ft, 1, i) + RSQ(ft, j, N)) % p;
            add(ft, N, i, d[m]);
        }
    }

    cout << d[n] << "\n";

    return 0;
}