#include <bits/stdc++.h>
using namespace std;
long long arr[500010];
map<long long, int> res;
int main() {
  long long n;
  while (cin >> n) {
    long long t;
    long long ans = 0;
    scanf("%I64d", &arr[0]);
    for (int i = 1; i < n; ++i) {
      scanf("%I64d", &t);
      arr[i] = arr[i - 1] + t;
    }
    if (n <= 2)
      cout << "0" << endl;
    else {
      long long part = arr[n - 1] / 3;
      if (part * 3 != arr[n - 1])
        cout << "0" << endl;
      else {
        long long times = 0;
        for (int i = 0; i < n - 2; ++i) {
          if (arr[i] == part) {
            times++;
            res.insert(make_pair(times, i));
          }
        }
        t = times;
        for (int i = n - 2; i >= 1; --i) {
          if (arr[i] == 2 * part) {
            while (res[t] >= i && t >= 1) {
              t--;
            }
            if (t == 0) break;
            ans += t;
          }
        }
        cout << ans << endl;
      }
    }
  }
  return 0;
}
