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

#define FOR(i, n) for (int i = 0; i < int(n); ++i)
#define FO(i, a, b) for (int i = (a); i < int(b); ++i)
#define OF(i, a, b) for (int i = (b)-1; i >= int(a); --i)

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((b) < (a) ? (a) : (b))

#define REMIN(a, b) ((a) = min(a, b))
#define REMAX(a, b) ((a) = max(a, b))

#define ALL(c) (c).begin(), (c).end()

#define SQR(x) ((x) * (x))

//

const int N = 1 << 19; // 300'000 + 9;
const int mod = 1e9 + 7;

map<int, int> remap;

int shift = 0;

struct T {
  int num[2] = {0, 0};
  bool swap = false;
};

T tree[2 * N];

int getParity(int v) {
  int parity = 0;
  while (v) {
    parity ^= tree[v].swap;
    v /= 2;
  }
  return parity;
}

// void printTree() {
//   cerr << "TREE" << endl;
//   FOR(parity, 2) {
//     cerr << "parity " << parity << endl;
//     FOR(i, N) {
//       int parityx = getParity(N + i) ^ parity;
//       if (tree[N + i].num[parityx])
//         cerr << i << "->" << tree[N + i].num[parityx] << " ";
//     }
//     cerr << endl;
//   }
// }

void update(int v) {
  assert(v < N);
  FOR(parity, 2) {
    tree[v].num[parity] = tree[v * 2].num[parity ^ tree[v * 2].swap] +
                          tree[v * 2 + 1].num[parity ^ tree[v * 2 + 1].swap];

    tree[v].num[parity] %= mod;
  }
}

void updateToRoot(int v) {
  while (v) {
    update(v);
    v /= 2;
  }
}

void add(int x, int parity, int val) {
  x += N;

  parity ^= getParity(x);

  tree[x].num[parity] += val;
  tree[x].num[parity] %= mod;

  updateToRoot(x / 2);
}

void treeSwap(int fr, int to, int a, int b, int v) {
  if (fr >= to)
    return;

  int mid = (a + b) / 2;

  if (fr <= a && b <= to) {
    tree[v].swap ^= 1;
    return;
  }

  if (fr < mid)
    treeSwap(fr, to, a, mid, v * 2);

  if (to > mid)
    treeSwap(fr, to, mid, b, v * 2 + 1);

  update(v);
}

void swapParity(int fr, int to) {
  // cerr << "swapParity fr:" << fr << " to:" << to << endl;

  auto frBound = remap.lower_bound(fr);
  fr = N;
  if (frBound != remap.end())
    fr = frBound->second;

  auto toBound = remap.lower_bound(to);
  to = N;
  if (toBound != remap.end())
    to = toBound->second;

  // cerr << "  compressed fr:" << fr << " to:" << to << endl;

  treeSwap(fr, to, 0, N, 1);
}

int main() {
  ios_base::sync_with_stdio(0);

  int n;
  cin >> n;

  vector<int> input(n);

  FOR(i, n) cin >> input[i];

  set<int> all;

  shift = 0;
  FOR(i, n) {
    shift += input[i];
    shift %= mod;

    int raw = (mod + input[i] - shift) % mod;
    all.insert(raw);
  }

  {
    int curr = 0;
    for (auto &e : all) {
      remap[e] = curr++;
    }
  }

  // for (auto &[k, v] : remap) {
  //   cerr << "  " << k << "->" << v << endl;
  // }

  shift = 0;
  FOR(i, n) {
    // cerr << "\n i == " << i << endl;

    int numPrev = i == 0 ? 1 : tree[1].num[tree[1].swap];

    int fr = (-shift + 2 * mod - input[i]) % mod;
    int to = mod - shift;

    // printTree();
    // cerr << "BEFORE good:" << tree[1].num[tree[1].swap]
    //      << " bad:" << tree[1].num[!tree[1].swap] << endl;

    if (fr < to)
      swapParity(fr, to);
    else {
      swapParity(0, to);
      swapParity(fr, mod);
    }

    if (input[i] % 2 == 1) {
      swapParity(0, mod);
    }

    // printTree();
    // cerr << "AFTER good:" << tree[1].num[tree[1].swap]
    //      << " bad:" << tree[1].num[!tree[1].swap] << endl;

    shift += input[i];
    shift %= mod;

    int raw = (mod + input[i] - shift) % mod;

    assert(remap.find(raw) != remap.end());

    add(remap[raw], input[i] % 2, numPrev);

    // cerr << "add raw:" << raw << " (compressed:" << remap[raw] << ")"
    //      << " parity:" << input[i] % 2 << " numPrev:" << numPrev << endl;

    // cerr << "RES good:" << tree[1].num[tree[1].swap]
    //      << " bad:" << tree[1].num[!tree[1].swap] << endl;
  }

  cout << tree[1].num[tree[1].swap] << endl;

  return 0;
}