#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
void __print(int x) { cerr << x; }
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) {
  int 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...);
}
const int mx = 5e5 + 5;
long long ara[mx], pre[mx];
map<long long, vector<int>> indx;
void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> ara[i];
    pre[i] = pre[i - 1] + ara[i];
    indx[pre[i]].push_back(i);
  }
  if (pre[n] % 3) {
    cout << 0;
    return;
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (pre[i] == pre[n] / 3) {
      long long a = pre[i] * 2;
      long long temp =
          upper_bound(indx[a].begin(), indx[a].end(), i) - indx[a].begin();
      temp = indx[a].size() - temp;
      if (pre[n] == a) temp--;
      temp = max(temp, 0LL);
      ans += temp;
    }
  }
  cout << ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
