#include <bits/stdc++.h>
void quit();
using namespace std;
const long double EPS = 1e-12;
double __t;
int n;
long long a[500500];
long long s[500500];
map<int, int> mapp;
int main() {
  cin >> n;
  for (int i = 0; i < (int)n; ++i) {
    cin >> a[i];
    s[i + 1] = s[i] + a[i];
    mapp[s[i + 1]]++;
  }
  long long ans = 0;
  mapp[s[n]]--;
  for (int i = 1; i < n; i++) {
    mapp[s[i]]--;
    if (s[n] != 3 * s[i]) continue;
    ans += mapp[2 * s[i]];
  }
  cout << ans;
  quit();
}
void quit() { exit(0); }
