#include <bits/stdc++.h>
using namespace std;
template <class e, class t, int N>
std::basic_istream<e, t>& operator>>(std::basic_istream<e, t>& in,
                                     const e (&literal)[N]) {
  std::array<e, N - 1> buffer;
  in >> buffer[0];
  if (N > 2) in.read(&buffer[1], N - 2);
  if (strncmp(&buffer[0], literal, N - 1))
    in.setstate(in.rdstate() | std::ios::badbit);
  return in;
}
template <class e, class t>
std::basic_istream<e, t>& operator>>(std::basic_istream<e, t>& in,
                                     const e& literal) {
  e buffer;
  in >> buffer;
  if (buffer != literal) in.setstate(in.rdstate() | std::ios::badbit);
  return in;
}
template <class e, class t, int N>
std::basic_istream<e, t>& operator>>(std::basic_istream<e, t>& in,
                                     e (&carray)[N]) {
  return std::operator>>(in, carray);
}
template <class t>
ostream& operator<<(ostream& out, vector<t>& a) {
  for (auto x : a) {
    out << x << " ";
  }
  return out;
}
template <class t1, class t2>
ostream& operator<<(ostream& out, const pair<t1, t2>& a) {
  out << a.first << " " << a.second;
  return out;
}
template <class t1, class t2>
pair<t1, t2> operator+(const pair<t2, t1>& a, const pair<t1, t2>& b) {
  return {a.first + b.first, a.second + b.second};
}
template <class t1, class t2>
pair<t1, t2> operator-(const pair<t2, t1>& a, const pair<t1, t2>& b) {
  return {a.first - b.first, a.second - b.second};
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }
long long fpow(long long b, long long e, long long m = (long long)(1e9 + 7)) {
  if (!e) return 1;
  long long q = fpow(b, e / 2, m);
  q = (q * q) % m;
  return e & 1 ? (b * q) % m : q;
}
int32_t main(int32_t argc, char const* argv[]) {
  0;
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n;
  cin >> n;
  long long a[n];
  for (long long i = 0; i < (long long)n; i++) cin >> a[i];
  long long p[n];
  p[0] = a[0];
  for (long long i = 1; i < (long long)n; i++) p[i] = a[i] + p[i - 1];
  if (p[n - 1] % 3 != 0) {
    cout << 0 << "\n";
    return 0;
  }
  if (p[n - 1] == 0) {
    long long c = 0;
    for (long long i = 0; i < (long long)n; i++) c += (p[i] == 0);
    c--;
    cout << (c * (c - 1)) / 2;
    return 0;
  }
  long long second = p[n - 1] / 3;
  long long c = 0, k = 0;
  for (long long i = 0; i < (long long)n; i++) {
    if (p[i] == second) c++;
    if (p[i] == 2 * second) k += c;
  }
  cout << k << "\n";
  0;
  return 0;
}
