#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
long long pre[N];
int a[N], b[N];
map<int, vector<int> > mp;
int d, n;
long long s, ans;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> d;
    s += d;
    pre[i] = s;
  }
  if (s % 3 || n < 3) return cout << 0, 0;
  long long z = pre[n];
  long long y = pre[n] / 3 * 2;
  long long x = pre[n] / 3;
  for (int i = 1; i <= n; i++) a[i] += (x == pre[i]) + (a[i - 1]);
  for (int i = n; i >= 1; i--) b[i] += (z == pre[i]) + (b[i + 1]);
  for (int i = 2; i < n; i++)
    if (pre[i] == y) ans += a[i - 1];
  cout << ans << "\n";
}
