#include <bits/stdc++.h>
using namespace std;
inline int lowbit(int x) { return x & -x; }
inline int ABS(int x) { return x < 0 ? -x : x; }
inline long long ABS(long long x) { return x < 0 ? -x : x; }
inline double ABS(double x) { return x < 0 ? -x : x; }
inline void MAX(int &a, int b) {
  if (b > a) a = b;
}
inline void MAX(long long &a, long long b) {
  if (b > a) a = b;
}
inline void MIN(int &a, int b) {
  if (b < a) a = b;
}
inline void MIN(long long &a, long long b) {
  if (b < a) a = b;
}
int fast(int x, int v) {
  int ans = 1;
  for (; v; v >>= 1, x = (long long)x * x % 10000007)
    if (v & 1) ans = (long long)ans * x % 10000007;
  return ans;
}
long long fast(long long x, long long v) {
  long long ans = 1;
  for (; v; v >>= 1, x = x * x % 10000007)
    if (v & 1) ans = ans * x % 10000007;
  return ans;
}
int n;
long long a[500005], sum, ans, p;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%I64d", a + i);
    a[i] += a[i - 1];
  }
  if (n < 3 || a[n] % 3) {
    puts("0");
    return 0;
  }
  sum = a[n] / 3;
  for (int i = 1; i <= n - 1; ++i) {
    if (a[i] == sum * 2) ans += p;
    if (a[i] == sum) ++p;
  }
  printf("%I64d\n", ans);
}
