#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
long long dx[] = {1, 0, -1, 0};
long long dy[] = {0, 1, 0, -1};
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  long long f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
pair<long long, long long> subArraylen(long long arr[], long long n,
                                       long long K) {
  unordered_map<long long, long long> mp;
  mp[arr[0]] = 0;
  for (long long i = 1; i < n; i++) {
    arr[i] = arr[i] + arr[i - 1];
    mp[arr[i]] = i;
  }
  long long len = INT_MAX;
  for (long long i = 0; i < n; i++) {
    if (arr[i] < K)
      continue;
    else {
      long long x = arr[i] - K;
      if (x == 0) len = min(len, i);
      if (mp.find(x) == mp.end())
        continue;
      else {
        len = min(len, i - mp[x]);
      }
    }
  }
}
long long solve() {
  long long n;
  cin >> n;
  long long a[n];
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  if (sum % 3 != 0 || n < 3) return 0;
  sum /= 3;
  long long lsum = sum, rsum = lsum * 2;
  sum = 0;
  long long cnt = 0, ans = 0;
  for (long long i = 0; i < n - 1; i++) {
    sum += a[i];
    if (sum == rsum) ans += cnt;
    if (sum == lsum) cnt++;
  }
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << solve();
  return 0;
}
