#include <bits/stdc++.h>
using namespace std;
const int N = 5 * 1E5 + 5;
int Array[N], Mark[N], cntOfTwo[N];
int main() {
  int n;
  long long sum = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &Array[i]);
    sum += Array[i];
  }
  if (sum % 3 != 0) {
    cout << 0;
    return 0;
  }
  sum /= 3;
  long long sum_ = 0, cntOfZero = 0;
  for (int i = 1; i <= n; i++) {
    sum_ += Array[i];
    if (sum_ == sum) Mark[i] = 1;
    if (sum_ == sum * 2) Mark[i] = 2;
    if (sum_ == 0 && sum == 0) cntOfZero++;
  }
  if (sum == 0) {
    cntOfZero--;
    cout << cntOfZero * (cntOfZero - 1) / 2;
    return 0;
  }
  for (int i = n - 1; i > 0; i--) {
    if (Mark[i] == 2) cntOfTwo[i] = 1;
    cntOfTwo[i] += cntOfTwo[i + 1];
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (Mark[i] == 1) ans += cntOfTwo[i + 1];
  }
  cout << ans;
}
