#include <bits/stdc++.h>
using namespace std;
const long long INF = 2147483647;
long long digitsum(long long num) {
  long long sum = 0;
  while (num != 0) {
    sum += (num % 10);
    num /= 10;
  }
  return sum;
}
bool isPrime[100005 + 1];
void sieve(int n) {
  for (int i = 0; i <= n; ++i) {
    isPrime[i] = true;
  }
  isPrime[0] = false;
  isPrime[1] = false;
  for (int i = 2; i * i <= n; ++i) {
    if (isPrime[i] == true) {
      for (int j = i * i; j <= n; j += i) isPrime[j] = false;
    }
  }
}
vector<vector<long long> > matmul(vector<vector<long long> > &a,
                                  vector<vector<long long> > &b) {
  int i, j, k, n = a.size();
  vector<vector<long long> > ans(n, vector<long long>(n));
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++) {
      long long temp = 0;
      for (k = 0; k < n; k++) temp = (temp + a[i][k] * b[k][j]) % 1000000007;
      ans[i][j] = temp;
    }
  return ans;
}
vector<vector<long long> > matpwr(vector<vector<long long> > &a, long long p) {
  int n = a.size();
  vector<vector<long long> > ans(n, vector<long long>(n));
  for (int i = 0; i < n; i++) ans[i][i] = 1;
  while (p) {
    if (p & 1) ans = matmul(ans, a);
    a = matmul(a, a);
    p >>= 1;
  }
  return ans;
}
long long pwr(long long base, long long p, long long mod = 1000000007) {
  long long ans = 1;
  while (p) {
    if (p & 1) ans = (ans * base) % mod;
    base = (base * base) % mod;
    p /= 2;
  }
  return ans;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
const int N = 5e5 + 5;
int n;
long long ans = 0, fir = 0, sec = 0, sum = 0;
long long a[N], s[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  s[0] = 0;
  for (int i = (1); i <= (n); ++i) {
    cin >> a[i];
    sum += a[i];
    s[i] = sum;
  }
  if (n < 3 || sum % 3) {
    cout << ans;
    return 0;
  }
  long long u = sum / 3, v = 2 * sum / 3;
  for (int i = (1); i <= (n); ++i) {
    if (s[i] == u)
      fir++;
    else if (s[i] == v)
      sec++;
  }
  if (!sum) {
    fir--;
    cout << fir * (fir - 1) / 2;
    return 0;
  }
  for (int i = (1); i <= (n); ++i) {
    if (s[i] == u)
      ans += sec;
    else if (s[i] == v)
      sec--;
  }
  cout << ans;
  return 0;
}
