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
#if defined(EMBE_DEBUG) && !defined(NDEBUG)
#include "embe-debug.hpp"
#else
#define LOG_INDENT(...) do {} while (false)
#define LOG(...) do {} while (false)
#define DUMP(...) do {} while (false)
#endif

#include <algorithm>
#include <cassert>
#include <iostream>
#include <iterator>
#include <ranges>
#include <numeric>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <utility>
#include <array>
#include <vector>
using namespace std;

namespace {

constexpr int q = 512;

long long solve(vector<int> data)
{
  // data = vector(500, -1);
  // for (int i = 0; i < 500; i += 2) data[i] = 1;
  vector<int> sums;
  sums.reserve(sums.size() + 1);
  sums.emplace_back(0);
  partial_sum(data.begin(), data.end(), back_inserter(sums));
  DUMP(sums);
  // sums = vector(500, 0);
  int n = sums.size();
  unordered_set<int> sums3;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        sums3.emplace(sums[i] + sums[j] + sums[k]);
      }
    }
  }
  unordered_map<int, vector<pair<int, int>>> sums2;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      sums2[sums[i] + sums[j]].emplace_back(i, j);
    }
  }
  unordered_map<int, vector<tuple<short, short, short>>> buf;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        int cur = sums[i] + sums[j] + sums[k];
        buf[cur].emplace_back(i, j, k);
      }
    }
  }
  // unordered_map<int, int> tmp;
  // for (int c = 0; c < n; ++c) {
  //   for (int a = 0; a < n; ++a) {
  //     for (int b = 0; b < a; ++b) {
  //       tmp[sums[a] - sums[b] + sums[c]]++;
  //     }
  //   }
  // }
  // long long res = 0;
  // for (int c = 0; c < n; ++c) {
  //   for (int a = 0; a < n; ++a) {
  //     for (int b = 0; b < a; ++b) {
  //       tmp[sums[a] - sums[b] + sums[c]]--;
  //     }
  //   }
  //   for (int a = 0; a < n; ++a) {
  //     for (int b = 0; b < a; ++b) {
  //       res += tmp[-(sums[a] - sums[b] + sums[c])];
  //     }
  //   }
  // }
  // // - (a, b) (a, b) (e, f)
  // // - (a, b) (c, d) (c, d)
  // // + (a, b) (a, b) (a, b)
  // for (int a = 0; a < n; ++a) {
  //   for (int b = 0; b < a; ++b) {
  //     int x = sums[a] - sums[b];
  //     if (x == 0) --res;
  //   }
  // }
  // // res /= 6;
  // // / 6
  long long res2 = 0;
  for (auto const& [sum, triples]: buf) {
    for (auto const& [i, j, k]: triples) {
      res2 += ranges::count_if(triples, 
          [&](auto const& t) {
          auto const& [ii, jj, kk] = t;
          if (!(i < ii && j < jj && k < kk)) return false;
          // if (tuple{i, ii} == tuple{j, jj} && tuple{j, jj} == tuple{k, kk}) return false;
          if (tuple{i, ii} >= tuple{j, jj}) return false;
          if (tuple{j, jj} >= tuple{k, kk}) return false;
          // if (!(i < ii && j < jj && k < kk)) return false;
          // if (i == j && ii >= jj) return false;
          // if (j == k && jj >= kk) return false;
          return true;
          }
          );
  }
}
clog << res2 << endl;
return res2;
}

}

int main()
{
  iostream::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;

  vector<int> data(n);
  for (auto& x: data) {
    cin >> x;
  }

  cout << solve(move(data)) << endl;

  return 0;
}