#include <bits/stdc++.h>
using namespace std;
void _fast() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
int ar[500010];
int dp1[500010], dp2[500010];
int main() {
  _fast();
  int n;
  cin >> n;
  long long int sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
    sum += ar[i];
  }
  if (sum % 3 != 0 || n <= 2) {
    cout << 0;
    return 0;
  }
  long long int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if ((sum - cnt) == (sum / 3)) dp1[i] = 1;
    cnt += ar[i];
  }
  for (int i = n; i >= 1; i--) dp1[i] += dp1[i + 1];
  cnt = 0;
  for (int i = n; i >= 1; i--) {
    if ((sum - cnt) == (sum / 3)) dp2[i] = 1;
    cnt += ar[i];
  }
  cnt = 0;
  for (int i = 1; i < n; i++)
    if (dp2[i] != 0) {
      for (int j = i + 2; j <= n; j++)
        if (dp1[j] != 0) {
          cnt += dp1[j];
          break;
        }
    }
  cout << cnt;
}
