import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()) {
int n = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
// 1. 预处理前缀和与正数计数
long[] prefixSum = new long[n + 1];
int[] posPrefix = new int[n + 1];
for (int i = 1; i <= n; i++) {
prefixSum[i] = prefixSum[i - 1] + a[i - 1];
posPrefix[i] = posPrefix[i - 1] + (a[i - 1] > 0 ? 1 : 0);
}
long totalSum = prefixSum[n];
if (totalSum % 3 != 0) {
System.out.println(0);
scanner.close();
return;
}
long target = totalSum / 3;
long ans = 0;
//使用 Map存储前缀和出现的次数
// Key: 正数前缀和的值 (pos_prefix[j])
// Value: 该值出现的次数 (有多少个合法的 j)
Map<Integer, Integer> countMap = new HashMap<>();
// 3. 从右向左扫描
for (int i = n - 2; i >= 1; i--) {
// --- A. 将 i+1 加入 Map ---
int j = i + 1;
if (j <= n - 1) {
boolean isValidJSum = (prefixSum[j] == 2 * target);
boolean hasPositivePart3 = (posPrefix[n] - posPrefix[j] > 0);
if (isValidJSum && hasPositivePart3) {
int key = posPrefix[j];
// 计数加 1
countMap.put(key, countMap.getOrDefault(key, 0) + 1);
}
}
// --- B. 查询 Map ---
if (prefixSum[i] == target && posPrefix[i] > 0) {
int currentPosCount = posPrefix[i];
// 核心逻辑:统计 Map 中所有 Key > currentPosCount 的 Value 之和
int countValidJ = 0;
for (Map.Entry<Integer, Integer> entry : countMap.entrySet()) {
int mapKey = entry.getKey();
int mapValue = entry.getValue();
if (mapKey > currentPosCount) {
countValidJ += mapValue;
}
}
ans += countValidJ;
}
}
System.out.println(ans);
}
scanner.close();
}
}