#include <bits/stdc++.h>
#pragma GCC Optimize("Ofast");
using namespace std;
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cerr.tie(NULL);
  cout.tie(NULL);
  ;
  int tests = 1;
  while (tests--) {
    int N;
    cin >> N;
    vector<int> arr(N);
    for (auto &it : arr) cin >> it;
    long long sum = 0;
    for (int i = 0; i < N; ++i) sum += arr[i];
    if (sum % 3) {
      cout << 0 << "\n";
    } else {
      sum /= 3;
      long long suffixSum = 0;
      vector<int> count(N, 0);
      for (int i = N - 1; i >= 0; --i) {
        suffixSum += arr[i];
        if (suffixSum == sum) count[i] = 1;
      }
      for (int i = N - 2; i >= 0; --i) {
        count[i] += count[i + 1];
      }
      suffixSum = 0;
      long long ans = 0;
      for (int i = 0; i < N - 2; ++i) {
        suffixSum += arr[i];
        if (suffixSum == sum) ans += count[i + 2];
      }
      cout << ans << "\n";
    }
  }
  return 0;
}
