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

using namespace std;

template <typename T>
class FenwickTree
{
  vector<T> values;
  typedef function<T(T, T)> Add;

  Add add_f;

public:
  FenwickTree(size_t size, Add add) : values(size + 1, T{}), add_f(add) {}

  int LSB(int index) const { return index & (-index); }

  void add(int index, T value)
  {
    int position = index + 1;
    while (position < static_cast<int>(values.size()))
    {
      values[position] = add_f(values[position], value);
      position += LSB(position);
    }
  }

  T sum(int index) const
  {
    T result = T{};
    int position = index + 1;
    while (position > 0)
    {
      result = add_f(result, values[position]);
      position -= LSB(position);
    }
    return result;
  }

  T sum(int first, int last) const { return sum(last) - sum(first - 1); }

  size_t size() const { return values.size() - 1; }
};

struct mod_t
{
  static const int P = 1000000007;
  int value;
  mod_t() : value(0) {}
  mod_t(int value) : value((P + value % P) % P) {}
};

istream& operator>>(istream& stream, mod_t& value)
{
  stream >> value.value;
  return stream;
}

ostream& operator<<(ostream& stream, const mod_t& value)
{
  stream << value.value;
  return stream;
}

struct TestCase
{
  size_t size;
  vector<mod_t> values;
};

TestCase read_test_case();
void solve_test_case(const TestCase&);

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve_test_case(read_test_case());
}

TestCase read_test_case()
{
  TestCase tc;
  cin >> tc.size;
  tc.values.resize(tc.size);
  for (auto& value : tc.values) cin >> value;
  return tc;
}

mod_t operator+(const mod_t& lhs, const mod_t& rhs)
{
  return (lhs.value + rhs.value);
}

mod_t operator-(const mod_t& lhs, const mod_t& rhs)
{
  return (lhs.value - rhs.value);
}

struct Prefix
{
  mod_t sum;
  size_t size;
  size_t index_in_sorted;
};

void sort_prefixes(vector<Prefix>& prefixes)
{
  sort(prefixes.begin(), prefixes.end(), [](const auto& lhs, const auto& rhs) {
    if (lhs.sum.value == rhs.sum.value)
      return lhs.size < rhs.size;
    else
      return lhs.sum.value < rhs.sum.value;
  });
}

struct PrefixData
{
  mod_t sum;
  const vector<Prefix>& even_prefixes;
  const vector<Prefix>& odd_prefixes;
  const FenwickTree<mod_t>& even;
  const FenwickTree<mod_t>& odd;
};

auto prefix_upper_bound(const vector<Prefix>& prefixes, Prefix find)
{
  return upper_bound(
      prefixes.begin(), prefixes.end(), find,
      [](const Prefix& lhs, const Prefix& rhs) {
        return lhs.sum.value < rhs.sum.value;
      });
}

mod_t solve_prefix(PrefixData data)
{
  auto even_it = prefix_upper_bound(data.even_prefixes, {data.sum});
  size_t even_index = even_it - data.even_prefixes.begin() - 1;
  mod_t even_sum = data.even.sum(even_index);

  auto odd_it = prefix_upper_bound(data.odd_prefixes, {data.sum});
  size_t odd_index = odd_it - data.odd_prefixes.begin() - 1;
  mod_t odd_sum = data.odd.sum(odd_index);
  mod_t total_odd_sum = data.odd.sum(data.odd.size() - 1);

  cerr << "Solving prefix: " << even_sum << " " << odd_sum << " "
       << total_odd_sum << "\n";
  return total_odd_sum - odd_sum + even_sum;
}

void solve_test_case(const TestCase& tc)
{
  vector<Prefix> prefix_sums(tc.values.size() + 1);
  prefix_sums[0] = {0, 0};
  for (size_t i = 0; i < tc.size; i++)
  {
    prefix_sums[i + 1].sum = prefix_sums[i].sum + tc.values[i];
    prefix_sums[i + 1].size = i + 1;
  }

  vector<Prefix> even_prefixes;
  vector<Prefix> odd_prefixes;
  for (size_t i = 0; i < prefix_sums.size(); i++)
  {
    if (prefix_sums[i].sum.value % 2 == 0)
      even_prefixes.push_back(prefix_sums[i]);
    else
      odd_prefixes.push_back(prefix_sums[i]);
  }

  sort_prefixes(even_prefixes);
  sort_prefixes(odd_prefixes);
  for (size_t i = 0; i < even_prefixes.size(); i++)
    prefix_sums[even_prefixes[i].size].index_in_sorted = i;
  for (size_t i = 0; i < odd_prefixes.size(); i++)
    prefix_sums[odd_prefixes[i].size].index_in_sorted = i;

  FenwickTree<mod_t> even(even_prefixes.size(), std::plus<mod_t>());
  FenwickTree<mod_t> odd(odd_prefixes.size(), std::plus<mod_t>());

  vector<mod_t> division_count(tc.values.size() + 1, 0);
  division_count[0] = 1;
  even.add(0, 1);

  for (size_t i = 1; i < division_count.size(); i++)
  {
    auto prefix = prefix_sums[i];
    mod_t current_sum = prefix.sum;

    if (current_sum.value % 2 == 0)
      division_count[i] =
          solve_prefix({current_sum, even_prefixes, odd_prefixes, even, odd});
    else
      division_count[i] =
          solve_prefix({current_sum, odd_prefixes, even_prefixes, odd, even});

    if (current_sum.value % 2 == 0)
      even.add(prefix.index_in_sorted, division_count[i]);
    else
      odd.add(prefix.index_in_sorted, division_count[i]);
  }

  cout << division_count[tc.size] << "\n";
}