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
#include <bits/stdc++.h>
using namespace std;
long long TRE1[4000009];
long long TRE2[4000009];
long long pref[300009];
long long dp[300009];
int PUNKT1[300009];
int PUNKT2[300009];
bool tab1[3][300009];
pair<int,int>tab2[3][300009];
long long mod=1000000000+7;
long long mod2=2000000000+14;
long long t;
long long Przedzial(int l, int p,long long *Tre)
{
    l+=t;
    p+=t;
    int v=1;
    int P=t;
    long long wynik=0;
    while(true)
    {
        if(v*P+P-1<l||l>p)
        {
            v=v/2;
            P=P*2;
            if(v==0){break;}
        }
        else if(v*P==l&&v*P+P-1<=p)
        {
            wynik=(wynik+Tre[v])%mod;
            l+=P;
        }
        else
        {
            if(l<v*P+(P/2))
            {
                v=v*2;
                P=P/2;
            }
            else
            {
                v=v*2+1;
                P=P/2;
            }
        }
    }
    return wynik;
}
void dod(int v, long long dod,long long *Tre)
{
    v+=t;
    while(v!=0)
    {
        Tre[v]=(Tre[v]+dod)%mod;
        v=v/2;
    }
}
int TworzenieDrzewa(int x)
{
    x--;
    int u=1;
    while(x>0)
    {
        x=x/2;
        u=u*2;
    }
    return u;
}
int main()
{ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ///1=parszyste
    ///2=nieparszyste
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        long long x;
        cin>>x;
        pref[i]=(pref[i-1]+x)%mod2;
    }
    vector<pair<int,pair<int,int>>>skal;
    ///{(L v P v PUNKT),{ind,info}}
    ///info1=l i [0]
    ///info2=l i [1]
    ///info3=l i [2]
    ///info4=p i [0]
    ///info5=p i [1]
    ///info6=p i [2]
    ///info7=PUNKT
    for(int i=1;i<=n;i++)
    {
        long long u=pref[i];
        if(pref[i]<mod)
        {///<0,6>
            pair<long long,long long>xd1,xd2,xd3;
            ///xd1,xd2
            ///xd3
            xd1={0,u}; xd2={u+mod+1,mod2-1};
            xd3={u+1,u+mod};
            //cout<<xd1.first<<" "<<xd1.second<<endl;
            //cout<<xd2.first<<" "<<xd2.second<<endl;
            //cout<<xd3.first<<" "<<xd3.second<<endl<<endl;
            if(pref[i]%2==0)
            {///<0,6>parzyste
                tab1[0][i]=0;
                tab1[1][i]=0;
                tab1[2][i]=1;
                skal.push_back({xd1.first,{i,1}});
                skal.push_back({xd2.first,{i,2}});
                skal.push_back({xd3.first,{i,3}});
                skal.push_back({xd1.second,{i,4}});
                skal.push_back({xd2.second,{i,5}});
                skal.push_back({xd3.second,{i,6}});
                skal.push_back({pref[i],{i,7}});
                PUNKT1[i]=0;
            }
            else
            {///<0,6>nieparzyste
                tab1[0][i]=1;
                tab1[1][i]=1;
                tab1[2][i]=0;
                skal.push_back({xd1.first,{i,1}});
                skal.push_back({xd2.first,{i,2}});
                skal.push_back({xd3.first,{i,3}});
                skal.push_back({xd1.second,{i,4}});
                skal.push_back({xd2.second,{i,5}});
                skal.push_back({xd3.second,{i,6}});
                skal.push_back({pref[i],{i,7}});
                PUNKT1[i]=1;
            }
        }
        else
        {///<7,13>
            pair<long long,long long>xd1,xd2,xd3;
            ///xd1,xd2
            ///xd3
            xd1={u-mod+1,u};
            xd2={0,u-mod};xd3={u+1,mod2-1};
            //cout<<xd1.first<<" "<<xd1.second<<endl;
            //cout<<xd2.first<<" "<<xd2.second<<endl;
            //cout<<xd3.first<<" "<<xd3.second<<endl<<endl;
            if(pref[i]%2==0)
            {///<7,13>parzyste
                tab1[0][i]=0;
                tab1[1][i]=1;
                tab1[2][i]=1;
                skal.push_back({xd1.first,{i,1}});
                skal.push_back({xd2.first,{i,2}});
                skal.push_back({xd3.first,{i,3}});
                skal.push_back({xd1.second,{i,4}});
                skal.push_back({xd2.second,{i,5}});
                skal.push_back({xd3.second,{i,6}});
                skal.push_back({pref[i],{i,7}});
                PUNKT1[i]=0;
            }
            else
            {///<7,13>nieparzyste
                tab1[0][i]=1;
                tab1[1][i]=0;
                tab1[2][i]=0;
                skal.push_back({xd1.first,{i,1}});
                skal.push_back({xd2.first,{i,2}});
                skal.push_back({xd3.first,{i,3}});
                skal.push_back({xd1.second,{i,4}});
                skal.push_back({xd2.second,{i,5}});
                skal.push_back({xd3.second,{i,6}});
                skal.push_back({pref[i],{i,7}});
                PUNKT1[i]=1;
            }
        }
    }
    sort(skal.begin(),skal.end());
    int h=0;
    for(int i=0;i<skal.size();i++)
    {
        int ziom=skal[i].first;
        int ind=skal[i].second.first;
        int info=skal[i].second.second;
        if(i!=0&&skal[i-1].first!=ziom)
        {
            h++;
        }
        if(info==1)
        {
            tab2[0][ind].first=h;
        }
        else if(info==2)
        {
            tab2[1][ind].first=h;
        }
        else if(info==3)
        {
            tab2[2][ind].first=h;
        }
        else if(info==4)
        {
            tab2[0][ind].second=h;
        }
        else if(info==5)
        {
            tab2[1][ind].second=h;
        }
        else if(info==6)
        {
            tab2[2][ind].second=h;
        }
        else if(info==7)
        {
            PUNKT2[ind]=h;
        }
    }
    t=TworzenieDrzewa(h+10);
    dod(0,1,TRE1);
    for(int i=1;i<=n;i++)
    {
        if(tab1[0][i]==0)
        {
            dp[i]+=Przedzial(tab2[0][i].first,tab2[0][i].second,TRE1);
        }
        else
        {
            dp[i]+=Przedzial(tab2[0][i].first,tab2[0][i].second,TRE2);
        }
        if(tab1[1][i]==0)
        {
            dp[i]+=Przedzial(tab2[1][i].first,tab2[1][i].second,TRE1);
        }
        else
        {
            dp[i]+=Przedzial(tab2[1][i].first,tab2[1][i].second,TRE2);
        }
        if(tab1[2][i]==0)
        {
            dp[i]+=Przedzial(tab2[2][i].first,tab2[2][i].second,TRE1);
        }
        else
        {
            dp[i]+=Przedzial(tab2[2][i].first,tab2[2][i].second,TRE2);
        }
        dp[i]%=mod;
        if(PUNKT1[i]==0)
        {
            dod(PUNKT2[i],dp[i],TRE1);
        }
        else
        {
            dod(PUNKT2[i],dp[i],TRE2);
        }
    }
    cout<<dp[n]<<endl;
    return 0;
}