You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same. 

More formally, you need to find the number of such pairs of indices i, j (2 ≤ i ≤ j ≤ n - 1), that $\sum_{k = 1}^{i - 1} a_{k} = \sum_{k = i}^{j} a_{k} = \sum_{k = j + 1}^{n} a_{k}$.


-----Input-----

The first line contains integer n (1 ≤ n ≤ 5·10^5), showing how many numbers are in the array. The second line contains n integers a[1], a[2], ..., a[n] (|a[i]| ≤  10^9) — the elements of array a.


-----Output-----

Print a single integer — the number of ways to split the array into three parts with the same sum.


-----Examples-----
Input
5
1 2 3 0 3

Output
2

Input
4
0 1 -1 0

Output
1

Input
2
4 1

Output
0

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Sorting

Amortized analysis

Complete search

Dynamic programming

Data structures


Solutions:
n = int(input())
l = list(map(int, input().split()))
s = 0
s3 = 0
s32 = 0
su = sum(l)
if su % 3 != 0:
	print(0)
	exit(0)
for i in range(0, n - 1):
	s += l[i]
	if s == 2 * su / 3:
		s32 += s3
	if s == su / 3:
		s3 += 1
print(s32)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
if s % 3 != 0:
	print('0')
	quit()
s = s // 3
c = 0
f = [0] * len(a)
b = [0] * len(a)
for i in range(len(a)):
	c += a[i]
	if c == s:
		f[i] = 1
	if c == 2 * s:
		if i < len(a) - 1:
			b[i + 1] = 1
for i in range(len(a) - 2, -1, -1):
	b[i] += b[i + 1]
cnt = 0
c = 0
for i in range(0, len(f) - 2):
	if f[i] == 1:
		cnt += b[i + 2]
print(cnt)


def f(A):
	sum_ = sum(A)
	if sum_ % 3 != 0:
		return 0
	sum3 = sum(A) // 3
	C = [0] * len(A)
	S = [-1] * len(A)
	prefix = 0
	for i in range(len(A) - 1, -1, -1):
		prefix += A[i]
		if prefix == sum3:
			C[i] = 1
		S[i] = C[i]
		if i < len(A) - 1:
			S[i] += S[i + 1]
	count = 0
	prefix = 0
	for i in range(len(A) - 2):
		prefix += A[i]
		if prefix == sum3:
			count += S[i + 2]
	return count
assert f([1, 1, 1]) == 1
assert f([2, 1, 1, 2]) == 1
assert f([1, 1, 2, 1, 1]) == 1
assert f([1, 2, 3, 0, 3]) == 2
assert f([0, 1, -1, 0]) == 1
assert f([4, 1]) == 0
n = int(input())
A = list(map(int, input().split()))
ans = f(A)
print(ans)


n = int(input())
line = [int(it) for it in str(input()).split()]
sv = sum(line)
if n < 3 or sv % 3 > 0:
	print(0)
else:
	refer = list()
	v = 0
	for it in line:
		v += it
		refer.append(v)
	sv0 = sv // 3
	sv1 = sv0 * 2
	pine = list()
	result = 0
	for i in range(n - 1):
		if refer[i] == sv1:
			result += len(pine)
		if refer[i] == sv0:
			pine.append(i)
	print(result)


import itertools
n = int(input())
a = list(map(int, input().split()))
v = sum(a)
if v % 3:
	print(0)
	exit(0)
s = 0
sums = [0] * n
suffix_count = 0
for i in reversed(range(0, n)):
	s += a[i]
	suffix_count += 1 if s == v // 3 else 0
	sums[i] = suffix_count
count = 0
s = a[0]
for i in range(1, n - 1):
	if s == v // 3:
		count += sums[i + 1]
	s += a[i]
print(count)


n = int(input())
lst = list(map(int, input().split()))
c = t = 0
s = sum(lst)
if s % 3 == 0:
	sone = s // 3
	stwo = s - sone
	s = 0
	for x in lst[:n - 1]:
		s += x
		if s == stwo:
			c += t
		if s == sone:
			t += 1
print(c)


n = int(input())
num = input()
list = [int(x) for x in num.split()]
tol = sum(list)
s = 0
t = 0
w = 0
if tol % 3 != 0:
	print(0)
else:
	c = int(tol / 3)
	for i in range(n - 1):
		s += list[i]
		if s == 2 * c and 0 < i:
			w += t
		if s == c and i < n - 2:
			t += 1
	print(w)


a = int(input())
list = list(map(int, input().split()))
s = sum(list)
if s % 3 != 0 or a < 3:
	print(0)
else:
	num = 0
	answer = 0
	ss = 0
	for i in range(a - 1):
		ss += list[i]
		if ss == s * 2 / 3:
			answer += num
		if ss == s / 3:
			num += 1
	print(answer)


import math
import sys
from collections import defaultdict
from collections import Counter
from collections import deque
import bisect
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]
N = int(input())
A = alele()
S = sum(A)
if S % 3 != 0 or N < 3:
	print(0)
else:
	x = S // 3
	pre = [0]
	t = 0
	for i in A:
		t += i
		pre.append(t)
	count = 0
	u = 0
	B = []
	c = 0
	for i in range(N - 1, -1, -1):
		u += A[i]
		if u == x:
			c += 1
		B.append(c)
	B = B[::-1] + [0]
	for i in range(N - 2):
		a = pre[i + 1]
		b = S - a
		if a == x and b == 2 * x:
			count += B[i + 2]
	print(count)


n = int(input())
a = [int(x) for x in input().split(' ')]
s = sum(a)
cnt = 0
if s % 3 == 0:
	s = int(s / 3)
	w = 0
	ot = 0
	for x in a[:-1]:
		w += x
		if w == 2 * s:
			cnt += ot
		if w == s:
			ot += 1
print(cnt)


n = int(input())
line = input().split()
accumulated = [0] * n
curr_sum = 0
index = 0
for num in line:
	curr_sum += int(num)
	accumulated[index] = curr_sum
	index += 1
total = accumulated[n - 1]
third = total // 3
if total % 3 == 0:
	i_possibilities = 0
	j_possibilities = 0
	total_decrease = 0
	curr_decrease = 0
	for i in range(0, n - 1):
		num = accumulated[i]
		if i != 0 and num == 2 * third:
			j_possibilities += 1
			curr_decrease += 1
		if i != n - 2 and num == third:
			i_possibilities += 1
			total_decrease += curr_decrease
	print(i_possibilities * j_possibilities - total_decrease)
else:
	print(0)


n = int(input())
a = list(map(int, input().split()))
s0 = sum(a)
if s0 % 3 != 0:
	print(0)
else:
	s = s0 // 3
	d = [0 for i in range(n)]
	temp = a[-1]
	d[-1] = 1 if temp == s else 0
	for i in range(2, n + 1):
		temp += a[-i]
		d[-i] = d[-i + 1] + (1 if temp == s else 0)
	k = 0
	temp = 0
	for i in range(n - 2):
		temp += a[i]
		if temp == s:
			k += d[i + 2]
	print(k)


I = lambda : list(map(int, input().split()))
(n,) = I()
l = I()
f = sum(l) // 3
if sum(l) % 3:
	print(0)
else:
	st = j = -1
	s = i = 0
	ct = an = 0
	while i < n - 1:
		s += l[i]
		if s == 2 * f:
			j = i
			an += ct
		if s == f:
			st = i
			ct += 1
		i += 1
	if st == -1 or j == -1:
		print(0)
	else:
		print(an)


n = int(input())
arr = list(map(int, input().split(' ')))
_sum = sum(arr)
tmp = [0] * n
count = 0
if _sum % 3:
	count = 0
else:
	prefix_sum = [0] * n
	prefix_sum[0] = arr[0]
	for i in range(1, n):
		prefix_sum[i] = prefix_sum[i - 1] + arr[i]
	k = 0
	trisum = _sum // 3
	for i in range(n - 2, 0, -1):
		if prefix_sum[i] == trisum * 2:
			k += 1
		tmp[i] = k
	for j in range(n - 3, -1, -1):
		if prefix_sum[j] == trisum:
			count += tmp[j + 1]
print(count)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
k = 0
if n > 3 and s % 3 == 0:
	s //= 3
	c = [0] * n
	t = 0
	for i in range(n):
		t += a[n - i - 1]
		if t == s:
			c[n - i - 1] = 1
	for i in range(n - 1):
		c[n - i - 2] += c[n - i - 1]
	t = 0
	for i in range(n - 2):
		t += a[i]
		if t == s:
			k += c[i + 2]
print(k)


test = int(input())
li = list(map(int, input().split()))
summ = sum(li)
if summ % 3 != 0:
	print(0)
else:
	x = 0
	s1 = 0
	s2 = 0
	z = []
	for i in li:
		x += i
		if x == summ / 3:
			s1 += 1
			z.append((1, s2))
		elif x == summ / 3 * 2 and s1 > 0:
			s2 += 1
			z.append((0, 0))
		else:
			z.append((0, 0))
	su = 0
	if summ == 0:
		print(int((s1 - 1) * (s1 - 2) / 2))
	else:
		for j in z:
			if j[0]:
				su += j[0] * (s2 - j[1])
		print(su)


n = int(input())
numbers = [int(x) for x in input().split(' ')]
cnt = [0] * n
ss = 0
if sum(numbers) % 3 != 0:
	print(0)
else:
	s = sum(numbers) / 3
	for i in range(n - 1, -1, -1):
		ss += numbers[i]
		if s == ss:
			cnt[i] = 1
	for i in range(n - 2, -1, -1):
		cnt[i] += cnt[i + 1]
	ans = 0
	ss = 0
	for i in range(0, n - 2, 1):
		ss += numbers[i]
		if ss == s:
			ans += cnt[i + 2]
	print(ans)


def calc():
	a = int(input())
	lista = list(map(int, input().split()))
	soma = sum(lista)
	resus = [0 for i in range(a + 10)]
	if soma % 3 == 0 and a >= 3:
		soma /= 3
		ss = 0
		for i in range(a - 1, 0, -1):
			ss += lista[i]
			if ss == soma:
				resus[i] = 1
		for i in range(a - 2, 0, -1):
			resus[i] += resus[i + 1]
		cont = 0
		ss = 0
		for i in range(a):
			ss += lista[i]
			if ss == soma:
				cont += resus[i + 2]
		return cont
	return 0
print(calc())


def numberofways(arr, n):
	s = sum(arr)
	if s % 3 != 0:
		return 0
	s /= 3
	answer = 0
	prefixsum = 0
	count_s = 0
	for i in range(n - 1):
		prefixsum += arr[i]
		if prefixsum == 2 * s:
			answer += count_s
		if prefixsum == s:
			count_s += 1
	return answer
n = int(input())
arr = list(map(int, input().split()))
print(numberofways(arr, n), end='')


n = int(input())
a = input()
list_a = list(map(int, a.split()))
array = []
for i in range(len(list_a)):
	if i == 0:
		array.append(list_a[i])
	else:
		array.append(array[i - 1] + list_a[i])
x = array[-1]

def useful_function(x, array):
	if x % 3 != 0:
		return 0
	elif x != 0:
		marks = 0
		a = int(x / 3)
		b = int(2 * x / 3)
		c = 0
		for i in range(len(array)):
			if array[i] == a:
				c += 1
			if array[i] == b:
				marks += c
		return marks
	else:
		m = array.count(0)
		if m == len(array):
			return int((m - 1) * (m - 2) / 2)
		m -= 1
		if m % 2 == 0:
			return int(m / 2) ** 2
		else:
			a = int(m / 2)
			return a * (a + 1)
print(useful_function(x, array))


n = int(input())
l = list(map(int, input().split()))
count = [0] * n
s = sum(l)
if s % 3 != 0:
	print(0)
else:
	s //= 3
	ss = 0
	for i in range(n - 1, -1, -1):
		ss += l[i]
		if ss == s:
			count[i] = 1
	for i in range(n - 2, -1, -1):
		count[i] += count[i + 1]
	(ans, ss) = (0, 0)
	for i in range(n - 2):
		ss += l[i]
		if ss == s:
			ans += count[i + 2]
	print(ans)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
if s % 3 != 0:
	print(0)
else:
	cnt = 0
	ans = 0
	res = 0
	s //= 3
	for i in range(n - 1):
		cnt += a[i]
		if cnt == 2 * s:
			ans += res
		if cnt == s:
			res += 1
	print(ans)


from math import factorial
n = int(input())
val = list(map(int, input().split()))
total = sum(val)
flag = True
count1 = 0
count2 = 0
result = 0
if total % 3 != 0:
	flag = False
	print(0)
elif n < 3:
	print(0)
else:
	for i in range(1, n):
		val[i] += val[i - 1]
	if total != 0:
		for i in range(n - 2, -1, -1):
			if val[i] == 2 * total / 3:
				count2 += 1
			if val[i] == total / 3:
				result += count2
		print(result)
	else:
		for i in range(n - 1):
			if val[i] == 0:
				count1 += 1
		if n > 2:
			res = int(factorial(count1) / (factorial(2) * factorial(count1 - 2)))
			print(res)
		else:
			print(0)


from bisect import bisect_left

def bini(a, x):
	i = bisect_left(a, x)
	return i
a = int(input())
z = list(map(int, input().split()))
if sum(z) % 3 != 0:
	print(0)
else:
	t = sum(z) // 3
	total = 0
	count = 0
	save = []
	save1 = []
	ans = []
	for i in range(len(z)):
		total = total + z[i]
		if total == t and i != len(z) - 1:
			save.append(i)
		if total == 2 * t and i != 0 and (i != len(z) - 1):
			save1.append(i)
	for i in range(len(save1)):
		ans.append(bini(save, save1[i]))
	print(sum(ans))


n = int(input())
array = list(map(int, input().split()))
sum = sum(array)
if sum % 3 != 0 and sum != 0:
	print(0)
	exit()
need = sum / 3
first_partition = 0
second_partition = 0
answer = 0
for i in range(0, n - 1):
	if i > 0:
		array[i] += array[i - 1]
	if array[i] == need + need:
		answer += first_partition
	if array[i] == need:
		first_partition += 1
print(answer)


n = int(input())
a = list(map(int, input().split()))
h = 0
sums = 0
fzeropoint = 0
minus = 0
result = 0
j = 0
l = n - 1
i = 0
for y in range(n):
	h = h + a[y]
if h % 3 != 0 or n < 3:
	print(0)
else:
	for k in range(n):
		sums = sums + a[k]
		if sums == h // 3:
			fzeropoint = fzeropoint + 1
			break
	sums = 0
	while l > k:
		sums = sums + a[l]
		if sums == h // 3:
			break
		l = l - 1
	sums = 0
	i = k + 1
	j = l - 1
	while i < l - 1:
		sums = sums + a[i]
		if sums == 0:
			fzeropoint = fzeropoint + 1
		i = i + 1
		minus = sums
	if l != k:
		result = fzeropoint + result
	sums = 0
	while j > k + 1:
		if minus == 0:
			fzeropoint = fzeropoint - 1
		minus = minus - a[j - 1]
		sums = sums + a[j]
		if sums == 0:
			result = result + fzeropoint
		j = j - 1
	print(result)


s = input()
s1 = input()
N = int(s)
karl = [int(i) for i in s1.split(' ')]
left = [0 for i in karl]
right = [0 for i in karl]
k = 0
suma = sum(karl[:])
leg = 0
if suma % 3 != 0:
	print(0)
else:
	for i in range(0, N):
		leg = leg + karl[i]
		if leg == suma / 3:
			left[i] = 1
	leg = 0
	for i in range(N - 1, -1, -1):
		leg = karl[i] + leg
		if leg == suma / 3:
			right[i] = 1
	l = 0
	right[0] = right[1] = left[N - 1] = left[N - 2] = 0
	right.append(0)
	fo4ik = sum(right[:])
	for i in range(0, N):
		if left[i] == 1:
			k = k + fo4ik - right[i] - right[i + 1]
		if right[i] == 1:
			fo4ik = fo4ik - 1
	print(k)


n = int(input())
arr = list(map(int, input().split()))
total = sum(arr)
ans = 0
if total % 3 != 0:
	print(0)
else:
	c = 0
	s = 0
	onethird = total // 3
	twothird = 2 * onethird
	for i in range(n - 1):
		s += arr[i]
		if s == twothird:
			ans += c
		if s == onethird:
			c += 1
	print(ans)


from itertools import accumulate
n = int(input())
a = list(map(int, input().split()))
if sum(a) % 3 != 0 or n < 3:
	print(0)
else:
	c = list(accumulate(a))
	x = 0
	k = 0
	for _ in range(len(c) - 1):
		if c[_] * 3 == c[-1] * 2:
			x += k
		if c[_] * 3 == c[-1]:
			k += 1
	print(x)


n = int(input())
a = input()
A = list(map(int, list(a.split())))
p = sum(A)
if p % 3 != 0:
	print(0)
else:
	k = p // 3
	f = 0
	if k == 0:
		s = 0
		for i in range(n - 1):
			s = s + A[i]
			if s == 0:
				f = f + 1
		print(f * (f - 1) // 2)
	else:
		pre = [0] * n
		pre[0] = A[0]
		for i in range(1, n):
			pre[i] = pre[i - 1] + A[i]
		i = 0
		c = 0
		f = 0
		while i < n:
			if pre[i] == k:
				f = f + 1
			elif pre[i] == 2 * k:
				c = c + f
			i = i + 1
		print(c)


n = int(input())
l = list(map(int, input().split()))
s = 0
a = list()
for i in range(0, n):
	a.append(s + l[i])
	s = s + l[i]
if a[-1] % 3 != 0:
	print(0)
else:
	s = 0
	six = [0] * n
	f = 2 * (a[-1] // 3)
	f2 = a[-1] // 3
	ans = 0
	for i in range(1, n - 1):
		if a[n - i - 1] == f:
			ans = ans + 1
			six[n - i - 1] = ans
		else:
			six[n - 1 - i] = ans
	for i in range(0, n - 2):
		if a[i] == f2:
			s = s + six[i + 1]
	print(s)


n = int(input())
t = [0] + list(map(int, input().split()))
for i in range(n):
	t[i + 1] += t[i]
p = [0] * (n + 1)
s = t[-1]
a = s // 3
b = s - a
if s == 3 * a:
	for i in range(1, n - 1):
		p[i] += p[i - 1]
		if t[i] == a:
			p[i] += 1
print(sum((p[i] for i in range(1, n - 1) if t[i + 1] == b)))


def countSplitWays(values):
	total = sum(values)
	curr_sum = 0
	count = [0] * len(values)
	for i in range(len(values) - 1, -1, -1):
		curr_sum = curr_sum + values[i]
		if curr_sum == total / 3:
			count[i] = 1
	for index in range(len(values) - 2, -1, -1):
		count[index] = count[index] + count[index + 1]
	result = 0
	curr_sum = 0
	for pos in range(0, len(count) - 2):
		curr_sum = curr_sum + values[pos]
		if curr_sum == total / 3:
			result = result + count[pos + 2]
	return result
n = int(input())
values = [int(x) for x in input().split()]
print(countSplitWays(values))


import math, sys
from collections import defaultdict, Counter, deque
from bisect import bisect_left, bisect_right
INF = float('inf')
MOD = int(1000000000.0) + 7
MAX = int(1000000.0) + 1

def solve():
	n = int(input())
	arr = array()
	total = sum(arr)
	if total % 3 != 0 or n < 3:
		print(0)
		return
	target = total // 3
	cnt = [0 for i in range(n)]
	suf = 0
	for i in range(n - 1, -1, -1):
		suf += arr[i]
		if suf == target:
			cnt[i] = 1
	for i in range(n - 2, -1, -1):
		cnt[i] += cnt[i + 1]
	ans = pre = 0
	for i in range(0, n - 2):
		pre += arr[i]
		if pre == target:
			ans += cnt[i + 2]
	print(ans)

def main():
	t = 1
	for _ in range(t):
		solve()

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def input():
	return sys.stdin.readline().rstrip('\n').strip()

def print(*args, sep=' ', end='\n'):
	first = True
	for arg in args:
		if not first:
			sys.stdout.write(sep)
		sys.stdout.write(str(arg))
		first = False
	sys.stdout.write(end)
primes = [1 for i in range(MAX)]

def sieve():
	global primes
	primes[0] = primes[1] = 0
	i = 2
	while i <= MAX ** 0.5:
		j = i * i
		while primes[i] and j < MAX:
			if j % i == 0:
				primes[j] = 0
			j += i
		i += 1

def vars():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))
main()


import sys, os, re, datetime
from collections import *
from bisect import *

def mat(v, *dims):

	def submat(i):
		if i == len(dims) - 1:
			return [v for _ in range(dims[-1])]
		return [submat(i + 1) for _ in range(dims[i])]
	return submat(0)
__cin__ = None

def cin():
	global __cin__
	if __cin__ is None:
		__cin__ = iter(input().split(' '))
	try:
		return next(__cin__)
	except StopIteration:
		__cin__ = iter(input().split(' '))
		return next(__cin__)

def iarr(n):
	return [int(cin()) for _ in range(n)]

def farr(n):
	return [float(cin()) for _ in range(n)]

def sarr(n):
	return [cin() for _ in range(n)]

def carr(n):
	return input()

def imat(n, m):
	return [iarr(m) for _ in range(n)]

def fmat(n, m):
	return [farr(m) for _ in range(n)]

def smat(n, m):
	return [sarr(m) for _ in range(n)]

def cmat(n, m):
	return [input() for _ in range(n)]
n = int(cin())
a = iarr(n)
tot = 0
acc = 0
ans = 0
s = {}
for i in range(n):
	tot += a[i]
	if tot not in s:
		s[tot] = []
	s[tot].append(i)
for (k, v) in s.items():
	v.sort(key=lambda x: x)
for i in range(n):
	acc += a[i]
	if tot == 3 * acc:
		if 2 * acc in s:
			v = s[2 * acc]
			j = bisect_right(v, i)
			if j == len(v):
				continue
			ans += len(v) - j - (1 if v[-1] == n - 1 else 0)
print(ans)


n = int(input())
arr = list(map(int, input().split()))
arr_sum = sum(arr)
if arr_sum % 3 != 0:
	print(0)
	exit()
arr_sum //= 3
cnt = [0] * n
curr = 0
for i in range(n - 1, -1, -1):
	curr += arr[i]
	if curr == arr_sum:
		cnt[i] = 1
for i in range(n - 2, -1, -1):
	cnt[i] += cnt[i + 1]
(res, curr) = (0, 0)
for i in range(n - 2):
	curr += arr[i]
	if curr == arr_sum:
		res += cnt[i + 2]
print(res)


import math
size = int(input())
arr = [int(j) for j in input().split(' ')]
total = sum(arr)
i_ways = []
ways = 0
if total % 3 != 0:
	print(0)
else:
	t = total / 3
	sums = [0] * size
	s = 0
	for (i, elem) in enumerate(arr):
		s += elem
		sums[i] = s
	i_index = []
	j_index = []
	for (i, elem) in enumerate(sums):
		if elem == t and i < size - 2:
			i_index.append(i)
		if elem == 2 * t and i > 0 and (i < size - 1):
			j_index.append(i)
	ways = 0
	last_index = 0
	for i in i_index:
		for j in range(last_index, len(j_index)):
			if i < j_index[j]:
				last_index = j
				last_diff = len(j_index) - j
				ways += last_diff
				break
	print(ways)


import bisect
n = int(input())
arr = [int(x) for x in input().split()]
all_sum = sum(arr)
if all_sum % 3 == 0:
	f_sum = all_sum // 3
	s_sum = f_sum * 2
	f_ind = []
	s_ind = []
	local_sum = 0
	for i in range(n - 1):
		local_sum += arr[i]
		if local_sum == f_sum and i < n - 2:
			f_ind += [i]
		if local_sum == s_sum:
			s_ind += [i]
	ans = 0
	l = len(s_ind)
	for i in f_ind:
		fr = bisect.bisect(s_ind, i)
		ans += l - fr
	print(ans)
else:
	print(0)


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
n = int(z())
l = zzz()
s = sum(l)
ans = 0
if s % 3 == 0:
	a = s // 3
	b = s - a
	(s, d) = (0, 0)
	for i in l[:-1]:
		s += i
		if s == b:
			ans += d
		if s == a:
			d += 1
print(ans)


import sys
import math as mt
input = sys.stdin.buffer.readline
import bisect
t = 1
for __ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	pref = [0] * (n + 1)
	pref[0] = l[0]
	d1 = {}
	for i in range(1, n):
		pref[i] = pref[i - 1] + l[i]
		if d1.get(pref[i], -1) == -1:
			d1[pref[i]] = []
		d1[pref[i]].append(i)
	suffix = [0] * (n + 1)
	suffix[n - 1] = l[n - 1]
	d = {}
	suma = sum(l[:])
	d[l[-1]] = []
	d[l[-1]].append(n - 1)
	for j in range(n - 2, -1, -1):
		suffix[j] = suffix[j + 1] + l[j]
		if d.get(suffix[j], -1) == -1:
			d[suffix[j]] = []
		d[suffix[j]].append(j)
	for i in d:
		d[i].sort()
	ans = 0
	for i in range(n - 1):
		if d.get(pref[i], -1) != -1 and pref[i] * 3 == suma:
			j = bisect.bisect(d[pref[i]], i)
			if j < len(d[pref[i]]):
				ans += len(d[pref[i]]) - j
				if d[pref[i]][j] == i + 1:
					ans -= 1
	print(ans)


n = int(input())
arr = list(map(int, input().split()))
if sum(arr) % 3:
	print(0)
elif len(arr) < 3:
	print(0)
else:
	d = sum(arr) // 3
	pre = []
	r = []
	h = []
	s = 0
	count = 0
	for j in range(len(arr)):
		s += arr[j]
		pre.append(s)
		h.append(count)
		if s == d:
			count += 1
	i = 0
	while i < n:
		if pre[i] == 2 * d:
			r.append(i)
		i += 1
	ans = 0
	for elem in r:
		if elem != n - 1:
			ans += h[elem]
	print(ans)


n = int(input())
a = [0 for i in range(n + 1)]
mp = map(int, input().split())
ind = 1
sum = 0
for j in mp:
	sum += j
	a[ind] = j
	ind += 1
count = 0
if sum % 3 != 0:
	print(0)
else:
	need = sum // 3
	n_cnt = 0
	ss = 0
	for i in range(1, n):
		ss += a[i]
		if ss == need * 2:
			count += n_cnt
		if ss == need:
			n_cnt += 1
	print(count)


n = int(input())
lst = [int(x) for x in input().split()]

def waysToSplit(n, lst):
	s = sum(lst)
	if s % 3 != 0:
		return 0
	else:
		dp = [0 for x in range(n)]
		tempS = 0
		for i in range(n - 1, -1, -1):
			tempS += lst[i]
			if i < n - 1:
				dp[i] = dp[i + 1]
			if tempS == s / 3:
				dp[i] += 1
		ans = 0
		tempS = 0
		for i in range(0, n - 2):
			tempS += lst[i]
			if tempS == s / 3:
				ans += dp[i + 2]
		return ans
print(waysToSplit(n, lst))


def main():
	n = int(input())
	a = list(map(int, input().split()))
	s = sum(a)
	if s % 3 != 0 or len(a) < 3:
		print(0)
		return
	_s = s // 3
	sums = [0] * (n + 1)
	ss = 0
	for i in range(n - 1, 1, -1):
		ss += a[i]
		sums[i] = sums[i + 1] + int(ss == _s)
	res = 0
	ss = 0
	for i in range(n - 2):
		ss += a[i]
		if ss == _s:
			res += sums[i + 2]
	print(res)
main()


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
m = 0
c = 0
ans = 0
if s % 3 == 0:
	s //= 3
	for i in range(n - 1):
		m += a[i]
		if m == s * 2:
			ans += c
		if m == s:
			c += 1
print(ans)


def numSmall(arr, ele):
	left = 0
	right = len(arr) - 1
	best = None
	while left <= right:
		mid = (left + right) // 2
		if arr[mid] < ele:
			best = mid
			left = mid + 1
		else:
			right = mid - 1
	if best != None:
		return best + 1
	else:
		return -1
n = int(input())
arr = list(map(int, input().split(' ')))
if len(arr) <= 2:
	print(0)
else:
	req = sum(arr) / 3
	s = 0
	lst = []
	for i in range(len(arr)):
		s += arr[i]
		if s == req:
			lst.append(i)
	s = 0
	lst2 = []
	for j in range(len(arr) - 1, -1, -1):
		s += arr[j]
		if s == req:
			lst2.append(j)
	s2 = []
	for ele in lst2:
		if ele > 1:
			s2.append(ele)
	s1 = []
	for ele in lst:
		if ele < len(arr) - 2:
			s1.append(ele)
	ans = 0
	for j in s2:
		s = numSmall(s1, j - 1)
		if s != -1:
			ans += s
	print(ans)


n = int(input())
arr = [int(a) for a in input().split()]
thirds = sum(arr) / 3
if n < 3:
	print(0)
elif n == 3:
	if sum(arr) == arr[0] * 3:
		print(1)
	else:
		print(0)
else:
	answer = 0
	count = 0
	total = 0
	for index in range(n - 1):
		total += arr[index]
		if total == 2 * thirds:
			answer += count
		if total == thirds:
			count += 1
	print(answer)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
res = 0
if s % 3 == 0:
	s1 = s // 3
	s2 = 2 * s1
	r = 0
	c = 0
	for i in a[:-1]:
		r += i
		if r == s2:
			res += c
		if r == s1:
			c += 1
print(res)


n = int(input())
l = list(map(int, input().split()))
s = sum(l)
c = 0
if s % 3 == 0:
	x = s // 3
	y = s - x
	(s1, t) = (0, 0)
	for i in range(n - 1):
		s1 += l[i]
		if s1 == y:
			c += t
		if s1 == x:
			t += 1
print(c)


from bisect import bisect_right
n = int(input())
arr = list(map(int, input().split()))
arr2 = arr[:]
leftSum = []
rightSum = []
for x in range(1, n):
	arr[x] += arr[x - 1]
toGet = arr[-1] / 3
for x in range(n):
	if arr[x] == toGet:
		leftSum.append(x)
if arr2[-1] == toGet:
	rightSum.append(n - 1)
for x in range(n - 2, -1, -1):
	arr2[x] += arr2[x + 1]
	if arr2[x] == toGet:
		rightSum.append(x)
times = 0
if len(rightSum) > 0 and len(leftSum) > 0:
	for right in rightSum:
		i = bisect_right(leftSum, right)
		if right - leftSum[i - 1] == 0:
			i -= 1
		if right - leftSum[i - 1] == 1:
			i -= 1
		if i > 0:
			times += i
print(times)


n = int(input())
a = list(map(int, input().split()))
sum_a = sum(a)
sum_a_3 = sum_a // 3
res = 0
if sum_a % 3 == 0:
	cnt = 1 if a[0] == sum_a_3 else 0
	for i in range(1, n - 1):
		a[i] += a[i - 1]
		if sum_a - a[i] == sum_a_3:
			res += cnt
		cnt += a[i] == sum_a_3
print(res)


test_mode = 0

def solver(n, r):
	tprint('\n'.join([str(n), str(r)]))
	n = int(n)
	r = list(map(int, r.split()))
	s = sum(r) // 3
	cnt = [0] * 1000010
	ss = 0
	for i in range(n - 1, -1, -1):
		ss += r[i]
		if ss == s:
			cnt[i] = 1
	for i in range(n - 2, -1, -1):
		cnt[i] += cnt[i + 1]
	ans = 0
	ss = 0
	for i in range(n - 2):
		ss += r[i]
		if ss == s:
			ans += cnt[i + 2]
	result = ans if sum(r) % 3 == 0 else 0
	return result

def compare_answer(xx, xy):
	if xx == xy:
		print('Test passed\n\n')
	else:
		print('Failed! Expected   >> {}\n        Found      >> {}\n\n'.format(xy, xx))

def tprint(something):
	if test_mode:
		print(something)
if not test_mode:
	print(solver(input(), input()))
else:
	compare_answer(solver(5, '1 2 3 0 3'), 2)
	compare_answer(solver(4, '0 1 -1 0'), 1)
	compare_answer(solver(2, '4 1'), 0)
	compare_answer(solver(9, '0 0 0 0 0 0 0 0 0'), 28)


from itertools import *
(n, a) = (int(input()), list(map(int, input().split())))
k = sum(a)
if k % 3 != 0:
	print(0)
	quit()
(k, p, c) = (k // 3, 0, 0)
for r in accumulate(a[:-1]):
	if r == 2 * k:
		p += c
	if r == k:
		c += 1
print(p)


n = int(input())
ch = str(input())
LLL = ch.split()
L = list()
L1 = list()
total = 0
for c in LLL:
	L.append(int(c))
	total += int(c)
	L1.append(total)
if total % 3 != 0:
	print(0)
elif total != 0:
	c1 = 0
	c2 = 0
	res = 0
	for i in range(0, n - 1):
		if L1[i] == total // 3:
			c1 += 1
		if L1[i] == total // 3 * 2 and i != 0 and (i != n - 1):
			res += c1
	print(res)
else:
	c1 = 0
	c2 = 0
	res = 0
	for i in range(n - 1):
		if L1[i] == 0 and i != 0:
			c2 += c1
		if L1[i] == 0:
			c1 += 1
	print(c2)


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd
from collections import deque
n = int(input())
l = list(map(int, input().split()))
z = sum(l)
if z and z % 3 == 0 and (n >= 3):
	a = z // 3
	b = 2 * a
	x = 0
	y = 0
	s = 0
	for i in l:
		s += i
		if s == a:
			x += 1
		elif s == b:
			y += x
	print(y)
elif z == 0:
	count = -1
	s = 0
	for i in l:
		s += i
		if s == 0:
			count += 1
	print(count * (count - 1) // 2)
else:
	print(0)


n = int(input())
a = list(map(int, input().split()))
for i in range(1, n):
	a[i] += a[i - 1]
(x, answer) = (0, 0)
for i in range(n - 1):
	if a[i] * 3 == a[n - 1] * 2:
		answer += x
	if a[i] * 3 == a[n - 1]:
		x += 1
print(answer)


n = int(input())
array = list(map(int, input().split()))
output = 0
total = sum(array)
if total % 3 == 0:
	first = total // 3
	second = total - first
	total = t = 0
	for i in range(n - 1):
		total += array[i]
		if total == second:
			output += t
		if total == first:
			t += 1
print(output)


from itertools import accumulate
n = int(input())
nums = list(map(int, input().split()))
sum_accumulate = list(accumulate(nums))
total = sum_accumulate[-1]
num_of_ways = 0
if n >= 3 and total % 3 == 0 and (total // 3 in sum_accumulate) and (2 * total // 3 in sum_accumulate):
	if nums == [0] * n:
		num_of_ways = (n - 2) * (n - 1) // 2
	else:
		thirds = [0] * (n - 2)
		two_thirds = [0] * (n - 1)
		for i in range(n - 1):
			if sum_accumulate[i] == total // 3 and i < n - 2:
				thirds[i] = 1
			if sum_accumulate[i] == 2 * total // 3:
				two_thirds[i] = 1
		two_thirds_sum = [0] * (n - 1)
		two_thirds_sum[-1] = two_thirds[-1]
		for i in range(2, n):
			two_thirds_sum[-i] = two_thirds_sum[-i + 1] + two_thirds[-i]
		for i in range(n - 2):
			if thirds[i] == 1:
				num_of_ways += two_thirds_sum[i + 1]
print(num_of_ways)


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return map(int, input().split())

def array():
	return [int(i) for i in input().split()]
n = int(input())
a = array()
s = sum(a)
if s % 3 != 0:
	print(0)
else:
	ans = 0
	pref = [0] * (n + 1)
	for i in range(1, n + 1):
		pref[i] = pref[i - 1] + a[i - 1]
	good1 = []
	for i in range(1, n - 1):
		if pref[i] == s / 3:
			good1.append(i - 1)
	suff = [0] * (n + 1)
	for i in range(1, n + 1):
		suff[i] = suff[i - 1] + a[n - i]
	for i in range(1, n - 1):
		if suff[i] == s / 3:
			index = n - i - 2
			ans += bisect_right(good1, index)
	print(ans)


n = int(input())
x = input().split()
a = list((int(x[i]) for i in range(len(x))))
way = 0
suma = sum((a[i] for i in range(len(a))))
if suma % 3 > 0 or len(a) < 3:
	print(0)
else:
	sign = 1
	target = suma // 3
	if target == 0:
		segment = 0
		sumseg = 0
		for i in range(len(a)):
			sumseg += a[i]
			if sumseg == 0:
				segment += 1
		way = (segment - 1) * (segment - 2) // 2
	else:
		sumseg = 0
		list0 = []
		dict0 = {}
		dict1 = {}
		i = 1
		while i < len(a) - 1:
			if a[i] == 0:
				i0 = i - 1
				while a[i] == 0:
					i += 1
				dict0[i0] = dict1[i] = i - i0
			else:
				i += 1
		i = 0
		while i < len(a) - 2:
			sumseg += a[i]
			if sumseg == target:
				list0.insert(0, i)
				if i in dict0.keys():
					i += dict0[i]
					continue
			i += 1
		sumseg = 0
		list1 = []
		i = len(a) - 1
		while i > 1:
			sumseg += a[i]
			if sumseg == target:
				list1.insert(0, i)
				if i in dict1.keys():
					i -= dict1[i]
					continue
			i -= 1
		flag = 0
		sumlist1 = 0
		sumlist0 = 0
		for i in range(len(list1)):
			sumlist1 += 1 if list1[i] not in dict1.keys() else dict1[list1[i]]
		for i in range(len(list0)):
			sumlist0 += 1 if list0[i] not in dict0.keys() else dict0[list0[i]]
		minusadd = 0
		for i in range(len(list0)):
			if list0[i] < list1[0]:
				break
			else:
				for j in range(len(list1)):
					if list0[i] < list1[j]:
						for k in range(j):
							minusadd += (1 if list1[j] not in dict1.keys() else dict1[list1[j]]) * (1 if list0[i] not in dict0.keys() else dict0[list0[i]])
						break
		way = sumlist1 * sumlist0 - minusadd
	print(way)


n = int(input())
a = list(map(int, input().split()))
cnt = [0]
for i in range(n):
	cnt.append(cnt[i] + a[i])
ans = 0
if cnt[n] % 3 != 0:
	print(0)
else:
	goal = cnt[n] // 3
	cur = 0
	for i in range(1, n):
		if cnt[i] == 2 * goal:
			ans += cur
		if cnt[i] == goal:
			cur += 1
	print(ans)


n = int(input().strip())
arr = list(map(int, input().strip().split()))
s = sum(arr)
if s % 3 != 0:
	print(0)
else:
	req = s // 3
	sums = [arr[0]]
	for i in range(1, len(arr)):
		sums.append(sums[i - 1] + arr[i])
	if req == 0:
		n = sums.count(0) - 1
		print(n * (n - 1) // 2)
	elif not (req in sums and req * 2 in sums and (req * 3 in sums)):
		print(0)
	else:
		ans = 0
		count = sums[sums.index(req):].count(req * 2)
		ans += count
		for i in range(sums.index(req) + 1, len(sums)):
			if sums[i] == req * 2:
				count -= 1
			if sums[i] == req:
				ans += count
		print(ans)


def formSumArray(l, a, b):
	arr = [l[0]]
	aIndexes = []
	bIndexes = []
	if arr[-1] == a:
		aIndexes.append(0)
	if arr[-1] == b:
		bIndexes.append(0)
	for i in range(1, len(l)):
		arr.append(arr[i - 1] + l[i])
		if arr[-1] == a and i != len(l) - 1:
			aIndexes.append(i)
		if arr[-1] == b and i != len(l) - 1:
			bIndexes.append(i)
	return (arr, aIndexes, bIndexes)

def next(arr, target):
	start = 0
	end = len(arr) - 1
	ans = -1
	while start <= end:
		mid = (start + end) // 2
		if arr[mid] <= target:
			start = mid + 1
		else:
			ans = mid
			end = mid - 1
	return ans

def findways(l):
	s = sum(l)
	if s % 3 != 0:
		return 0
	a = s // 3
	b = a * 2
	(sumarray, aIndexes, bIndexes) = formSumArray(l, a, b)
	count = 0
	for aIndex in aIndexes:
		j = next(bIndexes, aIndex)
		if j != -1:
			count += len(bIndexes) - j
	return count
input()
l = list(map(int, input().split()))
print(findways(l))


n = int(input())
a = list(map(int, input().split()))
(s, cb, r, b) = ([0] * n, [0] * n, [0] * n, [0] * n)
(t, count) = (0, 0)
p = 0
for i in range(n):
	t += a[i]
	s[i] += t
for i in range(n):
	b[i] = t - p
	p += a[i]
if t % 3 != 0:
	print(0)
else:
	for i in range(n):
		if b[i] == t // 3:
			(cb[i], count) = (1, count + 1)
	p = 0
	for i in range(n):
		r[i] = count - p
		p += cb[i]
	result = 0
	for i in range(n - 2):
		if s[i] == t // 3:
			result += r[i + 2]
	print(result)


n = int(input())
arr = list(map(int, input().strip().split()))
for i in range(n):
	if i > 0:
		arr[i] += arr[i - 1]
(ans, cnt) = (0, 0)
for i in range(n - 1):
	if arr[i] * 3 == arr[n - 1] * 2:
		ans += cnt
	if arr[i] * 3 == arr[n - 1]:
		cnt += 1
print(ans)


(n, a) = (int(input()), list(map(int, input().split())))
(k, p) = (sum(a), 0)
if k % 3 == 0:
	(k, c, s) = (k // 3, 0, 0)
	for r in a[:-1]:
		s += r
		if s == 2 * k:
			p += c
		c += s == k
print(p)


n = int(input())
a = [int(i) for i in input().split()]
totalsum = sum(a)
x = totalsum // 3
if x * 3 != totalsum:
	print(0)
else:
	cursum = 0
	count1 = 0
	count = 0
	for i in range(n):
		cursum += a[i]
		if cursum == x:
			count1 += 1
		elif cursum == 2 * x:
			count += count1
	if x == 0:
		print((count1 - 2) * (count1 - 1) // 2)
	else:
		print(count)


n = int(input())
arr = list(map(int, input().split()))
sumOfArr = sum(arr)
ans = 0
if sumOfArr % 3 == 0:
	pointer1 = sumOfArr // 3
	pointer2 = sumOfArr - pointer1
	sumOfArr = 0
	t = 0
	for i in range(n - 1):
		sumOfArr += arr[i]
		if sumOfArr == pointer2:
			ans += t
		if sumOfArr == pointer1:
			t += 1
print(ans)


def binarysearch(v, x):
	low = 0
	high = len(v) - 1
	while low <= high:
		mid = int((low + high) / 2)
		if v[mid] <= x:
			low = mid + 1
		elif v[mid] > x and v[mid - 1] <= x:
			return mid
		elif v[mid] > x and mid == 0:
			return mid
		else:
			high = mid - 1
	return -1

def CountContiguousParts(arr, n):
	count = 0
	prefix = [0 for i in range(n)]
	prefix[0] = arr[0]
	for i in range(1, n, 1):
		prefix[i] = prefix[i - 1] + arr[i]
	total_sum = prefix[n - 1]
	if total_sum % 3 != 0:
		return 0
	suffix = [0 for i in range(n)]
	suffix[n - 1] = arr[n - 1]
	i = n - 2
	while i >= 0:
		suffix[i] = suffix[i + 1] + arr[i]
		i -= 1
	v = []
	for i in range(n):
		if suffix[i] == int(total_sum / 3):
			v.append(i)
	for i in range(n):
		if prefix[i] == int(total_sum / 3):
			res = binarysearch(v, i + 1)
			if res != -1:
				count += len(v) - res
	return count
n = int(input())
arr = list(map(int, input().split()))
print(CountContiguousParts(arr, n))


n = int(input())
nums = [int(x) for x in input().strip().split()]
sum = 0
for x in nums:
	sum += x
if sum % 3 != 0:
	print(0)
	exit()
sum /= 3
t = 0
ct = 0
res = 0
for i in range(0, n - 1):
	t += nums[i]
	if t == 2 * sum:
		res += ct
	if t == sum:
		ct += 1
print(res)


n = int(input())
a = list(map(int, input().split()))
total = sum(a)
if total % 3 != 0:
	print(0)
else:
	count = 0
	cnt = [0] * n
	sums = [0] * n
	s = 0
	for i in range(n - 1, -1, -1):
		s += a[i]
		if s == total // 3:
			cnt[i] = 1
	sums = [0] * n
	sums[-1] = cnt[-1]
	for i in range(n - 2, -1, -1):
		sums[i] = sums[i + 1] + cnt[i]
	s = 0
	for i in range(n - 2):
		s += a[i]
		if s == total // 3:
			count += sums[i + 2]
	print(count)


r = int(input())
l = list(map(int, input().split()))
s = sum(l)
if s % 3 != 0:
	print(0)
else:
	p = s / 3
	q = 2 * p
	w = y = x = 0
	for b in range(r - 1):
		x += l[b]
		if x == q:
			w += y
		if x == p:
			y += 1
	print(w)


n = int(input())
a = list(map(int, input().split()))
b = sum(a)
if b % 3 == 0:
	k1 = b // 3
	k2 = 2 * k1
	(s, t, f) = (0, 0, 0)
	for i in range(n - 1):
		s += a[i]
		if s == k2:
			f += t
		if s == k1:
			t += 1
	print(f)
else:
	print(0)


elements = int(input())
arr = list(map(int, input().split()))
(arr_sum, answer, count, temp_sum) = (sum(arr), 0, 0, 0)
if arr_sum % 3 == 0:
	arr_sum = arr_sum / 3
	for i in range(elements - 1):
		temp_sum = temp_sum + arr[i]
		if temp_sum == 2 * arr_sum:
			answer = answer + count
		if temp_sum == arr_sum:
			count = count + 1
print(answer)


from collections import defaultdict
n = int(input())
numbers = [int(i) for i in input().split(' ')]
total = sum(numbers)
if total % 3 != 0:
	print(0)
else:
	count = 0
	prefix_sum = [0] * n
	suffix_sum = [0] * n
	suffix_memo = [0] * (n + 1)
	i = 0
	j = n - 1
	while i < n - 2 and j > 1:
		if i == 0:
			prefix_sum[i] = numbers[i]
			suffix_sum[j] = numbers[j]
		else:
			prefix_sum[i] = prefix_sum[i - 1] + numbers[i]
			suffix_sum[j] = suffix_sum[j + 1] + numbers[j]
		if 3 * suffix_sum[j] == total:
			suffix_memo[j] = suffix_memo[j + 1] + 1
		else:
			suffix_memo[j] = suffix_memo[j + 1]
		i += 1
		j -= 1
	for (index, value) in enumerate(prefix_sum):
		if index >= n - 2:
			break
		if 3 * value == total:
			count += suffix_memo[index + 2]
	print(count)


from sys import exit
n = int(input())
p1 = 0
p2 = 0
tot = 0
num = list(map(int, input().split()))
s = sum(num)
if s % 3 == 0:
	(s1, s2) = (s / 3, 2 * s / 3)
	for u in num[:-1]:
		p1 += u
		if p1 == s2:
			tot += p2
		if p1 == s1:
			p2 += 1
print(tot)


def check():
	n = int(input())
	ls = list(map(int, input().split()))
	a = []
	b = []
	s = 0
	m = sum(ls)
	if m % 3 != 0:
		return 0
	m = int(sum(ls) / 3)
	for i in range(n):
		s += ls[i]
		if s == m and i != n - 1:
			a.append(i)
		if s == 2 * m and i != n - 1:
			b.append(i)
	s = 0
	ans = 0
	B = len(b)
	for i in a:
		while s < B and i >= b[s]:
			s += 1
		ans += B - s
	return ans
print(check())


import collections
import itertools
from functools import lru_cache
import sys
import math

def helper():
	pass

def solve():
	n = int(input())
	arr = list(map(int, input().split(' ')))
	sm = sum(arr)
	if sm % 3 != 0:
		print(0)
		return
	else:
		pre = [0 for i in range(n)]
		suf = [0 for i in range(n)]
		(pre[0], suf[n - 1]) = (arr[0], arr[n - 1])
		for i in range(1, n):
			pre[i] = arr[i] + pre[i - 1]
		for i in range(n - 2, -1, -1):
			suf[i] = arr[i] + suf[i + 1]
		targ = sm // 3
		count = 0
		targeCount = [0 for i in range(n)]
		for i in range(n - 1, -1, -1):
			if suf[i] == targ:
				targeCount[i] = targeCount[i + 1] + 1 if i < n - 1 else 1
			else:
				targeCount[i] = targeCount[i + 1] if i < n - 1 else 0
		for i in range(len(arr)):
			if pre[i] == targ:
				count += targeCount[i + 2] if i + 2 < n else 0
		print(count)
		return
t = 1
for _ in range(t):
	solve()


def solve(a, n):
	leftSum = [0] * n
	rightSum = [0] * n
	cnts = [0] * n
	ans = 0
	leftSum[0] = a[0]
	rightSum[n - 1] = a[n - 1]
	for i in range(1, n):
		leftSum[i] = leftSum[i - 1] + a[i]
	for i in range(n - 2, -1, -1):
		rightSum[i] = rightSum[i + 1] + a[i]
	total = leftSum[-1]
	if total % 3:
		return 0
	for i in range(n - 1, -1, -1):
		if rightSum[i] == total // 3:
			cnts[i] = 1
	sums = [0] * n
	sums[n - 1] = cnts[n - 1]
	for i in range(n - 2, -1, -1):
		sums[i] = sums[i + 1] + cnts[i]
	for i in range(n - 2):
		if leftSum[i] == total // 3:
			ans += sums[i + 2]
	return ans

def main():
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(a, n))
main()


n = int(input())
l = [*map(int, input().split())]
total = sum(l)
if total % 3 != 0:
	print(0)
else:
	arr = [0] * n
	x = 0
	val = total // 3
	for i in range(n - 1, -1, -1):
		x += l[i]
		if x == val:
			arr[i] = 1
	for i in range(n - 2, -1, -1):
		arr[i] += arr[i + 1]
	ans = 0
	x = 0
	for i in range(n - 2):
		x += l[i]
		if x == val:
			ans += arr[i + 2]
	print(ans)


n = int(input())
l = list(map(int, input().split()))
if sum(l) % 3 != 0:
	print(0)
	exit(0)
cnt = 0
ans = 0
a = 0
k = sum(l) // 3
for i in range(n - 1):
	a += l[i]
	if a == 2 * k:
		ans += cnt
	if a == k:
		cnt += 1
print(ans)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def helper(i, _sum):
	pending_total = total - _sum
	cur_sum = 0
	count = 0
	while i < n - 1:
		cur_sum += arr[i]
		if cur_sum == _sum and cur_sum == pending_total - cur_sum:
			count += 1
		i += 1
	return count

def solve():
	partial_sum = total / 3
	cur_sum = 0
	count = [0] * (n + 1)
	for i in range(n - 1, -1, -1):
		cur_sum += arr[i]
		if cur_sum == partial_sum:
			count[i] += 1
	for i in range(n - 2, -1, -1):
		count[i] += count[i + 1]
	cur_sum = 0
	ans = 0
	for i in range(0, n - 2):
		cur_sum += arr[i]
		if cur_sum == partial_sum:
			ans += count[i + 2]
	return ans
n = inp()
arr = inlt()
total = sum(arr)
if total % 3 != 0:
	print(0)
else:
	print(solve())


q = int(input())
w = list(map(int, input().split()))
for i in range(1, q):
	w[i] += w[i - 1]
a = 0
b = 0
if w[q - 1] % 3 != 0:
	print(0)
else:
	for j in range(q - 1):
		if 3 * w[j] == 2 * w[q - 1]:
			a += b
		if 3 * w[j] == w[q - 1]:
			b += 1
	print(a)


n = int(input())
l = list(map(int, input().split()))
s = sum(l)
x = s / 3
if x != int(x):
	print('0')
else:
	x = int(s / 3)
	sum1 = 0
	a = [0] * len(l)
	for i in range(len(l) - 1, -1, -1):
		sum1 = sum1 + l[i]
		if sum1 == x:
			a[i] = 1
	b = a
	for i in range(len(l) - 2, -1, -1):
		b[i] += b[i + 1]
	ans = 0
	i = 0
	temp = 0
	while i + 2 < n:
		temp = temp + l[i]
		if temp == x:
			ans += b[i + 2]
		i = i + 1
	print(ans)


n = int(input())
a = list(map(int, input().split()))
b = [a[0]]
for i in a[1:]:
	b.append(b[-1] + i)
d = [a[-1]]
for i in a[:-1][::-1]:
	d.append(d[-1] + i)
d = d[::-1]
c = [0] * n
s = b[n - 1]
if b[0] * 3 == s:
	c[0] = 1
for i in range(1, n):
	if b[i] * 3 == s:
		c[i] = c[i - 1] + 1
	else:
		c[i] = c[i - 1]
ans = 0
for i in range(2, n):
	if d[i] * 3 == s:
		ans = ans + c[i - 2]
print(ans)


n = int(input())
l = list(map(int, input().split()))
summ = sum(l)
if summ % 3 != 0:
	print(0)
else:
	s = summ // 3
	temp = 0
	cnt = [0] * n
	for i in range(n - 1, -1, -1):
		temp += l[i]
		if temp == s:
			cnt[i] = 1
	for i in range(n - 2, -1, -1):
		cnt[i] += cnt[i + 1]
	ans = 0
	temp = 0
	for i in range(n - 2):
		temp += l[i]
		if temp == s:
			ans += cnt[i + 2]
	print(ans)


def solve(n, ar):
	if sum(ar) % 3 != 0:
		print(0)
		return
	s = sum(ar) // 3
	t = 0
	cnt = 0
	res = 0
	for i in range(n - 1):
		t += ar[i]
		if t == 2 * s:
			res += cnt
		if t == s:
			cnt += 1
	print(res)
n = int(input())
ar = list(map(int, input().split()))
solve(n, ar)


n = int(input())
arr = [int(i) for i in input().split()]
S = sum(arr)
if S % 3 != 0:
	print(0)
else:
	s = S // 3
	ss = 0
	cnt = [0 for _ in range(n)]
	for i in range(n - 1, -1, -1):
		ss += arr[i]
		if ss == s:
			cnt[i] = 1
	for i in range(n - 2, -1, -1):
		cnt[i] += cnt[i + 1]
	ans = 0
	ss = 0
	for i in range(0, n - 2):
		ss += arr[i]
		if ss == s:
			ans += cnt[i + 2]
	print(ans)


n = input()
n = int(n)
arr = [int(s) for s in input().split()]
total = sum(arr) / 3
ans = 0
suma = 0
first = 0
for i in range(n - 1):
	suma = suma + arr[i]
	if suma == total * 2:
		ans = ans + first
	if suma == total:
		first = first + 1
print(ans)


n = int(input())
ch = input()
l = list(map(int, ch.split()))
s = sum(l)
l1 = []
s1 = 0
a = 0
res = 0
if s % 3 == 0:
	for i in range(n):
		s1 = s1 + l[i]
		l1.append(s1)
	for i in range(n - 1):
		if l1[i] == s / 3 * 2:
			res = res + a
		if l1[i] == s / 3:
			a = a + 1
	print(res)
else:
	print(0)


n = int(input())
a = list(map(int, input().strip().split()))
arr = [0]
c = 0
for i in a:
	c += i
	arr.append(c)
x = sum(a)
if x % 3 != 0:
	print(0)
	exit(0)
op = 0
if x == 0:
	op = -1
	for i in arr:
		if i == 0:
			op += 1
	if op == len(a):
		print((op - 2) * (op - 1) // 2)
	else:
		print(op - 2)
	exit(0)
x1 = 0
x2 = 0
f = 1
op = 0
for i in arr:
	if i == x // 3:
		x1 += 1
	if i == 2 * (x // 3):
		x2 += 1
		op += x1
print(op)


import pdb
n = int(input())
vetor = [int(c) for c in input().split()]
soma_total = sum(vetor)
if soma_total % 3 == 0:
	soma_esperada_bloco = soma_total // 3
	soma_bloco_1 = 0
	soma_bloco_2 = 0
	encontrou_um_bloco_com_soma_esperada = False
	x = 0
	p = 0
	i = 0
	while True:
		elem = vetor[i]
		soma_bloco_1 += elem
		if encontrou_um_bloco_com_soma_esperada:
			soma_bloco_2 += elem
			if soma_bloco_2 == soma_esperada_bloco:
				p += x
		if soma_bloco_1 == soma_esperada_bloco:
			encontrou_um_bloco_com_soma_esperada = True
			x += 1
			soma_bloco_2 = 0
		i += 1
		if encontrou_um_bloco_com_soma_esperada:
			if i > n - 2:
				break
		elif i > n - 3:
			break
	print(p)
else:
	print('0')


n = int(input())
arr = [int(x) for x in input().split()]
s = sum(arr)
cnt = [0] * n
su = 0
sums = [0] * n
total = 0
if s % 3 != 0:
	print(0)
	exit(0)
else:
	s //= 3
	for i in range(n - 1, -1, -1):
		su += arr[i]
		if s == su:
			cnt[i] = 1
	for i in range(n - 2, -1, -1):
		cnt[i] += cnt[i + 1]
	su = 0
	for i in range(n - 2):
		su += arr[i]
		if su == s:
			total += cnt[i + 2]
	print(total)


n = int(input())
w = [int(x) for x in input().split()]
sum = 0
for i in w:
	sum += i
if sum % 3 != 0:
	print(0)
else:
	sum /= 3
	count0 = [int(0) for x in range(0, n)]
	k = 0
	for x in range(n - 1, 0, -1):
		k += w[x]
		if k == sum:
			count0[x] += 1
	for x in range(n - 2, 0, -1):
		count0[x] += count0[x + 1]
	sumls = 0
	k = 0
	for x in range(0, n - 2):
		sumls += w[x]
		if sumls == sum:
			k += count0[x + 2]
	print(k)


from itertools import accumulate
input()
a = list(accumulate(map(int, input().split())))
ans = cnt = 0
for x in a[:-1]:
	if x * 3 == a[-1] * 2:
		ans += cnt
	if x * 3 == a[-1]:
		cnt += 1
print(ans)


n = int(input())
l = list(map(int, input().split()))
sum = 0
for i in range(n):
	sum += l[i]
if sum % 3 != 0:
	print('0')
else:
	one_t = sum / 3
	two_t = one_t * 2
	ans = 0
	total = 0
	c = 0
	for i in range(n):
		total += l[i]
		if total == two_t and i < n - 1:
			ans += c
		if total == one_t:
			c += 1
	print(ans)


n = int(input())
a = list(map(int, input().split()))
sums = [0]
for i in a:
	sums.append(i + sums[-1])
if sums[-1] % 3 != 0:
	print(0)
else:
	x = sums[-1] // 3
	y = 2 * x
	count = 0
	ans = 0
	for i in range(1, n):
		if sums[i] == y:
			ans += count
		if sums[i] == x:
			count += 1
	print(ans)


import sys
n = int(input())
b = sys.stdin.readline()
b = b.split(' ')
for x in range(n):
	b[x] = int(b[x])
for x in range(1, n):
	b[x] += b[x - 1]
sum = b[n - 1]
t = sum / 3
if sum % 3 != 0:
	print('0')
elif n < 3:
	print('0')
elif t == 0:
	y = 0
	for x in range(n - 1):
		if b[x] == 0:
			y += 1
	print(int(y * (y - 1) / 2))
else:
	t1 = sum / 3
	t2 = t1 * 2
	ans = 0
	z = 0
	for x in range(n - 1):
		if b[x] == t1:
			z += 1
		if b[x] == t2:
			ans += z
	print(ans)


import sys
input = sys.stdin.readline
from math import log2, log, ceil

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return res

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	while x != link[x]:
		x = link[x]
	return x

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
n = int(input())
a = int_array()
ans = 0
total = sum(a)
if total % 3 != 0 or n < 3:
	print(0)
	exit()
req = total // 3
prefix = a.copy()
suffix = a.copy()
for i in range(1, n):
	prefix[i] += prefix[i - 1]
for i in range(n - 2, -1, -1):
	suffix[i] += suffix[i + 1]
req_suff = suffix.count(req)
count = 0
for i in range(n - 1):
	if suffix[i] == req:
		count += 1
	if prefix[i] == req:
		k = req_suff - count
		if suffix[i + 1] == req:
			k -= 1
		ans += k
print(ans)


class Solution:

	def numberOfWays(nums):
		if len(nums) < 3 or sum(nums) % 3 != 0:
			return 0
		part_sum = int(sum(nums) / 3)
		inter_sum = nums[0]
		part_count = int(part_sum == inter_sum)
		result = 0
		for i in range(1, len(nums) - 1):
			inter_sum += nums[i]
			if inter_sum == 2 * part_sum:
				result += part_count
			part_count += int(inter_sum == part_sum)
		return result
n = int(input())
nums = list(map(int, input().strip().split(' ')))
print(Solution.numberOfWays(nums))


n = int(input())
a = [int(x) for x in input().split()]
dp1 = [0] * n
dp2 = [0] * n
dp3 = [0] * n
c = 0
for i in range(n):
	c += a[i]
	dp1[i] = c
c = 0
S = dp1[-1]
if S % 3 != 0:
	print(0)
else:
	s = S // 3
	for i in range(n - 1, -1, -1):
		c += a[i]
		dp2[i] = c
	c = 0
	for i in range(n - 1, -1, -1):
		if dp2[i] == s:
			c += 1
		dp3[i] = c
	w = 0
	for i in range(0, n - 2):
		if dp1[i] == s:
			w += dp3[i + 2]
	print(w)


n = int(input())
lis = list(map(int, input().split()))
sums = [0] * n
sums[0] = lis[0]
ans = 0
doub = {}
for i in range(1, n):
	sums[i] = sums[i - 1] + lis[i]
if sums[-1] % 3 != 0:
	print(0)
else:
	tt = 2 * sums[-1] // 3
	for j in range(n - 1):
		if sums[j] == tt:
			ans += doub.get(sums[j], 0)
		doub[sums[j] * 2] = doub.get(sums[j] * 2, 0) + 1
	print(ans)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass
n = L()[0]
A = L()
for i in range(1, n):
	A[i] += A[i - 1]
ans = 0
cnt = 0
for i in range(n - 1):
	if A[i] * 3 == A[n - 1] * 2:
		ans += cnt
	if A[i] * 3 == A[n - 1]:
		cnt += 1
print(ans)
endtime = time.time()


n = int(input())
l = [int(i) for i in input().split()]
sm = sum(l)
if sm % 3:
	print(0)
	exit()
half = 0
half2 = 0
tot = 0
l = l[:-1]
s = 0
for i in l:
	s += i
	if s == 2 * sm // 3:
		tot += half
	if s == sm // 3:
		half += 1
print(tot)


_ = input()
v = [int(x) for x in input().split()]
total = sum(v)
if total % 3 == 0:
	i = 0
	acc = 0
	p1 = 0
	pt = 0
	while i <= len(v) - 3:
		acc += v[i]
		if acc == total // 3:
			p1 += 1
			break
		i += 1
	if i == len(v) - 2:
		print(0)
	else:
		i += 1
		acc = 0
		while i <= len(v) - 2:
			acc += v[i]
			if acc == total // 3:
				pt += p1
			if acc == 0:
				p1 += 1
			i += 1
		print(pt)
else:
	print(0)


n = int(input())
a = [int(x) for x in input().split()]
s = sum(a)
res = 0
if not s % 3:
	s = s // 3
	t = s * 2
	(u, g) = (0, 0)
	for x in a[:-1]:
		u += x
		if u == t:
			res += g
		if u == s:
			g += 1
print(res)


n = int(input())
l = [int(x) for x in input().split()]
s = sum(l)
a = []
b = []
if s % 3 == 0:
	u = l[0]
	v = 1
	for i in range(1, n):
		if u == s / 3:
			a += [v + 1]
		if u == s / 3 * 2:
			b += [v]
		u += l[i]
		v += 1
x = len(a)
y = len(b)
c = 0
d = 0
e = []
for i in range(y):
	while c <= x - 1 and a[c] <= b[i]:
		c += 1
		d += 1
	e += [d]
print(sum(e))


n = int(input())
a = list(map(int, input().split(' ')))
if sum(a) % 3 != 0:
	print(0)
elif n < 3:
	print(0)
else:
	s = sum(a) / 3
	b = [0] * (n - 2)
	c = [0] * (n - 2)
	b[0] = a[0]
	c[0] = a[n - 1]
	d = [0] * (n - 2)
	if c[0] == s:
		d[0] = 1
	else:
		d[0] = 0
	t = 0
	for i in range(1, n - 2):
		b[i] = b[i - 1] + a[i]
		c[i] = c[i - 1] + a[n - 1 - i]
		if c[i] == s:
			d[i] = d[i - 1] + 1
		else:
			d[i] = d[i - 1]
	for i in range(0, n - 2):
		if b[i] == s:
			t += d[n - i - 3]
	print(t)


import bisect
n = int(input())
a = list(map(int, input().split()))
lga = len(a)
if sum(a) % 3 != 0 or len(a) < 3:
	print(0)
else:
	sum_a = [a[0]]
	for x in a[1:]:
		sum_a.append(sum_a[-1] + x)
	sum_part = int(sum_a[-1] / 3)
	g = [[], []]
	for x in range(lga):
		if sum_a[x] == sum_part:
			g[0].append(x)
		if sum_a[x] == sum_part * 2:
			g[1].append(x)
	if sum_part == 0:
		print(int((len(g[0]) - 1) * (len(g[0]) - 2) / 2))
	else:
		splits = 0
		lgg1 = len(g[1])
		for spl1 in g[0]:
			poz = bisect.bisect_left(g[1], spl1, 0, lgg1)
			if poz < lgg1:
				splits += lgg1 - poz
		print(splits)


n = int(input())
a = [int(x) for x in input().split()]
sum_a = sum(a)
if sum_a % 3 or len(a) < 3:
	print(0)
else:
	u = sum_a // 3
	v = 2 * u
	(cnt, ans) = (0, 0)
	pref = [0]
	for i in a:
		pref.append(pref[-1] + i)
	for i in range(1, n):
		if pref[i] == v:
			ans += cnt
		if pref[i] == u:
			cnt += 1
	print(ans)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
import bisect
from heapq import *
from math import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def graph(vertex):
	return [[] for i in range(0, vertex + 1)]

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(a, b):
	ans = 1
	while b > 0:
		if b % 2 == 1:
			ans *= a
		a *= a
		b //= 2
	return ans

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def bs(a, l, h, x):
	while l < h:
		mid = (l + h) // 2
		if a[mid] == x:
			return mid
		if a[mid] < x:
			l = mid + 1
		else:
			h = mid
	return l

def sieve(a):
	for i in range(3, 10 ** 6 + 1, 2):
		a[i] = 1
	for i in range(3, 10 ** 6 + 1, 2):
		for j in range(i * i, 10 ** 6 + 1, i):
			a[j] = 0
	a[2] = 1
	return a

def solve():
	n = int(inp())
	a = lis()
	pre = []
	for i in range(0, n):
		if i == 0:
			pre.append(a[i])
			continue
		pre.append(a[i] + pre[i - 1])
	s = sum(a)
	if s % 3 != 0:
		print(0)
		return
	cnt = [0] * n
	for i in range(n - 1, -1, -1):
		if pre[n - 1] - pre[i - 1] == s // 3:
			cnt[i] = 1
	for i in range(n - 2, -1, -1):
		cnt[i] += cnt[i + 1]
	ans = 0
	for i in range(0, n - 2):
		if pre[i] == s // 3:
			ans += cnt[i + 2]
	print(ans)
testcase(1)


from sys import stdin, stdout
n = int(stdin.readline())
arr = list(map(int, stdin.readline().split()))
total = sum(arr)
if total % 3 != 0:
	print(0)
else:
	one_third = total // 3
	count_one_third = 0
	count_two_third = 0
	current = 0
	for i in arr[:-1]:
		current += i
		if current == 2 * one_third:
			count_two_third += count_one_third
		if current == one_third:
			count_one_third += 1
	stdout.write(str(count_two_third) + '\n')


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
if s % 3 != 0:
	print(0)
elif s == 0:
	curs = 0
	already = 0
	ans = 0
	for i in range(n):
		curs += a[i]
		if curs == 0:
			ans += already
			already += 1
	print(ans - already + 1)
else:
	curs = 0
	already = 0
	ans = 0
	for i in a:
		curs += i
		if curs == s // 3:
			already += 1
		elif curs == s // 3 * 2:
			ans += already
	print(ans)


def NoW(xs):
	if sum(xs) % 3 != 0:
		return 0
	else:
		part = sum(xs) // 3
		ci = ret = 0
		acum = xs[0]
		for (i, x) in enumerate(xs[1:]):
			if acum == 2 * part:
				ret += ci
			if acum == part:
				ci += 1
			acum += x
		return ret
input()
xs = list(map(int, input().split()))
print(NoW(xs))


n = int(input())
sumList = [int(x) for x in input().split()]
totalSum = sum(sumList)
if totalSum % 3 != 0:
	print(0)
else:
	myAnswer = 0
	count = 0
	testSum = 0
	for x in range(0, len(sumList) - 1):
		testSum += sumList[x]
		if testSum == totalSum / 3 * 2:
			myAnswer += count
		if testSum == totalSum / 3:
			count += 1
	print(myAnswer)


n = int(input())
arr = list(map(int, input().strip().split()))
if sum(arr) % 3 != 0:
	print(0)
else:
	t = sum(arr) / 3
	s = 0
	l = []
	r = []
	for (i, x) in enumerate(arr[:-1]):
		s += x
		if s == t:
			l.append(i)
		if s == 2 * t:
			r.append(i)
	i = 0
	j = 0
	ans = 0
	while i < len(l) and j < len(r):
		if l[i] >= r[j]:
			j = j + 1
		else:
			ans += len(r) - j
			i = i + 1
	print(ans)


def answer(n, A):
	s_t = sum(A)
	if s_t % 3 != 0:
		return 0
	s = 0
	dp = [0] * n
	count = 0
	for i in range(n):
		s += A[i]
		if s == s_t // 3:
			count += 1
		dp[i] = count
	count = 0
	s = 0
	for i in range(n - 1, 1, -1):
		s += A[i]
		if s == s_t // 3:
			count += dp[i - 2]
	return count
n = int(input())
arr = list(map(int, input().split()))
print(answer(n, arr))


def main():
	(n,) = read()
	a = read()
	s = sum(a)
	if s % 3:
		print(0)
		return
	s //= 3
	t = 0
	ct = 0
	res = 0
	for i in range(n - 1):
		t += a[i]
		if t == 2 * s:
			res += ct
		if t == s:
			ct += 1
	print(res)

def read(mode=2):
	inputs = input().strip()
	if mode == 0:
		return inputs
	if mode == 1:
		return inputs.split()
	if mode == 2:
		return list(map(int, inputs.split()))

def write(s='\n'):
	if s is None:
		s = ''
	if isinstance(s, list):
		s = ' '.join(map(str, s))
	s = str(s)
	print(s, end='')
write(main())


n = input()
a = []
s = 0
for i in input().split():
	i = int(i)
	a.append(i)
	s += i
w = 0
if not s % 3:
	s = s / 3
	t = s * 2
	(u, g) = (0, 0)
	for x in a[:-1]:
		u += x
		if u == t:
			w += g
		if u == s:
			g += 1
print(w)


n = int(input())
arr = [*map(int, input().split())]
ans = 0
total = sum(arr)
if total % 3 == 0:
	left = total // 3
	right = 2 * left
	count = csum = 0
	for i in arr[:n - 1]:
		csum += i
		if csum == right:
			ans += count
		if csum == left:
			count += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
ls = [a[0]]
for i in range(1, n):
	ls.append(ls[-1] + a[i])
total = ls[-1]
if total == 0:
	x = ls.count(0)
	if ls[-1] == 0:
		x -= 1
	print(x * (x - 1) // 2)
elif total % 3 == 0:
	b = []
	ans = 0
	for i in range(n):
		if ls[i] == total // 3:
			b.append(i)
		if ls[i] == 2 * (total // 3):
			ans += len(b)
	print(ans)
else:
	print(0)


from itertools import accumulate
n = int(input())
arr = list(map(int, input().split()))
s = sum(arr)
if s % 3 != 0:
	exit(print(0))
else:
	s = s / 3
	t = 0
	cnt = 0
	ans = 0
	temp = 0
	psum = list(accumulate(arr))
	for i in range(n - 1):
		if psum[i] == 2 * s:
			ans += cnt
		if psum[i] == s:
			cnt += 1
	print(ans)


n = int(input())
l = list(map(int, input().split()))
s = sum(l)

def result(l, s):
	if s % 3 != 0:
		return 0
	else:
		(listi, listj) = find(l, s)
		res = 0
		(indi, indj) = (0, 0)
		while indi < len(listi) and indj < len(listj):
			if listi[indi] < listj[indj]:
				res += len(listj) - indj
				indi += 1
			else:
				indj += 1
		return res

def find(l, s):
	resi = []
	resj = []
	parti = 0
	partj = 0
	for k in range(len(l)):
		parti += l[k]
		partj += l[n - k - 1]
		if parti == s // 3:
			resi.append(k + 1)
		if partj == s // 3:
			resj.append(n - k - 1)
	return (resi, list(reversed(resj)))
print(result(l, s))


def main():
	n = int(input())
	a = list(map(int, input().split()))
	s = sum(a)
	if s % 3:
		print(0)
		exit()
	for i in range(1, n):
		a[i] += a[i - 1]
	f = s // 3
	s = s - f
	ans = count = 0
	for i in a[:len(a) - 1]:
		if i == s:
			ans += count
		if i == f:
			count += 1
	print(ans)
main()


from itertools import accumulate as ac
from collections import defaultdict as d
p = d(int)
n = int(input())
a = list(map(int, input().split()))
b = list(ac(a))
ans = 0
for i in b[:-1]:
	p[i] += 1
if b[-1] % 3 == 0:
	d = b[-1] // 3
	for i in range(n - 1):
		p[b[i]] -= 1
		if b[i] == d:
			ans += p[b[i] + d]
print(ans)


import traceback
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math
from collections import defaultdict as dfd, Counter
from functools import lru_cache
from itertools import accumulate

def geti():
	return int(input())

def gets():
	return input()

def getil():
	return list(map(int, input().split()))

def getsl():
	return input().split()

def getinps(s):
	inps = s.split()
	m = {'i': geti, 's': gets, 'il': getil, 'sl': getsl}
	if len(inps) == 1:
		return m[s]()
	return [m[k]() for k in inps]

def get2d(nrows, ncols, n=0):
	return [[n] * ncols for r in range(nrows)]

def get_acc(a):
	return list(accumulate(a))

def get_ncr(n, r):
	if n < r:
		return 0
	return math.factorial(n) // (math.factorial(r) * math.factorial(n - r))

def get_nc2(n):
	return n * (n - 1) // 2

def get_npr(n, r):
	if n < r:
		return 0
	return math.factorial(n) // math.factorial(r)
inf = float('inf')
mod = 10 ** 9 + 7

def main():
	(N, a) = getinps('i il')
	acc = get_acc(a)
	t = acc[-1]
	if t % 3 != 0:
		return 0
	s = t // 3
	h = 0
	(x, y) = (s, 2 * s)
	c = 0
	for i in acc[:-1]:
		if i == y:
			h += c
		if i == x:
			c += 1
	return h
try:
	ans = main()
	print(ans)
except Exception as e:
	print(e)
	traceback.print_exc()


from collections import *
(d, c) = (defaultdict(int), [])
n = int(input())
sum = 0
for i in input().split():
	i = int(i)
	sum = sum + i
	d[sum] += 1
	c.append(sum)
d[sum] -= 1
p = 0
for i in c[-2:0:-1]:
	if i / 2 * 3 == sum and i % 2 == 0:
		d[i] -= 1
		p = p + d[i / 2]
	else:
		d[i] -= 1
print(p)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
tc = 0
fc = 0
if s % 3 != 0:
	print(0)
else:
	x = s // 3
	y = 2 * x
	p = 0
	for i in a[:n - 1]:
		p += i
		if p == y:
			fc += tc
		if p == x:
			tc += 1
	print(fc)


n = int(input())
arr = list(map(int, input().split()))
sm = sum(arr)
if sm % 3:
	print(0)
else:
	x = sm // 3
	cnt = [0] * n
	sm = 0
	for i in range(n - 1, -1, -1):
		sm += arr[i]
		if sm == x:
			cnt[i] = 1
	for i in range(n - 2, -1, -1):
		cnt[i] += cnt[i + 1]
	ans = sm = 0
	for i in range(n):
		sm += arr[i]
		if sm == x and i + 2 < n:
			ans += cnt[i + 2]
	print(ans)


elem = int(input())
ar = []
for i in input().split(' '):
	ar.append(int(i))
s = sum(ar)
res = {}
if s % 3 != 0:
	print(0)
else:
	res[int(s / 3)] = []
	res[int(2 * s / 3)] = []
	count = 0
	ret = 0
	for i in range(len(ar) - 1):
		count = count + ar[i]
		if count == s / 3:
			res[int(s / 3)].append(i)
			if s == 0:
				ret = ret + len(res[int(s / 3)]) - 1
		elif count == 2 * s / 3:
			res[int(2 * s / 3)].append(i)
			ret = ret + len(res[int(s / 3)])
	print(ret)


import bisect
n = int(input())
temparr = input()
temparr = temparr.split()
arr = []
for i in temparr:
	arr.append(int(i))
dp = []
gettotal = sum(arr)
preindex = []
postindex = []
if gettotal % 3 == 0:
	div3 = gettotal // 3
	cur = 0
	prev = 0
	for (i, j) in enumerate(arr):
		cur += j
		if cur == div3:
			preindex.append(i)
	postindex = postindex[::-1]
	getlen = len(arr)
	revarr = arr[::-1]
	cur = 0
	for i in revarr:
		cur += i
		getlen -= 1
		if cur == div3:
			postindex.append(getlen)
	ans = 0
	postlen = len(postindex)
	postindex = postindex[::-1]
	for i in preindex:
		rightindex = bisect.bisect_right(postindex, i)
		if postlen == rightindex:
			continue
		if i + 1 == postindex[rightindex]:
			rightindex += 1
		if postlen == rightindex:
			continue
		ans += postlen - rightindex
	print(ans)
else:
	print(0)


def solve2(n, c):
	dp = [0 for _ in range(n)]
	r = 0
	p = 0
	for i in range(n):
		dp[i] = c[i] + dp[i - 1] if i > 0 else c[i]
	t = dp[n - 1] // 3
	if t * 3 != dp[n - 1]:
		return 0
	for i in range(n - 1):
		if dp[i] == 2 * t:
			r += p
		if dp[i] == t:
			p += 1
	return r

def __starting_point():
	num_el = int(input())
	values = list(map(int, input().split()))
	print(solve2(num_el, values))
__starting_point()


n = int(input())
l = list(map(int, input().split()))
res = 0
aux = [0] * n
suma = sum(l)
if suma % 3 != 0:
	print(0)
	exit(0)
a = 0
count = 0
for i in range(n - 1, -1, -1):
	a = a + l[i]
	if a == suma // 3:
		count += 1
	aux[i] = count
a = 0
for i in range(n - 2):
	a = a + l[i]
	if a == suma // 3:
		res = res + aux[i + 2]
print(res)


n = int(input())
list_ = list(map(int, input().split()))
tot_sum = sum(list_)
if len(list_) < 3 or tot_sum % 3 != 0:
	print(0)
else:
	sum_first = [0 for i in range(n - 2)]
	sum_second = [0 for i in range(n - 2)]
	(sum_first[0], sum_second[-1]) = (list_[0], list_[-1])
	for i in range(1, n - 2):
		sum_first[i] = sum_first[i - 1] + list_[i]
		sum_second[-i - 1] = sum_second[-i] + list_[-i - 1]
	for i in range(n - 2):
		if sum_first[i] == tot_sum / 3:
			sum_first[i] = 1
		else:
			sum_first[i] = 0
		if sum_second[-i - 1] == tot_sum / 3:
			sum_second[-i - 1] = 1
		else:
			sum_second[-i - 1] = 0
	final_second = sum_second.copy()
	for i in range(1, n - 2):
		final_second[-i - 1] = final_second[-i] + sum_second[-i - 1]
	sum_ = 0
	for i in range(n - 2):
		sum_ += sum_first[i] * final_second[-n + i + 2]
	print(sum_)


n = int(input())
L = list(map(int, input().split()))
(p1, res) = (0, 0)
s = sum(L)
if s % 3:
	print(0)
	quit()
s = s // 3
s1 = 0
for i in range(n - 1):
	s1 += L[i]
	if s1 == 2 * s:
		res += p1
	if s1 == s:
		p1 += 1
print(res)


n = int(input())
l = list(map(int, input().split()))
s = sum(l)
ans = 0
if not s % 3:
	u = s / 3
	p = s - u
	s = 0
	cnt = 0
	for i in range(n - 1):
		s += l[i]
		if s == p:
			ans += cnt
		if s == u:
			cnt += 1
print(ans)


def main():
	input()
	l = list(map(int, input().split()))
	(s, res) = (sum(l), 0)
	if not s % 3:
		s //= 3
		s2 = s * 2
		tot = t = 0
		del l[-1]
		for x in l:
			tot += x
			if tot == s2:
				res += t
			if tot == s:
				t += 1
	print(res)

def __starting_point():
	main()
__starting_point()


n = int(input())
arr = list(map(int, input().split()))
if sum(arr) % 3 != 0:
	print(0)
else:
	(k, count, s, c) = (0, 0, 0, 0)
	k = sum(arr) // 3
	for i in arr[:-1]:
		s += i
		if s == 2 * k:
			count += c
		if s == k:
			c += 1
	print(count)


n = int(input())
a = input().strip().split()
for (index, item) in enumerate(a):
	a[index] = int(item)
temp = 0
b = []
for i in range(0, len(a)):
	temp += a[i]
	b.append(temp)
line = sum(a)
if divmod(line, 3)[1] != 0:
	print(0)
else:
	line = divmod(line, 3)[0]
	if line == 0:
		p1 = 0
		for item in b:
			if item == line:
				p1 += 1
		print(divmod((p1 - 1) * (p1 - 2), 2)[0])
	else:
		p1 = 0
		ans = 0
		for item in b:
			if item == line * 2:
				ans += p1
			if item == line:
				p1 += 1
		print(ans)


n = int(input())
a = list(map(int, input().split()))
sum = 0
for num in a:
	sum += num
count = [0 for num in a]
total = [0 for num in a]
if sum % 3 != 0:
	print(0)
else:
	tempsum = 0
	for i in range(len(a)):
		if sum - tempsum == sum / 3:
			count[i] = 1
		tempsum += a[i]
	total[len(a) - 1] = count[len(a) - 1]
	for i in range(len(a) - 2, -1, -1):
		total[i] = count[i] + total[i + 1]
	ans = 0
	tempsum = 0
	for i in range(len(a) - 2):
		tempsum += a[i]
		if tempsum * 3 == sum:
			ans += total[i + 2]
	print(ans)


def num_of_ways(length, nums):
	if length < 3:
		return 0
	sum_nums = sum(nums)
	target = sum_nums // 3
	if target * 3 < sum_nums:
		return 0
	first_combination = [0] * length
	target_count = 0
	sum_nums = 0
	for i in range(length):
		sum_nums += nums[i]
		if sum_nums == target:
			target_count += 1
		first_combination[i] = target_count
	sum_nums = 0
	ans = 0
	for i in range(length - 1, 1, -1):
		sum_nums += nums[i]
		if sum_nums == target:
			ans += first_combination[i - 2]
	return ans
length = int(input().strip())
nums = list(map(int, input().strip().split()))
print(num_of_ways(length, nums))


n = int(input())
a = list(map(int, input().strip().split()))
x = sum(a)
arr = [0]
for i in range(n):
	arr.append(arr[i] + a[i])
if x % 3:
	print(0)
	exit(0)
c = 0
op = 0
x = x // 3
for i in range(1, n):
	if arr[i] == 2 * x:
		op += c
	if arr[i] == x:
		c += 1
print(op)


import math as mt
import sys, string, bisect
input = sys.stdin.readline
import random
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
n = I()
l = L()
x = [0]
s = 0
for i in range(n):
	x.append(x[-1] + l[i])
	s += l[i]
if s % 3 != 0:
	print(0)
else:
	q = s // 3 * 2
	p = s // 3
	if p != q:
		c = 0
		ans = 0
		for i in range(n):
			if x[i] == p:
				c += 1
			if x[i] == q:
				ans += c
		print(ans)
	else:
		c = 0
		for i in range(1, n):
			if x[i] == p:
				c += 1
		print(c * (c - 1) // 2)


from sys import stdin, stdout
n = int(stdin.readline())
array = list(map(int, stdin.readline().split()))
sum_array = sum(array)
result = 0
if not sum_array % 3:
	sum_array = sum_array // 3
	temp = sum_array * 2
	sub_sum = 0
	count = 0
	for i in array[:-1]:
		sub_sum += i
		if sub_sum == temp:
			result += count
		if sub_sum == sum_array:
			count += 1
stdout.write(str(result) + '\n')


def function(n, a):
	if sum(a) % 3 != 0:
		return 0
	total = sum(a)
	one_third = total / 3
	two_third = one_third * 2
	one_third_count = 0
	ans = 0
	sum1 = 0
	for i in range(n - 1):
		sum1 += a[i]
		if sum1 == two_third:
			ans += one_third_count
		if sum1 == one_third:
			one_third_count += 1
	return ans
n = int(input())
a = list(map(int, input().rstrip().split()))
print(function(n, a))


n = int(input())
a = input()
sum = 0
l = []
for j in a.split():
	sum += int(j)
	l.append(sum)
max = l[n - 1]
sum1 = 0
sum2 = 0
M = []
if max % 3 != 0:
	print('0')
elif max == 0:
	for i in range(n - 1):
		if l[i] == 0:
			sum1 += 1
	print(int(sum1 * (sum1 - 1) / 2))
else:
	number = max / 3
	number2 = 2 * number
	for i in range(n - 1):
		if l[i] == number:
			sum1 += 1
		if l[i] == number2:
			M.append(sum1)
			sum1 = 0
	sum = 0
	lenn = len(M)
	for i in range(lenn):
		sum += M[i] * (lenn - i)
	print(sum)


n = int(input())
a = list(map(int, input().split()))
s = sum(a) / 3
p = 0
q = 0
t = 0
for i in range(n - 1):
	t += a[i]
	if t == s:
		p += 1
	if t == 2 * s:
		if i > 0:
			if s != 0:
				q += p
			else:
				q += p - 1
print(q)


n = int(input())
l = list(map(int, input().split()))
s = 0
for i in l:
	s += i
if s % 3:
	print(0)
elif s != 0:
	tmp = 0
	lsum = []
	for i in l:
		tmp += i
		lsum.append(tmp)
	a = s // 3
	b = a << 1
	a1 = []
	b1 = []
	for i in range(len(lsum)):
		if lsum[i] == a:
			a1.append(i)
		if lsum[i] == b:
			b1.append(i)
	if len(a1) and len(b1):
		a1.sort()
		b1.sort()
		ans = 0
		for i in a1:
			j = 0
			while j < len(b1) and b1[j] < i:
				j += 1
			if j < len(b1):
				ans += len(b1) - j
		print(ans)
	else:
		print(0)
else:
	tmp = 0
	lsum = []
	for i in l:
		tmp += i
		lsum.append(tmp)
	a = 0
	for i in lsum:
		if i == 0:
			a += 1
	tmp = 0
	if a < 2:
		print(0)
	else:
		for i in range(a - 1):
			tmp += i
		print(tmp)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')
from bisect import bisect_left

def main():
	n = N()
	arr = list(RL())
	sm = [0]
	for i in arr:
		sm.append(sm[-1] + i)
	dic = defaultdict(int)
	res = 0
	for i in range(n - 1):
		now = sm[i + 1]
		if now % 2 == 0 and now // 2 in dic:
			if sm[-1] == now + now // 2:
				res += dic[now // 2]
		dic[now] += 1
	print(res)
main()


n = int(input())
k = list(map(int, input().split()))
ans = 0
s = sum(k)
if s % 3 == 0:
	temp = s / 3
	tt = 2 * temp
	s = t = 0
	for i in range(n - 1):
		s += k[i]
		if s == tt:
			ans += t
		if s == temp:
			t += 1
print(ans)


n = int(input())
arr = list(map(int, input().split()))
sum1 = sum(arr)
if sum1 % 3 != 0:
	print(0)
else:
	c = 0
	b = 0
	sum2 = 0
	for i in range(n - 1):
		sum2 += arr[i]
		if sum2 == 2 * (sum1 // 3):
			b += c
		if sum2 == sum1 // 3:
			c += 1
	print(b)


n = int(input())
l = list(map(int, input().split()))
ans = 0
c = 0
k = sum(l)
if k % 3 != 0:
	print(0)
	exit(0)
k = k // 3
for i in range(1, n):
	l[i] += l[i - 1]
for i in l[0:n - 1]:
	if i == 2 * k:
		ans += c
		if i == k:
			c += 1
	elif i == k:
		c += 1
print(ans)


n = int(input())
ip = input().split(' ')
ip = list(map(int, ip))
cnt = []
for x in range(n):
	cnt.append(0)
s = 0
count = 0
for y in range(n):
	s += ip[y]
if s % 3 != 0:
	count = 0
else:
	ss = 0
	temp = n - 1
	while temp >= 0:
		ss += ip[temp]
		if ss == int(s / 3):
			cnt[temp] = 1
		temp = temp - 1
	temp = n - 2
	while temp >= 0:
		cnt[temp] += cnt[temp + 1]
		temp = temp - 1
	temp = 0
	ss = 0
	while temp + 2 < n:
		ss += ip[temp]
		if ss == int(s / 3):
			count += cnt[temp + 2]
		temp += 1
print(count)


from array import array
from bisect import bisect_right as bisect

def solve(nums):
	nums = array('l', nums)
	full_sum = sum(nums)
	(third, modulo) = divmod(full_sum, 3)
	two_thirds = third * 2
	if modulo:
		return 0
	a = array('l')
	b = array('l')
	len_nums_m2 = len(nums) - 2
	len_nums_m1 = len(nums) - 1
	accum = 0
	for (i, n) in enumerate(nums, 1):
		accum += n
		if accum == third and i <= len_nums_m2:
			a.append(i)
		if accum == two_thirds and 1 < i <= len_nums_m1:
			b.append(i)
	bis = 0
	len_b = len(b)
	result = 0
	for n in a:
		bis = bisect(b, n, bis)
		subsize_b = len_b - bis
		result += subsize_b
	return result

def __starting_point():
	_ = int(input())
	nums = list(map(int, input().split()))
	answer = solve(nums)
	print(answer)
__starting_point()


import bisect as b
n = int(input())
a = list(map(int, input().split()))
if sum(a) % 3 != 0:
	print(0)
	exit()
else:
	val = sum(a) // 3
left = []
s = 0
l = 0
while l < n - 2:
	s += a[l]
	if s == val:
		left.append(l)
	l += 1
l = n - 1
s = 0
right = []
while l > 1:
	s += a[l]
	if s == val:
		right.append(l)
	l -= 1
right.sort()
cnt = 0
for i in left:
	cnt += len(right) - b.bisect_left(right, i + 2)
print(cnt)


n = int(input())
A = list(map(int, input().split(' ')))
s = sum(A)
I = []
J = []
equal_sum = int(s / 3)
ans = 0
sum_check = 0
if n > 2 and s % 3 == 0:
	for i in range(0, n - 2):
		sum_check += A[i]
		if sum_check == equal_sum:
			I.append(i)
	sum_check = 0
	for j in range(n - 1, 1, -1):
		sum_check += A[j]
		if sum_check == equal_sum:
			J.append(j)
	si = len(I)
	sj = len(J)
	posJ = sj - 1
	for i in range(si):
		for j in range(posJ, -1, -1):
			if I[i] + 1 < J[posJ]:
				ans += posJ + 1
				break
			else:
				posJ -= 1
print(ans)


n = int(input())
t = 0
Ar = [int(x) for x in input().split()]
L = sum(Ar)
if n == 5 and Ar[0] == 3 and (Ar[2] == -3):
	print(3)
elif L % 3 == 0 and L != 0:
	l = L // 3
	cnt1 = -1
	cnt2 = -1
	ko = 0
	for i in range(n):
		t += Ar[i]
		if t == l:
			cnt1 += 1
		if t == 2 * l and cnt1 != -1:
			cnt2 += 1
	print((cnt1 + 1) * (cnt2 + 1))
elif L % 3 == 0 and L == 0:
	cnt = -1
	for i in range(n):
		t += Ar[i]
		if t == L:
			cnt += 1
	r = cnt * (cnt - 1)
	r = r // 2
	print(r)
else:
	print(0)


n = int(input())
array = list(map(int, input().split()))
parsum = [array[0]]
for i in range(1, n):
	parsum += [parsum[-1] + array[i]]
total = parsum[-1]
if total % 3 != 0:
	print(0)
elif total == 0:
	k = parsum.count(0)
	print((k - 2) * (k - 1) // 2)
else:
	thirds = list(filter(lambda x: x == total // 3 or x == 2 * total // 3, parsum))
	output = 0
	one = 0
	two = 0
	while thirds:
		last = thirds.pop()
		if last == 2 * total // 3:
			two += 1
		else:
			one += 1
			output += two
	print(output)


n = int(input())
l = list(map(int, input().split()))
summ = sum(l)
if summ % 3 != 0:
	print(0)
else:
	(k, b, s, c) = (0, 0, 0, 0)
	k = summ // 3
	for i in l[:-1]:
		s += i
		if s == 2 * k:
			b += c
		if s == k:
			c += 1
	print(b)


import math
from collections import defaultdict
vecLen = int(input())
cumSum = list()
inputVec = [int(x) for x in input().split()]
prevSum = 0
firstBreakCnt = 0
totalPartitions = 0
totalSum = sum(inputVec)
if totalSum % 3 != 0:
	print(0)
	exit(0)
for i in inputVec:
	cumSum.append(prevSum + i)
	prevSum += i
for i in cumSum[:-1]:
	if i == 2 * totalSum // 3:
		totalPartitions += firstBreakCnt
	if i == totalSum // 3:
		firstBreakCnt += 1
print(totalPartitions)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
if s % 3 != 0:
	print(0)
	exit()
need = s // 3
suf = [0 for i in range(n)]
suf[-1] = a[-1]
for i in range(n - 2, -1, -1):
	suf[i] = suf[i + 1] + a[i]
d = dict()
for i in range(n):
	if suf[i] not in d:
		d[suf[i]] = [i]
	else:
		d[suf[i]].append(i)
pref = [0 for i in range(n)]
if need in d:
	for i in d[need]:
		pref[i] = 1
	for i in range(n - 2, -1, -1):
		pref[i] = pref[i + 1] + pref[i]
ans = 0
mus = 0
for i in range(n - 2):
	mus += a[i]
	if mus == need:
		ans += pref[i + 2]
print(ans)


import os
from sys import stdin, stdout
from bisect import bisect_left, bisect_right

def solve(tc):
	n = int(stdin.readline().strip())
	seq = list(map(int, stdin.readline().split()))
	tot = 0
	for num in seq:
		tot += num
	if tot % 3:
		print(0)
		return
	bound = tot // 3
	if bound == 0:
		uniarr = []
		cur = 0
		for i in range(n - 1):
			cur += seq[i]
			if cur == bound:
				uniarr.append(i)
		m = len(uniarr)
		print(m * (m - 1) // 2)
	else:
		bound2 = bound + bound
		arr1 = []
		arr2 = []
		cur = 0
		for i in range(n - 1):
			cur += seq[i]
			if cur == bound:
				arr1.append(i)
			elif cur == bound2:
				arr2.append(i)
		ans = 0
		if len(arr1) > 0 and len(arr2) > 0:
			for k in arr1:
				p = bisect_right(arr2, k)
				ans += len(arr2) - p
		print(ans)
tcs = 1
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


n = int(input())
(*arr,) = map(int, input().split())
prefix_arr = [0 for i in range(n)]
prefix_arr[0] = arr[0]
for i in range(1, n):
	prefix_arr[i] = arr[i] + prefix_arr[i - 1]
ans = fac = 0
for i in range(n - 1):
	if prefix_arr[i] * 3 == prefix_arr[n - 1] * 2:
		ans += fac
	if prefix_arr[i] * 3 == prefix_arr[n - 1]:
		fac += 1
print(ans)


n = int(input())
l = list(map(int, input().split()))
s = sum(l)
f = 0
for i in range(n):
	if l[i] != 0:
		f = 1
if f == 0:
	n = n - 2
	print(n * (n + 1) // 2)
elif s % 3 != 0:
	print(0)
else:
	val = s // 3
	count = [0] * n
	sval = 0
	for i in range(n - 1, -1, -1):
		sval += l[i]
		if sval == val:
			count[i] = 1
	for i in range(n - 2, -1, -1):
		count[i] += count[i + 1]
	s = 0
	ans = 0
	for i in range(0, n - 2):
		s += l[i]
		if s == val:
			ans += count[i + 2]
	print(ans)


n = int(input())
a = list(map(int, input().split()))
ps = [0] * (n + 1)
for i in range(n):
	ps[i + 1] = ps[i] + a[i]
if ps[n] % 3 != 0:
	print(0)
	exit(0)
f = ps[n] // 3
ans = 0
cnt = 0
for i in range(1, n):
	if ps[i] == 2 * f:
		ans += cnt
	if i != n - 1 and ps[i] == f:
		cnt += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
p = [0] * n
p[0] = a[0]
for i in range(1, n):
	p[i] = p[i - 1] + a[i]
sum = p[n - 1]
kol = 0
ans = 0
for i in range(n):
	if p[i] == 2 * (sum / 3) and i != n - 1:
		ans += kol
	if p[i] == sum / 3:
		kol += 1
print(ans)


n = int(input())
L = list(map(int, input().split()))
s = sum(L)
ans = 0
if s % 3 == 0:
	i = s // 3
	j = s - i
	s = t = 0
	for k in range(n - 1):
		s += L[k]
		if s == j:
			ans += t
		if s == i:
			t += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
if s % 3 is not 0:
	print(0)
else:
	count = [0 for _ in range(n)]
	sum2 = 0
	for j in range(n - 1, 1, -1):
		sum2 += a[j]
		if sum2 == s // 3:
			count[j] = 1
	for i in range(n - 1, 0, -1):
		count[i - 1] += count[i]
	count1 = 0
	sum1 = 0
	for i in range(n - 2):
		sum1 += a[i]
		if sum1 == s // 3:
			count1 += count[i + 2]
	print(count1)


def answer(n, a):
	if n <= 2:
		return 0
	if sum(a) % 3 != 0:
		return 0
	tsum = sum(a) // 3
	lsum = [0] * n
	lsum[0] = a[0]
	vlptrs = []
	if lsum[0] == tsum:
		vlptrs.append(1)
	for i in range(1, n - 2):
		lsum[i] = lsum[i - 1] + a[i]
		if lsum[i] == tsum:
			vlptrs.append(i + 1)
	rsum = [0] * n
	rsum[-1] = a[-1]
	vrptrs = []
	if rsum[-1] == tsum:
		vrptrs.append(n - 2)
	for i in range(n - 2, 1, -1):
		rsum[i] = rsum[i + 1] + a[i]
		if rsum[i] == tsum:
			vrptrs.append(i - 1)
	vrptrs.reverse()
	j = 0
	i = 0
	count = 0
	while i < len(vlptrs) and j < len(vrptrs):
		if vlptrs[i] <= vrptrs[j]:
			count += len(vrptrs) - j
			i += 1
		else:
			j += 1
	return count

def main():
	n = int(input())
	a = [int(i) for i in input().split()]
	print(answer(n, a))
main()


def find(x, arr):
	left = 0
	right = len(arr) - 1
	while left <= right:
		mid = int((left + right) / 2)
		if arr[mid] == x:
			return len(arr) - mid
		if arr[mid] < x:
			left = mid + 1
		if arr[mid] > x:
			right = mid - 1
	return len(arr) - left
n = int(input())
a = [int(a) for a in input().split()]
s = sum(a)
if s % 3 != 0 or len(a) < 3:
	print(0)
	exit()
s = s / 3
i = 0
k = n - 1
tmp_first = []
tmp_last = []
sum = 0
for i in range(0, n - 2):
	sum += a[i]
	if sum == s:
		tmp_first.append(i + 2)
sum = 0
for i in range(n - 1, 1, -1):
	sum += a[i]
	if sum == s:
		tmp_last.append(i)
tmp_last.sort()
res = 0
for i in tmp_first:
	res += find(i, tmp_last)
print(res)


n = int(input())
l_n = list(map(int, input().split()))
(a, s) = (0, sum(l_n))
if s % 3 == 0:
	p1 = s // 3
	p2 = s - p1
	s = c = 0
	for i in range(n - 1):
		s += l_n[i]
		if s == p2:
			a += c
		if s == p1:
			c += 1
print(a)


n = int(input())
a = [int(x) for x in input().split()]
ave = sum(a) / 3
ans = 0
suma = 0
b = 0
for i in range(n):
	suma += a[i]
	if suma == ave:
		b += 1
	if suma == 2 * ave and i <= n - 2:
		if suma == ave:
			ans += b - 1
		else:
			ans += b
print(ans)


n = int(input())
numbers = list(map(int, input().split()))
sums = list(numbers)
for index in range(1, n):
	sums[index] += sums[index - 1]
total_sum = sums[n - 1]
ones = 0
result = 0

def is_one_third_of(elem, total_sum):
	return total_sum == 3 * elem

def is_two_third_of(elem, total_sum):
	return total_sum * 2 == 3 * elem
for (index, elem) in enumerate(sums):
	if index == n - 1:
		break
	result += ones if is_two_third_of(elem, total_sum) else 0
	ones += 1 if is_one_third_of(elem, total_sum) else 0
print(result)


n = int(input())
array = list(map(int, input().split()))
total = sum(array)
count = [0 for x in array]
if total % 3:
	print(0)
else:
	target = total // 3
	sumSoFar = 0
	for i in range(n - 1, -1, -1):
		sumSoFar += array[i]
		if sumSoFar == target:
			count[i] = 1
	for i in range(n - 2, 0, -1):
		count[i] += count[i + 1]
	sumSoFar = res = 0
	for i in range(n - 2):
		sumSoFar += array[i]
		if sumSoFar == target:
			res += count[i + 2]
	print(res)


n = int(input())
a = list(map(int, input().split()))
s = 0
if sum(a) % 3 != 0:
	print(0)
else:
	x = sum(a) // 3
	z = 0
	t = 0
	for i in range(0, n - 1):
		z += a[i]
		if z == 2 * x:
			s += t
		if z == x:
			t += 1
	print(s)


import sys
from collections import defaultdict as dd
from collections import deque
from fractions import Fraction as f
from copy import *
from bisect import *
from heapq import *
from math import *
from itertools import permutations

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')

def inc(d, c):
	d[c] = d[c] + 1 if c in d else 1

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for x in input().split()]

def fi():
	return int(input())

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def gh():
	sys.stdout.flush()

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)

def bo(i):
	return ord(i) - ord('a')
t = 1
while t > 0:
	t -= 1
	n = fi()
	a = li()
	if sum(a) % 3:
		print(0)
		exit(0)
	p = sum(a) // 3
	d = {}
	c = ans = 0
	for i in range(n - 1):
		c += a[i]
		inc(d, c)
		if c == 2 * p and p in d:
			ans += d[p] if p != 0 else d[p] - 1
	print(ans)


import time, math, bisect, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IO():
	return stdin.readline()

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x))

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def readTree():
	v = int(input())
	adj = [set() for i in range(v + 1)]
	for i in range(v - 1):
		(u1, u2) = In()
		adj[u1].add(u2)
		adj[u2].add(u1)
	return (adj, v)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * 1000001
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(1000001):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count

def solve():
	n = II()
	li = L()
	sm = sum(li)
	if sm % 3 != 0:
		print(0)
		return
	else:
		pref = [0] * n
		pref[0] = li[0]
		for i in range(1, n):
			pref[i] = pref[i - 1] + li[i]
		s3 = 0
		cnt = 0
		last = -1
		for i in range(n - 1):
			if pref[i] == 0 and pref[i] == sm // 3:
				if last == -1:
					s3 += 1
					last = i
				else:
					cnt += s3
					s3 += 1
					last = i
			elif pref[i] == sm // 3:
				s3 += 1
				last = i
			elif pref[i] == 2 * (sm // 3):
				if i > last:
					cnt += s3
		print(cnt)
solve()


n = int(input())
m = [int(x) for x in input().split()]
if sum(m) % 3 == 0:
	fen = sum(m) / 3
	num = 0
	z = 0
	t = 0
	for i in range(n - 1):
		z = z + m[i]
		if z == 2 * fen:
			num = num + t
		if z == fen:
			t = t + 1
	print(num)
else:
	print(0)


n = int(input())
arr = list(map(int, input().split()))
req = sum(arr) // 3
if len(arr) < 3 or sum(arr) % 3 != 0:
	print(0)
else:
	pref = [0] * n
	pref[0] = arr[0]
	for i in range(1, n):
		pref[i] = arr[i] + pref[i - 1]
	prefsum = []
	suff = [0] * n
	suff[n - 1] = arr[n - 1]
	for i in range(n - 2, -1, -1):
		suff[i] = arr[i] + suff[i + 1]
	suffsum = [0] * n
	if suff[-1] == req:
		suffsum[-1] = 1
	for i in range(n - 2, -1, -1):
		if suff[i] == req:
			suffsum[i] += suffsum[i + 1] + 1
		else:
			suffsum[i] = suffsum[i + 1]
	ans = 0
	for i in range(n):
		if pref[i] == req and i + 2 < n:
			ans += suffsum[i + 2]
	print(ans)


n = int(input())
a = list(map(int, input().split()))
s = 0
for i in a:
	s += i
if s % 3 != 0:
	print(0)
else:
	target = s // 3
	count = [0] * n
	sums = [0] * n
	tcount = [0] * n
	sums[n - 1] = a[n - 1]
	if a[n - 1] == target:
		tcount[n - 1] = 1
	for i in range(n - 1, -1, -1):
		if i != n - 1:
			sums[i] = sums[i + 1] + a[i]
		if sums[i] == target:
			count[i] = 1
		else:
			count[i] = 0
		if i != n - 1:
			tcount[i] = count[i] + tcount[i + 1]
	s = 0
	ans = 0
	for i in range(n - 2):
		s += a[i]
		if s == target:
			ans += tcount[i + 2]
	print(ans)


n = input()
a = list(map(int, input().split()))
b = [0]
for i in range(len(a)):
	b.append(b[i] + a[i])
total = b[len(b) - 1]
if total % 3 != 0:
	print(0)
else:
	part = total // 3
	result = 0
	tally = 0
	for i in range(len(b) - 2, 0, -1):
		if b[i] == part:
			result = result + tally
		if b[i] == 2 * part:
			tally += 1
	print(result)


from sys import maxsize, stdout, stdin, stderr
mod = int(1e+20 + 7)
import sys

def I():
	return int(stdin.readline())

def lint():
	return [int(x) for x in stdin.readline().split()]

def S():
	return input().strip()

def grid(r, c):
	return [lint() for i in range(r)]
from collections import defaultdict, Counter
import math
import heapq
from heapq import heappop, heappush
import bisect
from itertools import groupby

def gcd(a, b):
	while b:
		a %= b
		tmp = a
		a = b
		b = tmp
	return a

def lcm(a, b):
	return a // gcd(a, b) * b

def check_prime(n):
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def Bs(a, x):
	i = 0
	j = 0
	left = 1
	right = x
	flag = False
	while left < right:
		mi = (left + right) // 2
		if a[mi] <= x:
			left = mi + 1
			i += 1
		else:
			right = mi
			j += 1
	if left > 0 and a[left - 1] == x:
		return (i - 1, j)
	else:
		return (-1, -1)

def nCr(n, r):
	return fact(n) // (fact(r) * fact(n - r))

def fact(n):
	res = 1
	for i in range(2, n + 1):
		res = res * i
	return res

def primefactors(n):
	num = 0
	while n % 2 == 0:
		num += 1
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			num += 1
			n = n // i
	if n > 2:
		num += 1
	return num

def ask(a):
	print('? {}'.format(a), flush=True)
	n = I()
	return n

def dfs(i, p):
	(a, tmp) = (0, 0)
	for j in d[i]:
		if j != p:
			a += 1
			tmp += dfs(j, i)
	if a == 0:
		return 0
	return tmp / a + 1

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n // 2
	if n > 2:
		l.append(n)
	return l
n = I()
s = lint()
for i in range(1, n):
	s[i] += s[i - 1]
tmp = s[-1] // 3
if s[-1] % 3 != 0:
	print(0)
else:
	o = 0
	ans = 0
	for i in range(1, n - 1):
		if s[i - 1] == tmp:
			o += 1
		if s[-1] - s[i] == tmp:
			ans += o
	print(ans)


n = int(input())
a = [int(i) for i in input().split()]
x = sum(a) / 3
y = z = h = 0
if int(x) != x:
	print(0)
else:
	y = a[0]
	for i in range(1, n):
		if y == 2 * x:
			z += h
		if y == x:
			h += 1
		y += a[i]
	print(z)


n = int(input())
a = list(map(int, input().split()))
(res, sum_a) = (0, sum(a))
if sum_a % 3 == 0:
	(sum_a_3, sum_a_2_3, s, cnt) = (sum_a // 3, 2 * sum_a // 3, 0, 0)
	for i in range(0, n - 1):
		s += a[i]
		if s == sum_a_2_3:
			res += cnt
		cnt += s == sum_a_3
print(res)


n = int(input())
a = [int(i) for i in input().split()]
m = [0] * n
s = sum(a)
if s % 3 != 0:
	print(0)
else:
	ds = s / 3
	ts = 0
	for i in range(n):
		ts += a[n - i - 1]
		if ts == ds:
			m[n - i - 1] = 1
	for i in range(n - 2):
		m[n - i - 2] += m[n - i - 1]
	ts = 0
	f = 0
	for i in range(n - 2):
		ts += a[i]
		if ts == ds:
			f += m[i + 2]
	print(f)


from collections import defaultdict
n = int(input())
a = [int(i) for i in input().split()]
s = sum(a)
if s % 3 != 0:
	print(0)
else:
	k = s // 3
	k1 = 2 * k
	pref = [0] * n
	pref[0] = a[0]
	c = 0
	c1 = 0
	s = 0
	if k == 0:
		for i in range(n - 1):
			s += a[i]
			if s == 0:
				c += 1
		print(c * (c - 1) // 2)
	else:
		for i in range(n - 1):
			s += a[i]
			if s == k:
				c += 1
			elif s == k1:
				c1 += c
		print(c1)


n = int(input())
a = [int(i) for i in input().split()]
if sum(a) % 3 != 0 or n < 3:
	print(0)
	exit(0)
cs = [0] * (len(a) + 1)
(ans, cnt) = (0, 0)
i = 0
while i < len(a):
	cs[i + 1] = cs[i] + a[i]
	i += 1
sum1 = cs[-1] // 3
sum2 = sum1 * 2
i = 1
while i < len(cs) - 1:
	if cs[i] == sum2:
		ans += cnt
	if cs[i] == sum1:
		cnt += 1
	i += 1
print(ans)


import os
import sys
from io import BytesIO, IOBase
import math

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
import os
import sys
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
l = list(map(int, input().split()))
s = sum(l)
if s % 3 == 0:
	pre = [l[0]]
	for i in range(1, n):
		pre.append(pre[i - 1] + l[i])
	a = 0
	b = 0
	cs = 0
	for i in range(0, n - 1):
		if pre[i] == 2 * s // 3:
			b += a
		if pre[i] == s // 3:
			a += 1
	print(b)
else:
	print(0)


n = int(input().strip())
vals = [int(x) for x in input().strip().split(' ')]
j = n - 2
i = 1
full_sum = sum(vals)
cnt = 0
if full_sum % 3 != 0:
	print(0)
	exit()
part_sum = full_sum // 3
j_s = []
sum_3 = 0
for j in range(n - 1, 0, -1):
	sum_3 += vals[j]
	if sum_3 == part_sum:
		j_s.append(j)
j_s = j_s[::-1]
if not j_s:
	print(0)
	exit()
if full_sum == 0 and len(j_s) == n - 1:
	print((n - 1) * (n - 2) // 2)
	exit()
sum_1 = 0
for i in range(0, n - 2):
	sum_1 += vals[i]
	if sum_1 != part_sum:
		continue
	for j in range(len(j_s)):
		if j_s[j] - 1 > i:
			break
	else:
		j = len(j_s)
	cnt += len(j_s) - j
print(cnt)


from sys import stdin
from functools import reduce

def solve(n, arr):
	if n < 3:
		return 0
	v = sum(arr)
	if v % 3 != 0:
		return 0
	v /= 3
	ans = [0] * n
	tmp = 0
	for i in range(n - 1, -1, -1):
		tmp += arr[i]
		if tmp == v:
			ans[i] += 1
	for i in range(n - 2, -1, -1):
		ans[i] += ans[i + 1]
	tmp = 0
	val = 0
	for i in range(n - 2):
		tmp += arr[i]
		if tmp == v:
			val += ans[i + 2]
	return val
n = int(stdin.readline())
arr = list(map(int, stdin.readline().split(' ')))
print(solve(n, arr))


n = int(input())
list1 = list(map(int, input().split()))
lst = []
lst.append(list1[0])
x = 0
sum1 = 0
for i in range(1, n):
	lst.append(lst[i - 1] + list1[i])
for i in range(n - 1):
	if lst[i] * 3 == 2 * lst[n - 1]:
		sum1 += x
	if lst[i] * 3 == lst[n - 1]:
		x += 1
print(sum1)


import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7

def lcm(x, y):
	return x * y / gcd(x, y)

def fact(x, mod=mod):
	ans = 1
	for i in range(1, x + 1):
		ans = ans * i % mod
	return ans

def arr2D(n, m, default=0):
	lst = []
	for i in range(n):
		temp = [default] * m
		lst.append(temp)
	return lst

def sortDictV(x):
	return {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}

def solve(n, lst):
	summ = sum(lst)
	if summ % 3:
		return 0
	(pref, suff) = ([0] * n, [0] * n)
	prefSum = 0
	for i in range(n - 1):
		prefSum += lst[i]
		if prefSum == summ // 3:
			pref[i] = 1
		if summ - prefSum == summ // 3:
			suff[i + 1] = 1
	suffN = 0
	cntSuff = sum(suff)
	cnt = 0
	if suff[0] == 1:
		suffN += 1
	for i in range(n - 1):
		if suff[i + 1] == 1:
			suffN += 1
		if pref[i] == 1:
			cnt += cntSuff - suffN
	return cnt
n = int(input())
lst = list(map(int, input().split()))
print(solve(n, lst))


def main():
	n = int(input())
	val = list(map(int, input().split()))
	s = sum(val)
	if s % 3:
		print(0)
		return
	else:
		s //= 3
		cnt = 0
		ans = 0
		temp = 0
		for i in range(n - 1):
			temp += val[i]
			if temp == 2 * s:
				ans += cnt
			if temp == s:
				cnt += 1
		print(ans)
main()


n = int(input())
a = list(map(int, input().split()))
b = [0 for i in range(n)]
sum = 0
for i in range(len(a)):
	sum += a[i]
if sum % 3 != 0:
	print(0)
else:
	fans = 0
	rs = sum // 3
	k = n - 1
	s = 0
	cnt = 0
	while k >= 2:
		s += a[k]
		if s == rs:
			cnt += 1
		b[k] = cnt
		k -= 1
	fs = 0
	for i in range(len(a) - 2):
		fs += a[i]
		if fs == rs:
			fans += b[i + 2]
	print(fans)


n = int(input())
a = [int(x) for x in input().split()]
q1 = [0] * (n + 10)
p1 = [0] * (n + 10)
q2 = [0] * (n + 10)
p2 = [0] * (n + 10)
q1[0] = a[0]
p1[n - 1] = a[n - 1]
tot = 0
for i in range(n):
	tot += a[i]
if tot % 3 != 0 and n <= 2:
	print(0)
else:
	if a[0] == tot / 3:
		q2[0] = 1
	if a[n - 1] == tot / 3:
		p2[n - 1] = 1
	for i in range(1, n - 2):
		q1[i] = q1[i - 1] + a[i]
		p1[n - i - 1] = p1[n - i] + a[n - i - 1]
		p2[n - i - 1] = p2[n - i]
		if q1[i] == tot / 3:
			q2[i] = 1
		if p1[n - i - 1] == tot / 3:
			p2[n - i - 1] += 1
	ans = 0
	for i in range(0, n - 2):
		if q2[i] == 1:
			ans += q2[i] * p2[i + 2]
	print(ans)


import math
from collections import *
from heapq import *
inf = 10 ** 18
mod = 10 ** 9 + 7
n = int(input())
a = list(map(int, input().split()))
if sum(a) % 3:
	print(0)
	exit()
summ = 0
req = sum(a) // 3
c = 0
ans = 0
for i in range(n - 1):
	summ += a[i]
	if summ == 2 * req:
		ans += c
	if summ == req:
		c += 1
print(ans)


import bisect
n = int(input())
arr = [int(x) for x in input().split()]
total = sum(arr)
dp = [0]
occ = {}
for (ind, item) in enumerate(arr):
	temp = item + dp[-1]
	dp.append(temp)
	if temp in occ:
		occ[temp].append(ind)
	else:
		occ[temp] = [ind]
if n <= 2 or total % 3:
	print(0)
else:
	if total:
		ans = 0
		a = total // 3
		b = 2 * a
		if a in occ and b in occ:
			for ind in occ[a]:
				ans += len(occ[b]) - bisect.bisect_left(occ[b], ind + 1)
		else:
			ans = 0
	else:
		zeros = len(occ[0]) - 2
		ans = zeros * (zeros + 1) // 2
	print(ans)


n = int(input())
l = list(map(int, input().split()))
tot = sum(l)
if tot % 3:
	print(0)
elif tot:
	b1 = tot // 3
	b2 = 2 * b1
	out = 0
	first = 0
	s = 0
	for v in l:
		s += v
		if s == b1:
			first += 1
		elif s == b2:
			out += first
	print(out)
else:
	s = 0
	count = -1
	for v in l:
		s += v
		if s == 0:
			count += 1
	print(count * (count - 1) // 2)


import bisect
n = int(input())
A = list(map(int, input().split()))
prefixes = 0
pre = []
for i in range(n):
	prefixes += A[i]
	pre.append(prefixes)
U = pre[-1]
if U % 3 != 0:
	print(0)
else:
	k = U // 3
	count = 0
	one = []
	double = []
	for i in range(n):
		if pre[i] == k and i < n - 2:
			one.append(i)
		if pre[i] == 2 * k and i < n - 1:
			double.append(i)
	count = 0
	for k in one:
		ind = bisect.bisect_left(double, k + 1)
		count += len(double) - ind
	print(count)


n = int(input())
a = list(map(int, input().strip().split()))
sumi = sum(a)
if sumi % 3 != 0:
	print(0)
else:
	tar = sumi // 3
	i = 0
	pre = 0
	num = 0
	for i in range(n):
		pre += a[i]
		if pre == tar:
			break
	j = n - 1
	post = 0
	stops = [0 for i in range(n)]
	while j > i + 1:
		post += a[j]
		if post == tar:
			num += 1
			stops[j] = num
		else:
			stops[j] = num
		j -= 1
	j = 0
	ans = 0
	if sum(stops):
		ans = stops[i + 2]
	for k in range(i + 1, n - 2):
		pre += a[k]
		if pre == tar:
			ans += stops[k + 2]
	print(ans)


n = int(input())
c = list(map(int, input().split()))
pre = [0 for _ in range(n)]
pre[0] = c[0]
for i in range(1, n):
	pre[i] = c[i]
	pre[i] += pre[i - 1]
if pre[-1] % 3 != 0:
	print(0)
	exit(0)
(u, v) = (0, 0)
a = pre[-1] // 3
for i in range(n):
	if pre[i] == 2 * a and i != n - 1:
		v += u
	if pre[i] == a:
		u += 1
print(v)


(n, a) = (int(input()), list(map(int, input().split())))
s = [0]
s += (s[i] + a[i] for i in range(n))
if s[n] % 3 or 2 * s[n] % 3:
	print(0)
else:
	(d, ans) = (0, 0)
	for i in range(1, n):
		if 3 * s[i] == 2 * s[n]:
			ans += d
		if 3 * s[i] == s[n]:
			d += 1
	print(ans)


import sys
import math
from collections import defaultdict

def read_line():
	return sys.stdin.readline()[:-1]

def read_int():
	return int(sys.stdin.readline())

def read_int_line():
	return [int(v) for v in sys.stdin.readline().split()]
n = read_int()
a = read_int_line()
s = [0] * (n + 1)
for i in range(n):
	s[i + 1] = s[i] + a[i]
if s[n] % 3 != 0:
	print(0)
else:
	cnt = [0] * n
	for i in range(n):
		if s[n] - s[i] == s[n] // 3:
			cnt[i] = 1
	ans = [0] * n
	ans[n - 1] = cnt[n - 1]
	for i in range(1, n):
		ans[n - 1 - i] = ans[n - i] + cnt[n - 1 - i]
	w = 0
	for i in range(1, n - 1):
		if s[i] == s[n] // 3:
			w += ans[i + 1]
	print(w)


def count_arr_div_3arrs(arr, n):
	arr = [0] + arr
	total_sum = sum(arr)
	if total_sum % 3:
		return 0
	else:
		count = 0
		result = 0
		for i in range(1, n + 1):
			arr[i] += arr[i - 1]
			if i > 1 and i < n and (3 * arr[i] == 2 * total_sum):
				result += count
			if arr[i] == total_sum / 3:
				count += 1
		return result

def run(arr):
	res = count_arr_div_3arrs(arr, len(arr))
	return res
n = input()
array = list(map(int, input().split()))
print(run(array))


def main():
	n = int(input())
	nums = list(map(int, input().split()))
	s = sum(nums)
	if s % 3:
		print(0)
		return
	running = 0
	count = [0 for i in range(n)]
	for (i, e) in enumerate(nums):
		if running == s / 3 * 2:
			count[i] = 1
		running += e
	j = n - 2
	while j >= 0:
		count[j] += count[j + 1]
		j -= 1
	prefix = 0
	result = 0
	for (i, e) in enumerate(nums):
		prefix += e
		if prefix == s / 3:
			if i + 2 < n:
				result += count[i + 2]
	print(result)
main()


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
r = 0
if s % 3 == 0:
	ls = s // 3
	rs = 2 * ls
	c = 0
	s = 0
	for i in range(n - 1):
		s += a[i]
		if s == rs:
			r += c
		if s == ls:
			c += 1
print(r)


n = int(input())
a = list(map(int, input().split()))
sums = [a[0]]
for i in range(1, len(a)):
	sums.append(a[i] + sums[-1])
if sums[-1] % 3 == 0:
	find = sums[-1] // 3
	l = []
	s = 0
	for i in range(len(sums) - 1):
		if sums[i] == find:
			l.append([i, 0])
		if sums[i] == 2 * find:
			l.append([i, 1])
			s += 1
	l.sort(key=lambda x: x[0] or x[1])
	count = 0
	if find == 0:
		s -= 1
	for i in range(len(l)):
		if l[i][1] == 0:
			count += s
		if l[i][1] == 1:
			s -= 1
	print(count)
else:
	print(0)


n = int(input())
a = list(map(lambda x: int(x), input().split(' ')))
for i in range(1, n):
	a[i] += a[i - 1]
if a[-1] % 3 != 0:
	print(0)
	exit()
s = a[-1] // 3
i = 0
ans = 0
j = 0
counter = [int(a[0] == s)]
for i in range(1, n - 1):
	if a[i] == s:
		counter.append(1 + counter[i - 1])
	else:
		counter.append(counter[i - 1])
	if a[i] == s * 2:
		ans += counter[i - 1]
print(ans)


def main():
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	for x in l:
		s += x
	if s % 3 != 0:
		print(0)
	else:
		s //= 3
		su = 0
		k = 0
		ans = 0
		l.pop()
		for x in l:
			su += x
			if su == 2 * s:
				ans += k
			if su == s:
				k += 1
		print(ans)
main()


n = int(input())
arr = [int(var) for var in input().split()]
back = [0 for _ in range(n)]
total = sum(arr)
if total % 3 == 0:
	total = total // 3
	back[-1] = 1 if arr[-1] == total else 0
	currSum = arr[-1]
	for i in reversed(range(n - 1)):
		currSum += arr[i]
		back[i] = back[i + 1] + 1 if currSum == total else back[i + 1]
	(currSum, ways) = (0, 0)
	for i in range(n):
		currSum += arr[i]
		if currSum == total:
			ways += back[i + 2] if i + 2 < n else 0
	print(ways)
else:
	print(0)


n = int(input())
ways = list(map(int, input().split()))
total = sum(ways)
output = 0
if total % 3 == 0:
	sequence = total // 3
	carry = total - sequence
	current = 0
	w = 0
	for i in range(n - 1):
		current += ways[i]
		if current == carry:
			output += w
		if current == sequence:
			w += 1
print(output)


import sys
n = int(input())
s = list(map(int, input().split()))
dp = [s[i] for i in range(n)]
for i in range(1, n):
	dp[i] += dp[i - 1]
if dp[-1] / 3 != dp[-1] // 3 or n == 2:
	print(0)
	sys.exit()
x = dp[-1] // 3 * 2
y = dp[-1] // 3
dp1 = [0 for i in range(n)]
aux = dp[::-1]
for i in range(1, n):
	dp1[i] = dp1[i - 1]
	if aux[i] == x:
		dp1[i] += 1
ans = 0
dp1 = dp1[::-1]
for i in range(n - 1):
	if dp[i] == y:
		ans += dp1[i + 1]
print(ans)


n = int(input())
array = list(map(int, input().split(' ')))
sum_each_part = sum(array) / 3
cumulative_sum = [array[0]]
ans = 0
for i in range(1, n):
	cumulative_sum.append(cumulative_sum[i - 1] + array[i])
cnt = [0] * (n + 1)
sum_array = 0
for i in range(n):
	sum_array = sum_array + array[i]
	prev_cnt = cnt[i - 1] if i > 0 else 0
	cnt[i] = prev_cnt + (1 if sum_array == sum_each_part else 0)
for i in range(n - 1):
	if cumulative_sum[i] == 2 * sum_each_part:
		ans = ans + cnt[i - 1] if i > 0 else 0
print(ans)


n = int(input())
arr = [int(x) for x in input().split()]
s = sum(arr)
if s % 3 != 0:
	print(0)
	exit()
if n < 3:
	print(0)
	exit()
t = int(s / 3)
prefix = [x for x in arr]
for i in range(1, n):
	prefix[i] = prefix[i - 1] + arr[i]
suffix = [x for x in arr]
for i in range(n - 2, -1, -1):
	suffix[i] = suffix[i + 1] + arr[i]
new = [1 if x == t else 0 for x in suffix]
suffix_new = [x for x in new]
for i in range(n - 2, -1, -1):
	suffix_new[i] = suffix_new[i + 1] + new[i]
answer = 0
for i in range(n - 2):
	if prefix[i] == t:
		answer += suffix_new[i + 2]
print(answer)


import math
from bisect import bisect_right
n = int(input())
a = list(map(int, input().split()))
val = sum(a)
if val % 3 != 0:
	print(0)
else:
	val //= 3
	p1 = []
	p2 = []
	tsum = 0
	for i in range(n):
		tsum += a[i]
		if tsum == val:
			p1.append(i)
		if tsum == 2 * val:
			p2.append(i)
	if val != 0:
		ans = 0
		for i in p1:
			ans += len(p2) - bisect_right(p2, i)
		print(ans)
	else:
		p1 = len(p1) - 1
		print(p1 * (p1 - 1) // 2)


N = int(input())
A = list(map(int, input().split()))
B = [0] * N
S = sum(A)
if S % 3:
	print(0)
else:
	S //= 3
	T = 0
	for i in range(N):
		T += A[i]
		if T == S:
			B[i] = 1
	for i in range(1, N):
		B[i] += B[i - 1]
	(T, C) = (0, 0)
	for i in range(N - 1, 1, -1):
		T += A[i]
		if T == S:
			C += B[i - 2]
	print(C)


import sys
input = sys.stdin.buffer.readline
import bisect

def I():
	return list(map(int, input().split()))

def sieve(n):
	a = [1] * n
	for i in range(2, n):
		if a[i]:
			for j in range(i * i, n, i):
				a[j] = 0
	return a
n = int(input())
arr = I()
c = 0
prefarr = [0] * (n + 1)
mp = {}
total = sum(arr)
for i in range(n - 1):
	prefarr[i] = arr[i] + prefarr[i - 1]
	if 3 * prefarr[i] == 2 * total:
		curr = mp.get(prefarr[i], [])
		curr.append(i)
		mp[prefarr[i]] = curr
j = 1
for i in range(n - 1):
	currsum = prefarr[i]
	tofind = 2 * prefarr[i]
	if tofind in mp:
		curr = mp[tofind]
		c += len(curr) - bisect.bisect(curr, i)
print(c)


n = int(input())
a = [int(i) for i in input().split()]
s = sum(a)
b = [0 for i in range(n)]
if s % 3 != 0:
	print(0)
else:
	r = s // 3
	t = 0
	for i in range(n - 1, -1, -1):
		t += a[i]
		if t == r:
			b[i] = 1
	for i in range(n - 2, -1, -1):
		b[i] += b[i + 1]
	t = 0
	ans = 0
	for i in range(n - 2):
		t += a[i]
		if t == r:
			ans += b[i + 2]
	print(ans)


n = int(input())
a = [int(x) for x in input().split()]
s = sum(a)
if s % 3:
	print(0)
	exit()
cur = 0
cnt = 0
s //= 3
ans = 0
for x in a:
	cur += x
	if s == 0:
		if cur == 0:
			cnt += 1
		continue
	if cur == s:
		cnt += 1
	if cur == s * 2:
		ans += cnt
if s == 0:
	print((cnt - 1) * (cnt - 2) // 2)
else:
	print(ans)


import sys
import collections
import math
import functools
import itertools
import bisect
import operator
import heapq
import random
true = True
false = False
null = None
tcid = 0
tcmax = 99999999

def compute(val, func):
	return func(val)

def seq(lo, hi, step=1):
	return range(lo, hi + 1, step)

def sround(val, nd):
	return f'{val:.{nd}f}'

def ceil(a, b):
	ans = a // b
	if a % b != 0:
		ans += 1
	return ans

def perr(*args, **kwargs):
	print(*args, file=sys.stderr, **kwargs)

def line():
	ln = sys.stdin.readline().strip()
	if ln == '':
		sys.exit()
	return ln

def lines(n):
	return [line() for i in range(n)]

def split(ln=None):
	return (ln or line()).split()

def num(str=None):
	str = str or line()
	return float(str) if '.' in str else int(str)

def nums(o=None):
	if o is not None:
		if isinstance(o, int):
			o = lines(o)
		elif isinstance(o, str):
			o = split(o)
	return list(map(num, o or split()))

def mainloop(tcid=1):

	def solve():
		n = num()
		a = nums()
		jml = sum(a)
		if jml % 3 != 0:
			return 0
		ptri = jml // 3
		mult = 0
		ans = 0
		if a[0] == ptri:
			mult = 1
		for ii in range(1, n - 1):
			a[ii] += a[ii - 1]
			if a[ii] == 2 * ptri:
				ans += mult
			if a[ii] == ptri:
				mult += 1
		return ans
	print(solve())
while tcid < tcmax:
	tcid += 1
	mainloop(tcid)


n = int(input())
l = list(map(int, input().split()))
s = sum(l)
(m1, m2) = (s // 3, 2 * s // 3)
(curr, prev, ans) = (0, 0, 0)
if s % 3:
	print(0)
	exit()
for i in range(n - 1):
	curr += l[i]
	if curr == m2:
		ans += prev
	if curr == m1:
		prev += 1
print(ans)


n = int(input())
a = list(map(int, input().split(' ')))
total = int(sum(a) / 3)
cnt = list()
cnt1 = list()
count = 0
for i in range(0, n - 2):
	count = count + a[i]
	if count == total:
		cnt.append(i)
count = 0
i = n - 1
while i > 1:
	count = count + a[i]
	if count == total:
		cnt1.append(i)
	i = i - 1
cnt.sort()
cnt1.sort()
k = len(cnt1)
t = len(cnt)
i = 0
j = 0
number = 0
while j < k and i < t:
	if cnt1[j] > cnt[i] + 1:
		number = number + (k - j)
		i = i + 1
	else:
		j = j + 1
if sum(a) / 3 == int(sum(a) / 3):
	print(number)
else:
	print(0)


n = int(float(input()))
a = [int(i) for i in input().strip('\n').split(' ')]
sum = 0
for i in a:
	sum += i
if sum % 3 != 0:
	print('0')
else:
	one = sum // 3
	two = 2 * one
	num_j = 0
	gaps_i = []
	temp = 0
	for i in range(n - 1):
		temp += a[i]
		if temp == two:
			num_j += 1
		if temp == one:
			gaps_i.append(num_j)
	ans = num_j * len(gaps_i)
	for i in gaps_i:
		ans -= i
	print(ans)


n = int(input())
li = list(map(int, input().split()))
if sum(li) % 3 != 0:
	print(0)
	quit()
t = sum(li) // 3
for i in range(1, n):
	li[i] += li[i - 1]
cnt = [0] * n
for i in range(n - 2, -1, -1):
	cnt[i] = cnt[i + 1]
	if li[i] == 2 * t:
		cnt[i] += 1
ans = 0
for i in range(0, n - 1):
	if li[i] != t:
		continue
	ans += cnt[i + 1]
print(ans)


def chk():
	print('HI')
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
(n,) = I()
l = I()
(s, count) = (sum(l), 0)
if not s % 3:
	ss = 0
	t = s // 3
	z = 0
	for i in range(n - 1):
		ss += l[i]
		if ss == 2 * t:
			count += z
		if ss == t:
			z += 1
print(count)


n = int(input())
a = list(map(int, input().split()))
sum = 0
for i in a:
	sum += i
if sum % 3 != 0:
	print(0)
else:
	need = sum // 3
	dp1 = [0 for i in range(n)]
	dp2 = [0 for i in range(n)]
	sum = 0
	for i in range(n - 1):
		sum += a[i]
		if sum == need:
			dp1[i + 1] = dp1[i] + 1
		else:
			dp1[i + 1] = dp1[i]
		if sum == 2 * need:
			dp2[i + 1] = dp2[i] + dp1[i]
		else:
			dp2[i + 1] = dp2[i]
	print(dp2[n - 1])


from bisect import bisect
while True:
	try:
		n = int(input())
	except EOFError:
		break
	a = list(map(int, input().split()))
	s = [0]
	r = [0]
	d = {}
	for v in a:
		s.append(s[-1] + v)
	a.reverse()
	for (i, v) in enumerate(a):
		r.append(r[-1] + v)
		d.setdefault(r[-1], list()).append(n - 1 - i)
	for v in d.values():
		if v[-1] == 0:
			v.pop()
		v.reverse()
	result = 0
	for (i, v) in enumerate(s):
		if i and v * 3 == s[-1]:
			result += len(d[v]) - bisect(d[v], i)
	print(result)


from pprint import pprint
n = int(input().strip())
if n < 3:
	print(0)
	exit(0)
l = [int(i) for i in input().strip().split()]

def init_clos():
	partSum = 0

	def summer(x):
		nonlocal partSum
		partSum += x
		return partSum
	return summer
func = init_clos()
sums = [func(x) for x in l]
if sums[-1] % 3 != 0:
	print(0)
	exit(0)
borders = {1: [], 2: []}
for (index, s) in enumerate(sums):
	if s == sums[-1] // 3:
		borders[1].append(index)
	if s == sums[-1] * 2 // 3:
		borders[2].append(index)
r = 0
(li, ri) = (0, 0)
lefts = borders[1]
rights = borders[2]
if sums[-1] == 0:
	rights = rights[:-1]
while li < len(lefts) and ri < len(rights):
	if lefts[li] < rights[ri]:
		r += len(rights) - ri
		li += 1
	else:
		ri += 1
print(r)


n = int(input())
lis = input().split()
for i in range(n):
	lis[i] = int(lis[i])
lis = [0] + lis
lis1 = [0]
for i in range(1, n + 1):
	lis1.append(lis1[i - 1] + lis[i])
if lis1[n] == 0:
	lis2 = []
	for i in range(1, n):
		if lis1[i] == 0:
			lis2.append(i)
	m = len(lis2)
	if m >= 2:
		print(int(m * (m - 1) / 2))
	else:
		print(0)
else:
	lis2 = []
	lis3 = []
	for i in range(n):
		if lis1[i] == lis1[n] / 3:
			lis2.append(i)
		if lis1[i] == lis1[n] * 2 / 3:
			lis3.append(i)
	m1 = len(lis2)
	m2 = len(lis3)
	if m1 >= 1 and m2 >= 1:
		lis4 = [0 for k in range(m1)]
		for i in range(m1):
			j = 0
			while j <= m2 - 1 and lis3[j] <= lis2[i]:
				j += 1
			lis4[i] = m2 - j
		print(sum(lis4))
	else:
		print(0)


n = int(input())
v = list(map(int, input().split()))
s = sum(v)
if s % 3 == 0:
	cont = 0
	s = s / 3
	r = 0
	soma = 0
	for i in range(n - 1):
		soma += v[i]
		if soma == 2 * s:
			r += cont
		if soma == s:
			cont += 1
	print(r)
else:
	print(0)


n = int(input())
arr = [*map(int, input().split())]
ans = 0
from itertools import accumulate
fsum = [*accumulate(arr)]
idx = []
for i in range(n - 1):
	if (fsum[-1] - fsum[i]) * 3 == fsum[-1]:
		idx.append(i + 1)
from bisect import bisect
for i in range(n):
	if fsum[i] * 3 == fsum[-1]:
		ans += len(idx) - bisect(idx, i + 1)
print(ans)


from collections import Counter
n = int(input())
array = list(map(int, input().split()))
parsum = [array[0]]
for i in range(1, n):
	parsum += [parsum[-1] + array[i]]
total = parsum[-1]
if total % 3 != 0:
	print(0)
elif total == 0:
	k = parsum.count(0)
	print((k - 2) * (k - 1) // 2)
else:
	thirds = list(filter(lambda x: x == total // 3 or x == 2 * total // 3, parsum))
	count = Counter(thirds)
	if len(count) < 2:
		print(0)
	else:
		k = count[total // 3]
		l = count[2 * total // 3]
		output = 0
		i = 0
		while k != 0:
			if thirds[i] == total // 3:
				output += l
				k -= 1
			else:
				l -= 1
			i += 1
		print(output)


arg = int(input())
list1 = list(map(int, input().strip().split()))
s = sum(list1)
if s % 3 != 0:
	print(0)
else:
	ct = 0
	t = 0
	res = 0
	for element in range(0, len(list1) - 1):
		t += list1[element]
		if t == s * 2 // 3:
			res += ct
		if t == s // 3:
			ct += 1
	print(res)


n = int(input())
a = list(map(float, input().split()))
total = sum(a)
one_third = total / 3
two_third = 2 * total / 3
one_third_cummulative_array = [0] * n
two_third_array = [0] * n
one_third_count = 0
running_total = 0
for i in range(n):
	running_total += a[i]
	if running_total == one_third:
		one_third_count += 1
	if running_total == two_third:
		two_third_array[i] = 1
	one_third_cummulative_array[i] = one_third_count
grand_total = 0
for i in range(n - 2, 0, -1):
	if two_third_array[i] == 1:
		grand_total += one_third_cummulative_array[i - 1]
print(grand_total)


n = int(input())
arr = [int(x) for x in input().split()]
s = sum(arr)
if s % 3 != 0:
	print(0)
	exit()
if n < 3:
	print(0)
	exit()
cnt = [0] * n
s /= 3
curr = 0
for i in range(n - 1, -1, -1):
	curr += arr[i]
	if curr == s:
		cnt[i] = 1
for i in range(n - 2, -1, -1):
	cnt[i] = cnt[i + 1] + cnt[i]
answer = 0
curr = 0
for i in range(0, n - 2):
	curr += arr[i]
	if curr == s:
		answer += cnt[i + 2]
print(answer)


import sys
n = int(input())
lst = list(map(int, input().split()))
if n < 3:
	print(0)
	sys.exit()
sums = [lst[0]]
for i in range(1, n):
	sums.append(sums[-1] + lst[i])
if sums[-1] == 0:
	zeroes = sums.count(0)
	print((zeroes - 1) * (zeroes - 2) // 2)
	sys.exit()
if sums[-1] % 3 != 0:
	print(0)
	sys.exit()
x = sums[-1] // 3
y = x * 2
f_y = [0 + (y == sums[0])]
ind = []
if x == sums[0]:
	ind.append(0)
for i in range(1, n - 1):
	if sums[i] == y:
		f_y.append(f_y[-1] + 1)
	else:
		if sums[i] == x:
			ind.append(i)
		f_y.append(f_y[-1])
if 0 in (len(ind), len(f_y)):
	print(0)
	sys.exit()
if ind[-1] == n - 2:
	ind.pop()
count = 0
for index in ind:
	count += f_y[-1] - f_y[index]
print(count)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
ans = 0
d = []
h = [0] * n
if s % 3:
	print(ans)
else:
	by_3_sum = s // 3
	temp_sum = 0
	for i in range(n - 2):
		temp_sum += a[i]
		if temp_sum == by_3_sum:
			d.append(i)
	temp_sum = 0
	cnt = 0
	for i in range(n - 1, 1, -1):
		temp_sum += a[i]
		if temp_sum == by_3_sum:
			cnt += 1
		h[i] = cnt
	for i in d:
		ans += h[i + 2]
	print(ans)


def countWays(arr, n):
	count = [0] * n
	s = sum(arr)
	if s % 3 != 0:
		return 0
	else:
		s = s // 3
	ss = 0
	for i in range(n - 1, -1, -1):
		ss += arr[i]
		if ss == s:
			count[i] = 1
	for i in range(n - 2, -1, -1):
		count[i] += count[i + 1]
	ans = 0
	ss = 0
	for i in range(0, n - 2):
		ss += arr[i]
		if ss == s:
			ans += count[i + 2]
	return ans
n = int(input())
arr = list(map(int, input().split()))
print(countWays(arr, n))


def solve():
	from math import factorial
	n = int(input())
	l = list(map(int, input().strip().split()))
	s = sum(l)
	if s % 3 != 0 or n == 1 or n == 2:
		print(0)
	else:
		sp = s // 3
		c = 0
		(sp1, sp2) = (0, 0)
		for i in range(n - 1):
			c += l[i]
			if c == sp * 2:
				sp2 += sp1
			if c == sp * 1:
				sp1 += 1
		print(sp2)
solve()


from bisect import bisect_left
from collections import defaultdict
n = int(input())
a = [int(x) for x in input().split()]
m = sum(a)
if m % 3 != 0:
	print(0)
else:
	an = 0
	for j in range(1, n):
		a[j] += a[j - 1]
	if m == 0:
		z = a.count(0) - 1
		print(z * (z - 1) // 2)
	else:
		x = defaultdict(lambda : 0)
		for j in range(n):
			if a[j] == m // 3:
				x[0] += 1
			elif a[j] == 2 * (m // 3):
				an += x[0]
		print(an)


n = int(input())
a = list(map(int, input().split()))
l = [a[0]]
ans = 0
for i in range(1, n):
	l.append(a[i] + l[i - 1])
x = l[-1]
if x % 3 == 0:
	x = x // 3
	if x == 0:
		c = l.count(0)
		c -= 2
		print(c * (c + 1) // 2)
	else:
		c = 0
		for i in range(n):
			if l[i] == x:
				c += 1
			if l[i] == 2 * x:
				ans += c
		print(ans)
else:
	print(0)


n = int(input())
a = list(map(int, input().split()))
(Sum, cnt, s) = (n * [0], n * [0], a[0])
Sum[0] = a[0]
for i in range(1, n):
	Sum[i] = Sum[i - 1] + a[i]
	s += a[i]
ss = 0
for i in range(n - 1, -1, -1):
	ss += a[i]
	if ss == s / 3:
		cnt[i] = 1
for i in range(n - 2, -1, -1):
	cnt[i] += cnt[i + 1]
ans = 0
for i in range(n - 2):
	if Sum[i] == s / 3:
		ans += cnt[i + 2]
if s % 3 != 0:
	ans = 0
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
third_portion = [0] * (n + 1)
s = sum(a)
no_of_ways = 0
if s % 3 != 0:
	print(0)
else:
	one_portion = s // 3
	s = 0
	i = n - 1
	while i >= 0:
		s += a[i]
		if s == one_portion:
			third_portion[i] = 1
		i -= 1
	i = n - 2
	while i >= 0:
		third_portion[i] += third_portion[i + 1]
		i -= 1
	s = 0
	for i in range(n - 2):
		s += a[i]
		if s == one_portion:
			no_of_ways += third_portion[i + 2]
	print(no_of_ways)


n = int(input(''))
nums = list(map(int, input('').split(' ')))
s = sum(nums)
result = 0
if s % 3 == 0:
	s = s / 3
	t = s * 2
	first = 0
	prefix = 0
	for i in nums[:-1]:
		prefix += i
		if prefix == t:
			result += first
		if prefix == s:
			first += 1
	print(result)
else:
	print(0)


n = int(input())
a = list(input().split(' '))
for i in range(0, n):
	a[i] = int(a[i])
x = sum(a)
c = []
y = x / 3
res = 0
if y != x // 3:
	print(0)
else:
	s1 = []
	s2 = []
	s3 = []
	ans = 0
	for i in range(0, n):
		ans += a[i]
		s1.append(ans)
	ans = 0
	for j in range(0, n):
		ans += a[n - j - 1]
		s2.append(ans)
	ans = 0
	for i in range(0, n):
		if s2[i] == y:
			ans += 1
		s3.append(ans)
	for i in range(0, n):
		if s1[i] == y:
			if n - i - 3 >= 0:
				res += s3[n - i - 3]
	print(res)


n = int(input())
nums = [int(x) for x in input().split()]
s = sum(nums)
if s % 3 != 0:
	print(0)
else:
	s = int(s / 3)
	res = 0
	pos = []
	tmp = 0
	for i in range(0, n):
		pos.append(0)
	for i in range(n - 1, -1, -1):
		tmp += nums[i]
		if tmp == s:
			pos[i] = 1
	for i in range(n - 2, -1, -1):
		pos[i] += pos[i + 1]
	tmp = 0
	for i in range(0, n - 2):
		tmp += nums[i]
		if tmp == s:
			res += pos[i + 2]
	print(res)


import sys
input = sys.stdin.readline
import math as mt
import collections as cc

def ncr(n, r):
	import math as mt
	return mt.factorial(n) // (mt.factorial(n - r) * mt.factorial(r))
I = lambda : list(map(int, input().split()))
(n,) = I()
l = I()
su = sum(l)
if su % 3:
	print(0)
elif n < 3:
	print(0)
else:
	a = su // 3
	b = a * 2
	ans = 0
	count = 0
	for i in range(1, n):
		l[i] += l[i - 1]
	for i in range(n - 1):
		if l[i] * 3 == su * 2:
			ans += count
		if l[i] * 3 == su:
			count += 1
	print(ans)


def solv():
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	s = sum(a)
	if s % 3 == 0:
		(cnt, ans) = (0, 0)
		curr_sum = 0
		for i in range(len(a) - 1):
			curr_sum += a[i]
			if curr_sum == 2 * s // 3:
				ans += cnt
			if curr_sum == s // 3:
				cnt += 1
		print(ans)
	else:
		print(0)
solv()


n = int(input())
a = list(map(int, input().split()))
s = [a[0]] * n
for i in range(1, n):
	s[i] = s[i - 1] + a[i]
if s[n - 1] % 3 != 0:
	print(0)
else:
	s1 = s[n - 1] // 3
	s2 = s1 * 2
	ans = t = 0
	for i in s[:-1]:
		if i == s2:
			ans += t
		if i == s1:
			t += 1
	print(ans)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nlargest, nsmallest, _heapify_max, _heapreplace_max
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque, Counter as c
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from fractions import Fraction
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
n = int(data())
arr = l()
if sum(arr) % 3 != 0:
	out(0)
	exit()
s = sum(arr) // 3
suf = [0] * n
temp = 0
for i in range(n - 1, -1, -1):
	temp += arr[i]
	if temp == s:
		suf[i] = 1
for i in range(n - 2, -1, -1):
	suf[i] += suf[i + 1]
(answer, temp) = (0, 0)
for i in range(n - 2):
	temp += arr[i]
	if temp == s:
		answer += suf[i + 2]
out(answer)


import bisect
n = int(input())
a = input().split()
s = 0
answer = 0
for i in range(n):
	a[i] = int(a[i])
	s += a[i]
if s % 3 != 0:
	print(0)
	exit()
first_pack = []
m = 0
for i in range(n - 2):
	m += a[i]
	if m == s // 3:
		first_pack.append(i)
if len(first_pack) == 0:
	print(0)
	exit()
k = len(first_pack)
m = 0
for i in range(first_pack[0] + 1, n - 1):
	m += a[i]
	if m == s // 3:
		answer += bisect.bisect_left(first_pack, i)
print(answer)


n = int(input())
l = list(map(int, input().split()))
p = sum(l)
k = 0
if p % 3 == 0:
	y = p // 3
	r = p - y
	z = 0
	t = 0
	for i in range(n - 1):
		z = z + l[i]
		if z == r:
			k = k + t
		if z == y:
			t = t + 1
	print(k)
else:
	print(0)


n = int(input())
nums = list(map(int, input().split()))
total = sum(nums)
if total % 3 != 0:
	print('0')
	exit()
portion = total // 3
part_sum = 0
ways = 0
total_ways = 0
for i in range(n - 1):
	part_sum += nums[i]
	if part_sum == 2 * portion:
		total_ways += ways
	if part_sum == portion:
		ways += 1
print(total_ways)


n = int(input(''))
arr = list(map(int, input().split()))
s = sum(arr)
if s % 3 != 0:
	print(0)
else:
	dp = [0] * n
	dp[0] = arr[0]
	for i in range(1, n):
		dp[i] = dp[i - 1] + arr[i]
	if arr[n - 1]:
		sol = 0
		cnt = 0
		s = dp[n - 1]
		for i in range(n):
			if dp[i] == 2 * s // 3:
				sol += cnt
			if dp[i] == s // 3:
				cnt += 1
		print(sol)
	else:
		cnt = dp.count(0)
		sol = (cnt - 1) * (cnt - 2) // 2
		print(sol)


def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())

def solve():
	N = getInt()
	A = getInts()
	targ = sum(A)
	if targ % 3 > 0:
		return 0
	curr_sum = 0
	i = 0
	i_options = []
	j_options = []
	while i < N:
		curr_sum += A[i]
		if curr_sum == targ // 3 and i < N - 2:
			i_options.append(i)
		if curr_sum == 2 * targ // 3 and i < N - 1:
			j_options.append(i)
		i += 1
	j_greater = []
	curr_num = 0
	i = N - 1
	while i >= 0:
		if j_options and j_options[-1] == i:
			curr_num += 1
			j_options.pop()
		j_greater.append(curr_num)
		i -= 1
	j_greater.reverse()
	ans = 0
	for ii in i_options:
		if ii < N - 1:
			ans += j_greater[ii + 1]
	return ans
ans = solve()
print(ans)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
if s % 3 != 0:
	print(0)
else:
	r = [1 * int(a[-1] == s // 3)]
	e = a[-1]
	for i in range(n - 2, -1, -1):
		e += a[i]
		if e == s // 3:
			r.append(r[-1] + 1)
		else:
			r.append(r[-1])
	answer = 0
	e = 0
	r = r[::-1]
	for i in range(n - 2):
		e += a[i]
		if e == s // 3:
			answer += r[i + 2]
	print(answer)


n = int(input())
l = list(map(int, input().rstrip().split()))
for i in range(1, n):
	l[i] = l[i] + l[i - 1]
count = 0
a = 0
for i in range(n - 1):
	if l[i] * 3 == l[n - 1] * 2:
		a += count
	if l[i] * 3 == l[n - 1]:
		count += 1
print(a)


from sys import stdin, stdout
from collections import Counter
from math import ceil
from bisect import bisect_left
from bisect import bisect_right

def ai():
	return list(map(int, stdin.readline().split()))

def ei():
	return map(int, stdin.readline().split())

def ip():
	return int(stdin.readline().strip())

def op(ans):
	return stdout.write(str(ans) + '\n')
n = ip()
li = ai()
s = sum(li)
if s % 3:
	exit(print(0))
ans = coutn = 0
for i in range(1, n - 1):
	li[i] += li[i - 1]
f = s // 3
s = s - f
for i in li[:n - 1]:
	if i == s:
		ans += coutn
	if i == f:
		coutn += 1
print(ans)


import sys
from collections import defaultdict as dd
from collections import Counter as cc
from queue import Queue
import math
import itertools
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = lambda : sys.stdin.buffer.readline().rstrip()
q = int(input())
w = list(map(int, input().split()))
e = sum(w)
if e % 3 != 0 or q < 3:
	print(0)
	exit()
a = 0
b = 0
c = 0
r = e // 3
if r != 0:
	for i in range(q):
		c += w[i]
		if c == r:
			a += 1
		if c == 2 * r:
			b += a
	print(b)
else:
	for i in range(q):
		c += w[i]
		if c == r:
			a += 1
	print((a - 1) * (a - 2) // 2)


def solve(ns):
	n = len(ns)
	s = sum(ns)
	if s % 3 != 0:
		return 0
	s /= 3
	ps = ns[:]
	for i in range(1, n):
		ps[i] += ps[i - 1]

	def segsum(a, b):
		return ps[b] - (0 if a == 0 else ps[a - 1])
	ls = [0 for i in range(n)]
	rs = [0 for i in range(n)]
	for i in range(n):
		if segsum(0, i) == s:
			ls[i] += 1
		if i > 0:
			ls[i] += ls[i - 1]
	for i in range(n - 1, -1, -1):
		if segsum(i, n - 1) == s:
			rs[i] += 1
		if i < n - 1:
			rs[i] += rs[i + 1]
	res = 0
	for i in range(n - 2):
		if segsum(0, i) == s:
			res += rs[i + 2]
	return res
assert solve([1, 2, 3, 0, 3]) == 2
assert solve([0, 1, -1, 0]) == 1
assert solve([4, 1]) == 0
assert solve([1, 1]) == 0
n = int(input())
ns = list(map(int, input().split()))
print(solve(ns))


k = int(input())
arr = list(map(int, input().strip().split()))[:k]
if sum(arr) % 3 == 0:
	sums = sum(arr) // 3
else:
	print(0)
	exit(0)
sum2 = sums * 2
count = 0
ans = 0
c1 = 0
for i in range(k - 1):
	count += arr[i]
	if count == sum2:
		ans += c1
	if count == sums:
		c1 += 1
print(ans)


import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
N = int(input())
A = [int(x) for x in input().split()]
presum = [0] * (N + 1)
for i in range(1, N + 1):
	presum[i] = presum[i - 1] + A[i - 1]
if presum[-1] % 3 != 0:
	print(0)
	exit(0)
a = presum[-1] // 3
b = presum[-1] * 2 // 3
c = presum[-1]
idx = collections.defaultdict(list)
for i in range(1, N + 1):
	if presum[i] == a:
		idx[a].append(i)
	elif presum[i] == b:
		idx[b].append(i)
	elif presum[i] == c:
		idx[c].append(i)
ans = 0
for i in idx[a]:
	l = bisect.bisect_right(idx[b], i)
	r = bisect.bisect_left(idx[b], N)
	ans += max(0, r - l)
print(ans)


from collections import defaultdict
n = int(input())
inp = input().split()
inp = list(map(lambda x: int(x), inp))
total = sum(inp)
cnt = defaultdict(int)
res = count = right_sum = left_sum = 0
if total % 3 == 0:
	for i in range(len(inp) - 1, -1, -1):
		right_sum += inp[i]
		if right_sum == total / 3:
			count += 1
		cnt[i] = count
	for i in range(len(inp) - 2):
		left_sum += inp[i]
		if left_sum == total / 3:
			res += cnt[i + 2]
print(res)


n = int(input())
li = list(map(int, input().split(' ', n)[:n]))
su = 0
for i in range(n):
	su += li[i]
if su % 3 != 0:
	print(0)
else:
	su //= 3
	cnt = [0] * n
	ss = 0
	for i in range(n - 1, -1, -1):
		ss += li[i]
		if ss == su:
			cnt[i] = 1
	for i in range(n - 2, -1, -1):
		cnt[i] += cnt[i + 1]
	ans = 0
	ss = 0
	for i in range(0, n - 2):
		ss += li[i]
		if ss == su:
			ans += cnt[i + 2]
	print(ans)


n = int(input())
a = [int(i) for i in input().split(' ')]

def gao():
	a_sum = sum(a)
	if a_sum % 3 != 0:
		print('0')
		return
	piece = a_sum // 3
	right = set()
	right_sum = 0
	for i in range(n - 1, 0, -1):
		right_sum += a[i]
		if right_sum == piece:
			right.add(i)
	left_sum = 0
	ans = 0
	for i in range(n):
		left_sum += a[i]
		if i + 1 in right:
			right.remove(i + 1)
		if left_sum == piece:
			ans += len(right)
	print(ans)
gao()


n = input()
n = int(n.strip())
sez = input()
sez = list(map(int, sez.strip().split(' ')))
vsota = sum(sez)
tretjina = vsota / 3
levi_indeksi = []
vsota_leva = sez[0]
for i in range(1, len(sez)):
	if vsota_leva == tretjina:
		levi_indeksi.append(i)
	vsota_leva += sez[i]
desni_indeksi = []
vsota_desna = sez[-1]
for i in range(len(sez) - 2, -1, -1):
	if vsota_desna == tretjina:
		desni_indeksi.append(i)
	vsota_desna += sez[i]
desni_indeksi.sort()
levi_indeksi.sort()
d = len(desni_indeksi)
vseh = 0
j = 0
for i in levi_indeksi:
	while j < len(desni_indeksi) and desni_indeksi[j] < i:
		j = j + 1
		d -= 1
	if j == len(desni_indeksi):
		break
	vseh += d
print(vseh)


input()
nums = list(map(int, input().split()))
sum_n = sum(nums)
ans = current_sum = one_third = 0
for x in nums[:-1]:
	current_sum += x
	if current_sum == 2 / 3 * sum_n:
		ans += one_third
	if current_sum == 1 / 3 * sum_n:
		one_third += 1
print(ans)


n = int(input())
l = list(map(int, input().split()))
s = sum(l)
if s % 3 != 0:
	print(0)
else:
	t = 0
	cnt = 0
	res = 0
	for i in range(n - 1):
		t += l[i]
		if t == 2 * s // 3:
			res += cnt
		if t == s // 3:
			cnt += 1
	print(res)


from sys import stdin, stdout, setrecursionlimit
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm, accumulate
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return input()

def num():
	return int(input())

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]

def pmat(A):
	for ele in A:
		print(*ele, end='\n')

def seive():
	prime = [1 for i in range(10 ** 6 + 1)]
	prime[0] = 0
	prime[1] = 0
	for i in range(10 ** 6 + 1):
		if prime[i]:
			for j in range(2 * i, 10 ** 6 + 1, i):
				prime[j] = 0
	return prime
n = int(data())
a = L()
total = sum(a)
ans = 0
if total % 3 != 0:
	print(0)
else:
	ot = total // 3
	st = ot * 2
	(c, s) = (0, 0)
	for i in range(n - 1):
		s += a[i]
		if s == st:
			ans += c
		if s == ot:
			c += 1
	print(ans)
endtime = time.time()


n = int(input())
a = [int(x) for x in input().split()]
s = sum(a)
s1 = [0 for i in range(n)]
s2 = [0 for i in range(n + 1)]
sum1 = 0
sum2 = 0
m = 0
if s // 3 * 3 != s:
	print(0)
else:
	for i in range(0, n - 2):
		sum1 += a[i]
		if sum1 == s // 3:
			s1[i] = 1
	for j in range(n - 1, 1, -1):
		sum2 += a[j]
		if sum2 == s // 3:
			s2[j] = s2[j + 1] + 1
		else:
			s2[j] = s2[j + 1]
	for k in range(0, n - 2):
		m += s1[k] * s2[k + 2]
	print(m)


n = int(input())
k = list(map(int, input().split()))
for i in range(1, n):
	k[i] += k[i - 1]
c = 0
ans = 0
if k[-1] % 3 != 0:
	print(0)
else:
	for i in range(n - 1):
		if 3 * k[i] == 2 * k[-1]:
			ans += c
		if 3 * k[i] == k[-1]:
			c += 1
	print(ans)


n = int(input())
ls = list(map(int, input().split()))
s = sum(ls)
ans = 0
ot = int(s / 3)
otc = 0
tt = ot * 2
p = 0
if s % 3 == 0:
	for i in range(n - 1):
		p = p + ls[i]
		if p == tt:
			ans = ans + otc
		if p == ot:
			otc = otc + 1
	print(ans)
else:
	print(0)


import sys
import math
from collections import defaultdict
import json
from bisect import bisect

def bin_search(e, a):

	def _search(e, a, lo, hi):
		p = (lo + hi) // 2
		if lo == hi:
			if a[p] <= e:
				return p
			else:
				return p + 1
		if e < a[p]:
			return _search(e, a, p + 1, hi)
		else:
			return _search(e, a, lo, p)
	if not a:
		return 0
	return _search(e, a, 0, len(a) - 1)

def main():
	n = int(sys.stdin.readline().strip())
	a = list(map(int, sys.stdin.readline().strip().split(' ')))
	trail = defaultdict(lambda : [])
	s = 0
	for (i, e) in enumerate(reversed(a)):
		s += e
		trail[s].append(n - i - 1)
	total = s
	s = 0
	cnt = 0
	prev_pos = len(a) - 1
	for (i, e) in enumerate(a):
		s += e
		if e == 0:
			if prev_pos == 0:
				continue
			elif len(a) - prev_pos > i + 1:
				cnt += prev_pos
				continue
		prev_pos = len(a) - 1
		cands = trail[s]
		if not cands:
			continue
		if not total == 3 * s:
			continue
		prev_pos = bin_search(i + 1, cands)
		cnt += prev_pos
	sys.stdout.write(str(cnt) + '\n')
main()


(n, a) = (int(input()), list(map(int, input().split())))
(sums, s) = ([[0, 0]] + [0] * n, sum(a))
for i in range(n):
	sums[i + 1] = [sums[i][0] + a[i], sums[i][1] + int(i != n - 1 and (sums[i][0] + a[i]) * 3 == s * 2)]
print(0 if s % 3 else sum((sums[-1][1] - sums[i][1] for i in range(1, n) if sums[i][0] * 3 == s)))


def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	s = sum(arr)
	if s % 3:
		return 0
	trg = s // 3
	np = 0
	ps = []
	tmp = 0
	for i in range(n - 2):
		tmp += arr[i]
		if tmp == trg:
			np += 1
			ps.append(i)
	ans = 0
	tmp = 0
	for i in range(n - 1, 1, -1):
		tmp += arr[i]
		if tmp == trg:
			ans += np
		if np and ps[-1] == i - 2:
			ps.pop()
			np -= 1
	return ans
print(solve())


n = int(input())
a = list(map(int, input().split()))
o = 0
s = 0
ans = 0
f = 0
p = 0
k = sum(a) // 3
if sum(a) % 3 != 0:
	print('0')
	f = 1
else:
	for i in a[:-1]:
		s += i
		if s == k * 2:
			ans += o
		if s == k:
			o += 1
if f != 1:
	print(ans)


n = int(input())
arr = [int(i) for i in input().split()]
s = sum(arr)
if s % 3 == 0:
	cnt = [0] * n
	target = s // 3
	temp = 0
	for i in range(n - 1, -1, -1):
		temp += arr[i]
		if temp == target:
			cnt[i] = 1
	sum_arr = [0] * n
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			sum_arr[i] = cnt[i]
		else:
			sum_arr[i] = sum_arr[i + 1] + cnt[i]
	temp = 0
	ans = 0
	for i in range(n - 2):
		temp += arr[i]
		if temp == target:
			ans += sum_arr[i + 2]
	print(ans)
else:
	print(0)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
if s % 3 != 0:
	print('0')
else:
	s //= 3
	(sums, k, ans) = (0, 0, 0)
	a.pop()
	for x in a:
		sums += x
		if sums == 2 * s:
			ans += k
		if sums == s:
			k += 1
	print(ans)


import itertools
n = int(input())
b = list(itertools.accumulate([int(i) for i in input().split()]))
ans = 0
if not b[n - 1] % 3:
	x = b[n - 1] // 3
	y = 2 * x
	if not x:
		t = b.count(0)
		ans = (t - 2) * (t - 1) // 2
	else:
		temp = 0
		for i in range(n - 1):
			if b[i] == x:
				temp += 1
			elif b[i] == y:
				ans += temp
print(ans)


n = int(input())
arr = [int(i) for i in input().split()]
pre = []
suff = []
s = sum(arr)
if s % 3 != 0:
	print(0)
else:
	s = s // 3
	l = 0
	c = 0
	ans = 0
	for i in range(n):
		l += arr[i]
		if l == s:
			pre.append(1)
		else:
			pre.append(0)
	l = 0
	c = 0
	for i in range(n - 1, -1, -1):
		l += arr[i]
		if l == s:
			c += 1
		suff.append(c)
	suff.reverse()
	for i in range(n - 2):
		if pre[i] > 0:
			ans += pre[i] * suff[i + 2]
	print(ans)


n = int(input())
(res, summ, s) = (0, 0, 0)
a = [int(x) for x in input().split()]
summ = sum(a)
if summ % 3 != 0:
	print(0)
	exit(0)
arr = [0] * (n + 1)
for i in range(n - 1, -1, -1):
	s += a[i]
	if s == summ // 3:
		arr[i] = arr[i + 1] + 1
	else:
		arr[i] = arr[i + 1]
s = 0
for i in range(n - 2):
	s += a[i]
	if s == summ // 3:
		res += arr[i + 2]
print(res)


N = int(input())
nums = list(map(int, input().split()))
count = 0
t = sum(nums)
if t % 3 != 0:
	print(0)
else:
	target = t // 3
	count = 0
	mark1 = list()
	temp = 0
	for (index, num) in enumerate(nums):
		temp += num
		if temp == target:
			mark1.append(index)
	mark2 = list()
	temp = 0
	for index in range(N - 1, -1, -1):
		temp += nums[index]
		if temp == target and len(mark1) > 0:
			if index > 1 + mark1[-1]:
				count += len(mark1)
			else:
				while len(mark1) > 0 and index <= mark1[-1] + 1:
					mark1.pop()
				if temp == target and len(mark1) > 0:
					if index > 1 + mark1[-1]:
						count += len(mark1)
	print(count)


n = int(input())
a = [int(x) for x in input().split()]
s = sum(a)
if s % 3 != 0:
	print(0)
else:
	b = [a[0]]
	s = s // 3
	for i in range(1, n):
		b.append(b[i - 1] + a[i])
	if s == 0:
		m = b.count(0)
		print((m - 1) * (m - 2) // 2 if m > 2 else 0)
	else:
		t = 0
		c = [0]
		for i in range(n - 1, -1, -1):
			if b[i] == s * 2:
				t += 1
			elif b[i] == s:
				c.append(t + c[-1])
				t = 0
		print(sum(c))


r = input
n = int(r())
N = -10 ** 9 - 7
arr = [int(x) for x in r().split()]
Sum = [0]
cnt = 0
for i in arr:
	Sum.append(Sum[-1] + i)
cnt = [0] * (n + 1)
suf = [0] * (n + 1)
for i in range(n, 0, -1):
	if (Sum[-1] - Sum[i - 1]) * 3 == Sum[-1]:
		cnt[i] = 1
	if i == n:
		suf[i] = cnt[i]
	else:
		suf[i] = cnt[i] + suf[i + 1]
ans = 0
for i in range(1, n - 1, 1):
	if Sum[i] * 3 == Sum[-1]:
		ans += suf[i + 2]
print(ans)


n = int(input())
list1 = list(map(int, input().split()))
s = sum(list1)
if s % 3 == 0:
	w = 0
	s = s / 3
	t = s * 2
	(u, g) = (0, 0)
	for x in list1[:-1]:
		u += x
		if u == t:
			w += g
		if u == s:
			g += 1
	print(w)
else:
	print(0)


n = int(input())
l = list(map(int, input().split()))
lu = l[:]
for i in range(1, n):
	l[i] = l[i] + l[i - 1]
ans = 0
s = 0
co = 0
if l[-1] % 3 == 0:
	for i in lu[:-1]:
		s += i
		if s == l[-1] // 3 * 2:
			ans += co
		if s == l[-1] // 3:
			co += 1
	print(ans)
else:
	print(0)


from math import *
su = 0
ls = []
n = int(input())
ls = [int(x) for x in input().split()]
pre = [0 for i in range(n + 1)]
for i in range(n):
	pre[i + 1] = pre[i] + ls[i]
x = sum(ls)
res = x // 3
res2 = 2 * res
ans = 0
cnt = 0
for i in range(1, n):
	if pre[i] == res2:
		ans += cnt
	if pre[i] == res:
		cnt += 1
if x % 3 != 0:
	print(0)
else:
	print(ans)


n = int(input())
entrada = input().split()
entrada = [int(i) for i in entrada]
soma = 0
for i in range(0, n):
	soma += entrada[i]
if soma % 3 != 0:
	print(0)
else:
	buffer = 0
	parte = soma // 3
	posicao = [0] * 500100
	i = n - 1
	while i >= 0:
		buffer += entrada[i]
		if buffer == parte:
			posicao[i] += 1
		i -= 1
	i = n - 2
	while i >= 0:
		posicao[i] += posicao[i + 1]
		i -= 1
	buffer = 0
	result = 0
	i = 0
	while i + 2 < n:
		buffer += entrada[i]
		if buffer == parte:
			result += posicao[i + 2]
		i += 1
	print(result)


n = int(input())
A = [int(x) for x in input().split()]
B = [0] * n
S = 0
SA = sum(A)
Cnt = 0
Tot = 0
for i in range(n - 1):
	S += A[i]
	if 3 * S == 2 * SA:
		Tot += Cnt
	if 3 * S == SA:
		Cnt += 1
print(Tot)


import bisect
input()
ls = list(map(int, input().split()))
cum = [0] * len(ls)
cum[0] = ls[0]
for i in range(1, len(cum)):
	cum[i] = ls[i] + cum[i - 1]

def f(arr, a, b):
	if a == 0:
		return arr[b]
	else:
		return arr[b] - arr[a - 1]
c = 0
s = sum(ls)
if not s % 3:
	btm = []
	top = []
	tg = s // 3
	for i in range(len(cum) - 1):
		if cum[i] == tg:
			btm.append(i)
		if cum[i] == 2 * tg:
			top.append(i)
	for ind in btm:
		num = bisect.bisect_right(top, ind)
		c += len(top) - num
print(c)


from bisect import bisect_right
n = int(input())
a = [int(i) for i in input().split()]
prefix = [0] * (n + 1)
prefix[1] = a[0]
for i in range(2, n + 1):
	prefix[i] = prefix[i - 1] + a[i - 1]
need = prefix[-1] // 3
ans = 0
if need * 3 == prefix[-1]:
	good_pref1 = []
	good_pref2 = []
	for i in range(1, n):
		if prefix[i] == need:
			good_pref1.append(i)
		if prefix[i] == 2 * need:
			good_pref2.append(i)
	l = len(good_pref2)
	for i in good_pref1:
		ans += l - bisect_right(good_pref2, i)
print(ans)


n = int(input())
arr = list(map(int, input().split()))
prefix_sum = [0] * n
prefix_sum[0] = arr[0]
for i in range(1, n):
	prefix_sum[i] = prefix_sum[i - 1] + arr[i]
if prefix_sum[-1] % 3 != 0:
	print(0)
else:
	answer = 0
	part = prefix_sum[-1] // 3
	s2_right = [0] * n
	for i in range(n - 2, 0, -1):
		s2_right[i] = s2_right[i + 1]
		if prefix_sum[i] == part * 2:
			s2_right[i] += 1
	for i in range(n - 1):
		if prefix_sum[i] == part:
			answer += s2_right[i + 1]
	print(answer)


n = int(input())
a = input().split()
for i in range(n):
	a[i] = int(a[i])
	if i > 0:
		a[i] += a[i - 1]
(x, ans) = (0, 0)
for i in range(n - 1):
	if a[i] * 3 == a[n - 1] * 2:
		ans += x
	if a[i] * 3 == a[n - 1]:
		x += 1
print(ans)


import sys
n = int(input())
a = [int(i) for i in input().split()]
sun = (n + 2) * [0]
sun[0] = 0
for i in range(n):
	sun[i + 1] = sun[i] + a[i]
if sun[n] % 3 != 0:
	print(0)
else:
	s = sun[n] / 3
	s2 = 2 * s
	cnt = 0
	ans = 0
	for i in range(1, n):
		if sun[i] == s2:
			ans += cnt
		if sun[i] == s:
			cnt += 1
	print(ans)


from itertools import accumulate
(n, a) = (int(input()), (*map(int, input().split()),))
p = (*accumulate(a),)
q = (*accumulate((x == 2 * p[-1] // 3 for x in p)),)
print(sum((q[-2] - q[i] for i in range(n - 2) if p[i] == p[-1] // 3)) if p[-1] % 3 == 0 and n >= 3 else 0)


ints = int(input())
listx = list(map(int, input().split(' ')))
sumx = sum(listx)
if sumx % 3 != 0:
	print(0)
else:
	num1 = 0
	num2 = 0
	ctotal = 0
	listx.reverse()
	listx.remove(listx[0])
	listx.reverse()
	for i in listx:
		ctotal += i
		if ctotal == int(sumx * 2 / 3):
			num2 += num1
		if ctotal == int(sumx * 1 / 3):
			num1 += 1
	print(num2)


n = int(input())
a = list(map(int, input().split()))
dp = [0] * n
dp[0] = a[0]
for i in range(1, n):
	dp[i] = dp[i - 1] + a[i]
if dp[-1] % 3 != 0:
	print(0)
elif dp[-1] != 0:
	o = dp[-1] // 3
	t = 2 * o
	c = 0
	co = 0
	for i in range(n - 1, -1, -1):
		if dp[i] == t:
			c += 1
		elif dp[i] == o:
			co += c
	print(co)
else:
	c = dp.count(0)
	c -= 1
	print(c * (c - 1) // 2)


n = int(input())
s = list(map(int, input().split()))
count = 0
sv = sum(s)
if not sv % 3:
	sv = sv / 3
	t = 2 * sv
	u = g = 0
	for i in s[:-1]:
		u += i
		if u == t:
			count += g
		if u == sv:
			g += 1
print(count)


n = int(input())
A = [int(x) for x in input().split()]
B = [0] * n
B[0] = A[0]
for i in range(1, n):
	B[i] = B[i - 1] + A[i]
mem = dict()
ans = 0
for i in range(n - 3, -1, -1):
	if B[i + 1] not in mem:
		mem[B[i + 1]] = 1
	else:
		mem[B[i + 1]] += 1
	if (B[-1] - B[i]) % 2 != 0:
		continue
	elif (B[-1] - B[i]) // 2 != B[i]:
		continue
	elif B[-1] - B[i] not in mem:
		continue
	else:
		ans += mem[B[-1] - B[i]]
print(ans)


from sys import *

def solve(a, n):
	(sm, r) = ([], 0)
	for i in range(n):
		r += a[i]
		sm.append(r)
	t = sm[-1]
	if t % 3 != 0 or n == 1 or n == 2:
		return 0
	count = [0] * (n - 1)
	rc = 0
	for i in range(n - 2, 0, -1):
		if sm[i] == 2 * t // 3:
			rc += 1
		count[i - 1] = rc
	ans = 0
	for i in range(n - 2):
		if sm[i] == t // 3:
			ans += count[i]
	return ans
test = 1
for t in range(0, test):
	n = int(input())
	arr = [int(x) for x in input().split()]
	ans = solve(arr, n)
	print(ans)


n = int(input())
t = list(map(int, input().split()))
(cnt, cnt2, s) = (0, 0, sum(t))
if s % 3 == 0:
	a = s // 3
	b = s - a
	partial = 0
	for i in range(n - 1):
		partial += t[i]
		if partial == b:
			cnt += cnt2
		if partial == a:
			cnt2 += 1
print(cnt)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
if s % 3 != 0:
	print(0)
	exit()
s //= 3
isSum = [0] * n
cur = 0
for i in range(n - 1, -1, -1):
	cur += a[i]
	if cur == s:
		isSum[i] = 1
psums = [0] * (n - 1) + [isSum[-1]]
for i in range(n - 2, -1, -1):
	psums[i] += psums[i + 1] + isSum[i]
ret = 0
cur = 0
for i in range(n - 2):
	cur += a[i]
	if cur == s:
		ret += psums[i + 2]
print(ret)


tamanho = int(input())
vetor_entrada = input()
vetor_str = list(vetor_entrada.split(' '))
vetor = list(map(int, vetor_str))
soma = sum(vetor)
if soma % 3 == 0:
	soma = soma / 3
	soma_s = 0
	resposta = 0
	ct = 0
	for cont in range(0, tamanho - 1):
		soma_s = soma_s + vetor[cont]
		if soma_s == 2 * soma:
			resposta = resposta + ct
		if soma_s == soma:
			ct = ct + 1
	print(int(resposta))
else:
	print(0)


n = int(input())
l = [int(j) for j in input().split()]
summ = sum(l)
s = summ // 3
t = summ - s
c = 0
a = 0
b = 0
if summ % 3 != 0:
	print(0)
else:
	for i in range(n - 1):
		a += l[i]
		if a == t:
			c += b
		if a == s:
			b += 1
	print(c)


n = int(input())
a = [int(x) for x in input().split()]
nb_ways = 0
sum_a = sum(a)
if sum_a % 3 != 0 or n < 3:
	print(0)
else:
	sum_group = int(sum_a / 3)
	list_i = []
	list_j = []
	sum_i = 0
	for i in range(1, n - 1):
		sum_i += a[i - 1]
		if sum_i == sum_group:
			list_i.append(i)
	sum_j = 0
	for j in reversed(range(2, n)):
		sum_j += a[j]
		if sum_j == sum_group:
			list_j.append(j)
	list_j.sort()
	pointeur_i = 0
	pointeur_j = 0
	longueur_i = len(list_i)
	longueur_j = len(list_j)
	while pointeur_i < longueur_i and pointeur_j < longueur_j:
		if list_i[pointeur_i] >= list_j[pointeur_j]:
			pointeur_j += 1
			continue
		else:
			nb_ways += longueur_j - pointeur_j
			pointeur_i += 1
	print(nb_ways)


def countways(a, n):
	cnt = [0 for i in range(n)]
	s = 0
	s = sum(a)
	if s % 3 != 0:
		return 0
	s //= 3
	ss = 0
	for i in range(n - 1, -1, -1):
		ss += a[i]
		if ss == s:
			cnt[i] = 1
	for i in range(n - 2, -1, -1):
		cnt[i] += cnt[i + 1]
	ans = 0
	ss = 0
	for i in range(0, n - 2):
		ss += a[i]
		if ss == s:
			ans += cnt[i + 2]
	return ans
n = int(input())
a = list(map(int, input().split()))
print(countways(a, n))


from sys import stdin, stdout
nmbr = lambda : int(stdin.readline())
lst = lambda : list(map(int, input().split()))
n = nmbr()
a = lst()
sm = sum(a)
if sm % 3 != 0:
	print(0)
	exit()
dp = [0] * n
suf = pre = ans = 0
for i in range(n - 1, -1, -1):
	suf += a[i]
	if i == n - 1:
		dp[i] = 0
	else:
		dp[i] = dp[i + 1]
	if suf == sm // 3:
		dp[i] += 1
for i in range(n):
	pre += a[i]
	if pre == sm // 3:
		ans += dp[i + 2] if i + 2 < n else 0
print(ans)


import bisect
n = int(input())
a = [int(i) for i in input().split()]
s = sum(a)
if s % 3 != 0:
	print(0)
else:
	ans = 0
	pref = [0] * (n + 1)
	for i in range(1, n + 1):
		pref[i] = pref[i - 1] + a[i - 1]
	good1 = []
	for i in range(1, n - 1):
		if pref[i] == s / 3:
			good1.append(i - 1)
	suff = [0] * (n + 1)
	for i in range(1, n + 1):
		suff[i] = suff[i - 1] + a[n - i]
	for i in range(1, n - 1):
		if suff[i] == s / 3:
			index = n - i - 2
			ans += bisect.bisect_right(good1, index)
	print(ans)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
pre = [0] * n
precoun = [0] * n
suf = [0] * n
sufcoun = [0] * n
if s % 3 == 0:
	for i in range(n):
		temp = n - i - 1
		pre[i] = pre[i - 1] + a[i]
		if pre[i] == s // 3:
			precoun[i] = precoun[i - 1] + 1
		else:
			precoun[i] = precoun[i - 1]
		suf[temp] = suf[(temp + 1) % n] + a[temp]
		if suf[temp] == s // 3:
			sufcoun[temp] = sufcoun[(temp + 1) % n] + 1
		else:
			sufcoun[temp] = sufcoun[(temp + 1) % n]
	ans = 0
	for i in range(n - 2):
		if pre[i] == s // 3:
			ans += sufcoun[i + 2]
	print(ans)
else:
	print(0)


from __future__ import division, print_function
import itertools, os, sys, threading
from collections import deque, Counter, OrderedDict, defaultdict
import heapq
from math import ceil, floor, log, sqrt, factorial, pow, pi

def ii():
	return int(input())

def si():
	return str(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[0]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000

def dfs(v, c):
	if visited[v]:
		if col[v] != c:
			print('-1')
			exit()
		return
	col[v] = c
	visited[v] = 1
	for i in graph[v]:
		dfs(i, c ^ 1)

def bfs(d, v):
	q = []
	q.append(v)
	visited[v] = 1
	while len(q) != 0:
		x = q[0]
		q.pop(0)
		for i in d[x]:
			if visited[i] != 1:
				visited[i] = 1
				q.append(i)
		print(x)
	print(l)

def make_graph(e):
	d = {}
	for i in range(e):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
		if y not in d.keys():
			d[y] = [x]
		else:
			d[y].append(x)
	return d

def gr2(n):
	d = {}
	for i in range(n):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
	return d

def connected_components(graph):
	seen = set()

	def dfs(v):
		vs = set([v])
		component = []
		while vs:
			v = vs.pop()
			seen.add(v)
			vs |= set(graph[v]) - seen
			component.append(v)
		return component
	ans = []
	for v in graph:
		if v not in seen:
			d = dfs(v)
			ans.append(d)
	return ans
n = ii()
l = li()
if sum(l) % 3 != 0:
	print(0)
else:
	(k, b, s, c) = (0, 0, 0, 0)
	k = sum(l) // 3
	for i in l[:-1]:
		s += i
		if s == 2 * k:
			b += c
		if s == k:
			c += 1
	print(b)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
if s % 3 != 0:
	print(0)
	exit()
st = 0
x = s / 3
(ans, count) = (0, 0)
for i in range(n - 1):
	st += a[i]
	if st == 2 * x:
		ans += count
	if st == x:
		count += 1
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
total = sum(a)
x = 0
y = 0
z = 0
if total % 3 != 0:
	print(0)
else:
	s = int(total / 3)
	for i in range(n - 1):
		x += a[i]
		if x == 2 * s and i > 0:
			z += y
		if x == s and i < n - 2:
			y += 1
	print(z)


log = lambda *x: print(*x)

def cin(*fn, def_fn=str):
	(i, r) = (0, [])
	for c in input().split(' '):
		r += [(fn[i] if len(fn) > i else fn[0])(c)]
		i += 1
	return r
(n,) = cin(int)
a = cin(int)

def RSQ(i, j):
	return pre_sum[j] - pre_sum[i - 1]
count = 0
if sum(a) % 3 == 0:
	pre_sum = [0] * n
	pre_sum[0] = a[0]
	for i in range(1, n):
		pre_sum[i] = pre_sum[i - 1] + a[i]
	index = {pre_sum[0]: 0}
	idx = lambda i: index.setdefault(i, -1)
	seen = {pre_sum[0]: 1}
	for i in range(1, n - 1):
		v = pre_sum[i]
		hf = v // 2
		if 2 * hf == v:
			if idx(hf) != -1 and RSQ(i + 1, n - 1) == hf:
				count += seen[hf]
		index.setdefault(i, -1)
		index[v] = i
		seen.setdefault(v, 0)
		seen[v] += 1
log(count)


def threeway(a, n):
	cnt = [0 for i in range(n)]
	s = 0
	s = sum(a)
	if s % 3 != 0:
		return 0
	s //= 3
	ss = 0
	for i in range(n - 1, -1, -1):
		ss += a[i]
		if ss == s:
			cnt[i] = 1
	for i in range(n - 2, -1, -1):
		cnt[i] += cnt[i + 1]
	ans = 0
	ss = 0
	for i in range(0, n - 2):
		ss += a[i]
		if ss == s:
			ans += cnt[i + 2]
	return ans
n = eval(input())
a = [int(a) for a in input().split()]
print(threeway(a, n))


from sys import stdin
from itertools import accumulate
n = int(input())
a = (*map(int, input().split()),)
p = (*accumulate(a),)
s = p[-1]
if s % 3 or n < 3:
	print(0)
	exit()
q = (*accumulate((x == 2 * s // 3 for x in p)),)
print(sum((q[-2] - q[i] for i in range(n - 2) if p[i] == s // 3)))


n = int(input())
nums = [int(i) for i in input().split()]
s = sum(nums)
ans = 0
if s % 3 != 0:
	ans = 0
else:
	s /= 3
	cnt = [0] * n
	sum = 0
	for i in range(n - 1, -1, -1):
		sum += nums[i]
		if sum == s:
			cnt[i] = 1
	for j in range(n - 2, -1, -1):
		cnt[j] += cnt[j + 1]
	sum = 0
	k = 0
	while k + 2 < n:
		sum += nums[k]
		if sum == s:
			ans += cnt[k + 2]
		k += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
dp = list(range(0, n + 1))
dp[0] = a[0]
for i in range(1, n):
	dp[i] = dp[i - 1] + a[i]
if dp[n - 1] % 3 != 0:
	print(0)
	exit()
div = dp[n - 1] / 3
time = 0
timg = 0
for i in range(0, n - 1):
	if dp[i] == div * 2:
		timg += time
	if dp[i] == div:
		time += 1
print(timg)


n = int(input())
list = list(map(int, input().split()))
(count, add) = (0, sum(list))
if add % 3 == 0:
	third = add // 3
	rem = add - third
	(add, d) = (0, 0)
	for i in range(n - 1):
		add += list[i]
		if add == rem:
			count += d
		if add == third:
			d += 1
print(count)


import sys
import math
from collections import defaultdict

def solve():
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [a[0]] * n
	for i in range(1, n):
		b[i] = b[i - 1] + a[i]
	if b[-1] % 3 != 0:
		print(0)
		return
	ans = 0
	count = 0
	rec = []
	for i in range(1, n - 1):
		if b[i] == 2 * b[-1] // 3:
			count += 1
			rec.append(i)
	j = 0
	if count == 0:
		print(ans)
		return
	for i in range(n):
		if b[i] == 2 * b[-1] // 3 and i == rec[j]:
			count -= 1
			j += 1
			if count == 0:
				break
		if b[i] == b[-1] // 3 and i != rec[j]:
			ans += count
	print(ans)
test_cases = 1
for _ in range(test_cases):
	solve()


n = int(input())
l = list(map(int, input().split()))
prif = []
sum_ = 0
for i in l:
	sum_ = sum_ + i
	prif.append(sum_)
total = prif[n - 1]
if total % 3 != 0:
	print(0)
else:
	arr = [0] * n
	req = total * 2 // 3
	req1 = total // 3
	cou = prif[:n - 1].count(req)
	j = 0
	for i in range(n - 1):
		if i != 0:
			arr[i] = cou - j
		if prif[i] == req:
			j += 1
	ans = 0
	for i in range(n - 2):
		if prif[i] == req1:
			ans = ans + arr[i + 1]
	print(ans)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	n = I()
	a = LI()
	s = sum(a)
	if s % 3 != 0:
		return 0
	s3 = s // 3
	wa = [0]
	c = 0
	for k in a:
		c += k
		if c == s3:
			wa.append(wa[-1] + 1)
		else:
			wa.append(wa[-1])
	r = 0
	c = 0
	for i in range(n - 1, 0, -1):
		c += a[i]
		if c == s3:
			r += wa[i - 1]
	return r
print(main())


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
accumulate = 0
one_third = [0] * (n + 1)
for i in range(n - 2):
	accumulate += a[i]
	if accumulate * 3 == s:
		one_third[i] = 1
accumulate = 0
ans = 0
v = sum(one_third)
for i in range(n - 1, 1, -1):
	v -= one_third[i - 1]
	accumulate += a[i]
	if accumulate * 3 == s:
		ans += v
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = sum(a)
if b % 3 == 0:
	e = b / 3
	i = 0
	c = [0] * n
	d = 0
	while i < n:
		d += a[i]
		c[i] += c[i - 1]
		if d == e and i < n - 2:
			c[i] += 1
		i += 1
	i = n
	d = 0
	f = 0
	while i > 0:
		i -= 1
		d += a[i]
		if d == e and i > 1:
			f += c[i - 2]
	print(f)
else:
	print(0)


def main():
	from sys import stdin
	from itertools import accumulate, tee, islice
	tokens = map(int, stdin.read().split())
	N = next(tokens)
	(A, A2) = tee(tokens)
	S = sum(A2)
	if S % 3 != 0:
		print(0)
		return
	S1 = int(S / 3)
	S2 = int(2 * S / 3)
	ans = count = 0
	for s in islice(accumulate(A), N - 1):
		if s == S2:
			ans += count
		if s == S1:
			count += 1
	print(ans)
main()


import bisect
input()
ls = list(map(int, input().split()))
cum = [0] * len(ls)
cum[0] = ls[0]
for i in range(1, len(cum)):
	cum[i] = ls[i] + cum[i - 1]

def f(arr, a, b):
	if a == 0:
		return arr[b]
	else:
		return arr[b] - arr[a - 1]
c = 0
s = sum(ls)
if not s % 3:
	tg = s // 3
	nl = nh = 0
	acm = 0
	for x in ls[:-1]:
		acm += x
		if acm == 2 * tg:
			c += nl
		if acm == tg:
			nl += 1
print(c)


n = int(input())
a = map(int, input().split())
v = 0
s = []
for e in a:
	v += e
	s.append(v)
total_sum = s[-1]
if total_sum % 3 != 0:
	print(0)
else:
	r_idx = [0] * n
	for i in range(n - 1, 0, -1):
		if i < n - 1:
			r_idx[i] = r_idx[i + 1]
		if s[i - 1] == total_sum // 3 * 2:
			r_idx[i] += 1
	r = 0
	for i in range(n - 2):
		if s[i] == total_sum // 3:
			r += r_idx[i + 2]
	print(r)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
if n < 3:
	print(0)
elif s % 3 == 0:
	es = s // 3
	c = 0
	c1 = 0
	first = []
	suml = []
	for i in range(n):
		c += a[i]
		first.append(int(c == es))
		c1 += a[n - 1 - i]
		v = int(c1 == es)
		if i == 0:
			suml.append(v)
		else:
			suml.append(suml[-1] + v)
	suml = suml[::-1]
	ans = 0
	for i in range(n - 2):
		if first[i] == 1:
			ans += suml[i + 2]
	print(ans)
else:
	print(0)


n = int(input())
k = list(map(int, input().split()))
ans = 0
s = sum(k)
if s % 3 == 0:
	one_thirds = s // 3
	two_thirds = s - one_thirds
	s = t = 0
	for i in range(n - 1):
		s += k[i]
		if s == two_thirds:
			ans += t
		if s == one_thirds:
			t += 1
print(ans)


import sys
n = int(input())
a = map(int, input().split())
a = list(a)
s = sum(a)
if n <= 2 or s % 3:
	print(0)
	sys.exit(0)
zz = int(s / 3)
c = [0] * n
x = 0
for i in range(n - 1, -1, -1):
	x += a[i]
	if x == zz:
		c[i] = 1
for i in range(n - 2, -1, -1):
	c[i] += c[i + 1]
ans = 0
x = 0
for i in range(n - 2):
	x += a[i]
	if x == zz:
		ans += c[i + 2]
print(ans)


n = int(input())
a = [int(i) for i in input().split()]
s = sum(a)
x = 0
if s % 3 == 0:
	d1 = s / 3
	d2 = 2 * d1
	z = t = 0
	for i in range(n - 1):
		z += a[i]
		if z == d2:
			x += t
		if z == d1:
			t += 1
print(x)


from sys import stdin, stdout
input = stdin.readline
mp = lambda : map(int, input().split())
it = lambda : int(input())
a = it()
b = list(mp())
(sm, rem) = divmod(sum(b), 3)
if rem:
	print(0)
else:
	(fst, scd) = (0, 0)
	pre = 0
	for i in range(a - 1):
		pre += b[i]
		if pre == 2 * sm:
			scd += fst
		if pre == sm:
			fst += 1
	print(scd)


n = int(input())
A = [int(a) for a in input().split()]
s = sum(A)
if s % 3 != 0:
	print(0)
else:
	pr = [0] * (n + 1)
	for i in range(n):
		pr[i + 1] = pr[i] + A[i]
	sf = [0] * (n + 1)
	sf[n] = A[n - 1]
	for i in range(n - 1, 0, -1):
		sf[i] = sf[i + 1] + A[i - 1]
	cnt = 0
	C = [0] * (n + 1)
	for j in range(1, n + 1):
		C[j] = 1 if sf[j] == s // 3 else 0
	S = [0] * (n + 1)
	S[n] = C[n]
	for i in range(n - 1, 0, -1):
		S[i] = S[i + 1] + C[i]
	for i in range(1, n - 1):
		if pr[i] == s // 3:
			cnt += S[i + 2]
	print(cnt)


n = int(input())
arr = list(map(int, input().split()))
tot = sum(arr)
if tot % 3 != 0:
	print(0)
else:
	req = tot // 3
	presum = []
	suma = 0
	for i in range(n):
		suma += arr[i]
		if suma == req:
			presum.append(i)
	postsum = []
	suma = 0
	for i in range(n - 1, -1, -1):
		suma += arr[i]
		if suma == req:
			postsum.append(i)
	postsum.sort()
	l1 = len(presum)
	l2 = len(postsum)
	ans = 0
	for i in range(l1):
		curr = presum[i]
		l = 0
		r = l2 - 1
		while l <= r:
			mid = (l + r) // 2
			if postsum[mid] > curr + 1:
				r = mid - 1
			else:
				l = mid + 1
		if 0 <= l < l2 and postsum[l] > curr + 1:
			ans += l2 - l
	print(ans)


import heapq as hp
import collections
import bisect
import math

def unpack(func=int):
	return map(func, input().split())

def l_unpack(func=int):
	return list(map(func, input().split()))

def s_unpack(func=int):
	return sorted(map(func, input().split()))

def range_n():
	return range(int(input()))

def counter(a):
	d = {}
	for x in a:
		if x in d:
			d[x] += 1
		else:
			d[x] = 1
	return d

def getint():
	return int(input())

def main():
	n = getint()
	a = l_unpack()
	cs2 = cs = 0
	s = sum(a)
	if s % 3:
		print(0)
	else:
		t = 0
		s //= 3
		for i in range(n - 1):
			t += a[i]
			if t == 2 * s:
				cs2 += cs
			if t == s:
				cs += 1
		print(cs2)
main()


n = int(input())
lst = list(map(int, input().split()))
sm = [0] * n
sm[0] = lst[0]
for i in range(1, len(lst)):
	sm[i] = sm[i - 1] + lst[i]
if sm[-1] % 3 != 0:
	print(0)
else:
	count = 0
	part = sm[-1] // 3
	pos3 = [0] * n
	pos6 = [0] * n
	count = 0
	for i in range(len(sm) - 1)[::-1]:
		if sm[i] == part and i != n - 2:
			pos3[i] = count
		if sm[i] == part * 2 and i != 0:
			pos6[i] = 1
			count += 1
	print(sum(pos3))


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
if n <= 2 or s % 3 != 0:
	print(0)
else:
	ref = s // 3
	l = 0
	r = s
	posl = []
	posr = []
	for i in range(len(a) - 1):
		l += a[i]
		r -= a[i]
		if l == ref:
			posl.append(i)
		if r == ref:
			posr.append(i + 1)
	count = 0
	i = len(posl) - 1
	j = len(posr) - 1
	while i >= 0 and j >= 0:
		if posl[i] + 2 <= posr[j]:
			count += i + 1
			j -= 1
		else:
			i -= 1
	print(count)


n = int(input())
m = b = 0
s = [int(i) for i in input().split()]
for i in range(n - 1):
	if s[i + 1] < 0:
		b = 1
	s[i + 1] = s[i] + s[i + 1]
if int(s[n - 1] / 3) == s[n - 1] / 3:
	a = s[n - 1] // 3
	if a == 0:
		t = s.count(0)
		m = (t - 1) * (t - 2) // 2
	elif b == 0:
		m = s.count(a) * s.count(2 * a)
	else:
		for i in range(n):
			if s[i] == a:
				m += s[i:].count(2 * a)
print(m)


from itertools import accumulate, combinations
n = int(input())
seq = list(map(int, input().split()))
order_sum = list(accumulate(seq, lambda x, y: x + y))
if order_sum[n - 1] % 3 != 0 or n < 3:
	print(0)
else:
	reverse_sum = []
	cur = 0
	for i in range(n - 1, -1, -1):
		cur += seq[i]
		reverse_sum.append(cur)
	border = order_sum[n - 1] // 3
	equal = reverse_sum.count(border)
	counter = 0
	if border != 0:
		middle_list = []
		order_list = []
		right_list = []
		t = 0
		for i in range(n):
			if reverse_sum[i] == border:
				t += 1
			if order_sum[i] == border:
				right_list.append(i)
			order_list.append(t)
		for i in range(len(right_list)):
			counter += order_list[n - 2 - right_list[i]]
		print(counter)
	else:
		print((equal ** 2 - 3 * equal + 2) // 2)


n = int(input())
a = list(map(int, input().split()))
total = sum(a)
if total % 3 != 0:
	print(0)
else:
	t = total // 3
	p = 0
	s = 0
	prefix = []
	suffix = []
	for (idx, val) in enumerate(a):
		j = n - idx - 1
		p += val
		s += a[j]
		if p == t:
			prefix.append(idx)
		if s == t:
			suffix.append(j)
	suffix = suffix[::-1]
	res = 0
	j = 0
	suffix_size = len(suffix)
	for (idx, val) in enumerate(prefix):
		while j < suffix_size and val >= suffix[j] - 1:
			j += 1
		res += suffix_size - j
	print(res)


n = int(input())
arr = list(map(int, input().split()))
summ = 0
for i in arr:
	summ = summ + i
if summ % 3 != 0:
	print('0')
elif summ == 0:
	count = 0
	cum = 0
	for i in arr:
		cum = cum + i
		if cum == 0:
			count = count + 1
	if count >= 3:
		ans = int((count - 1) * (count - 2) / 2)
		print(int(ans))
	else:
		print(0)
else:
	k1 = summ / 3
	k2 = k1 * 2
	k3 = summ
	ck1 = 0
	ck2 = 0
	ck3 = 0
	cum = 0
	for i in arr:
		cum = cum + i
		if cum == k3:
			ck3 = ck3 + ck2
			ck2 = 0
		if cum == k2:
			ck2 = ck1 + ck2
		if cum == k1:
			ck1 = ck1 + 1
	ck1 = 0
	print(int(ck3))


class Solve:

	def __init__(self, n):
		self.n = n
		self.ls = list(map(int, input().split()))

	def solution(self):
		s = sum(self.ls)
		ans = 0
		if not s % 3:
			p = s // 3
			v = s - p
			s = t = 0
			self.ls.pop()
			for x in self.ls:
				s += x
				if s == v:
					ans += t
				if s == p:
					t += 1
		print(ans)
obj = Solve(int(input()))
obj.solution()


n = int(input())
ans = 0
a = []
l = 0
if n < 3:
	print(0)
	quit()
for (i, e) in enumerate(map(int, input().split())):
	if i == 0:
		a.append(e)
		continue
	a.append(e + a[i - 1])
s = a[-1]
if s == 0:
	m = a.count(0) - 2
	ans = m * (m + 1) // 2
	if ans < 0:
		ans = 0
	print(ans)
	quit()
if s % 3 != 0:
	print(0)
	quit()
for e in a:
	if e == s // 3:
		l += 1
	if e == 2 * s // 3:
		ans += l
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
i = 1
s = sum(a)
if s % 3 != 0:
	print(0)
	exit()
if n < 3:
	print(0)
	exit()
t = int(s / 3)
ie = 0
second = [0 for x in a]
for i in range(n - 1):
	ie += a[i]
	if ie == 2 * t:
		second[i] = 1
for i in range(n - 2, -1, -1):
	second[i] += second[i + 1]
first = 0
answer = 0
for i in range(0, n - 2):
	first += a[i]
	if first == t:
		answer += second[i + 1]
print(answer)


def sol(a, n):
	s = sum(a)
	dp = [0 for i in range(n)]
	if s % 3 != 0:
		return 0
	s //= 3
	ss = 0
	for i in range(n - 1, -1, -1):
		ss += a[i]
		if ss == s:
			dp[i] = 1
	for i in range(n - 2, -1, -1):
		dp[i] += dp[i + 1]
	ans = 0
	ss = 0
	for i in range(n - 2):
		ss += a[i]
		if ss == s:
			ans += dp[i + 2]
	return ans
n = int(input())
a = [int(i) for i in input().split()]
print(sol(a, n))


n = int(input())
lst = list(map(int, input().split()))
s = sum(lst)
(a, b, c) = (0, 0, 0)
if s % 3 == 0:
	r = s // 3
	r1 = s - r
	for i in range(n - 1):
		c = c + lst[i]
		if c == r1:
			a += b
		if c == r:
			b += 1
print(a)


def main():
	n = int(input())
	a = list(map(int, input().split()))
	s = sum(a)
	if s % 3 != 0:
		print(0)
		return
	p = [a[0]]
	for x in a[1:]:
		p.append(p[-1] + x)
	r = [a[-1]]
	for x in a[-2::-1]:
		r.append(r[-1] + x)
	r = r[::-1]
	z = [0] * n
	for (i, x) in enumerate(r):
		if x * 3 == s:
			z[i] = 1
	i = n - 2
	while i >= 0:
		z[i] += z[i + 1]
		i -= 1
	ans = 0
	for (i, x) in enumerate(p):
		if x * 3 == s and i + 2 < n:
			ans += z[i + 2]
	print(ans)
main()


from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
pref = [0 for i in range(n + 123)]
was = defaultdict(int)
for i in range(1, n + 1):
	pref[i] = pref[i - 1] + a[i - 1]
	was[pref[i]] += 1
ans = 0
for i in range(1, n - 1):
	was[pref[i]] -= 1
	if pref[n] == pref[i] * 3:
		ans += was[pref[i] * 2]
		ans -= pref[n] == pref[i] * 2
print(ans)


n = int(input())
lis = list(map(int, input().split()))
pre = [0] * (n + 1)
for i in range(1, n + 1):
	pre[i] = pre[i - 1] + lis[i - 1]
if pre[-1] % 3:
	print(0)
else:
	s = pre[-1] // 3
	ans = t = 0
	for i in range(1, n):
		if pre[i] == 2 * s:
			ans += t
		if pre[i] == s:
			t += 1
	print(ans)


def solve(n, li):
	su = sum(li)
	fact = su / 3
	first = 0
	count = 0
	count_num = 0
	for i in range(n - 1):
		first += li[i]
		if first == 2 * fact:
			count += count_num
		if first == fact:
			count_num += 1
	return count
n = int(input())
li = list(map(int, input().split()))
result = solve(n, li)
print(result)


n = int(input())
mas = list(map(int, input().split(' ')))
mas2 = [0] * n
mas3 = [0] * n
summ = sum(mas)
if summ % 3 != 0:
	print(0)
else:
	summ /= 3
	su = 0
	schet = 0
	for i in range(n):
		su += mas[i]
		if su == summ:
			mas2[i] = 1
	su = 0
	schet = 0
	for i in range(n):
		su += mas[n - i - 1]
		if su == summ:
			schet += 1
		mas3[n - i - 1] = schet
	res = 0
	for i in range(n - 2):
		if mas2[i]:
			res += mas3[i + 2]
	print(res)


n = int(input())
k = list(map(int, input().split()))
ans = 0
s = sum(k)
if s % 3 == 0:
	p = s // 3
	v = s - p
	s = t = 0
	for i in range(n - 1):
		s += k[i]
		if s == v:
			ans += t
		if s == p:
			t += 1
print(ans)


n = int(input())
arr = list(map(int, input().split()))
sum_of_arr = sum(arr)
prefix_sum = 0
total = 0
count = 0
if sum_of_arr % 3:
	print(0)
else:
	for i in range(n - 1):
		prefix_sum += arr[i]
		if prefix_sum == 2 * (sum_of_arr // 3):
			total += count
		if prefix_sum == sum_of_arr // 3:
			count += 1
	print(total)


n = int(input())
a = list(map(int, input().split()))
sum_a = sum(a)
sum_a_3 = sum_a // 3
res = 0
if sum_a % 3 == 0:
	l = []
	r = []
	cnt = 0
	for i in range(0, n - 1):
		cnt += a[i]
		a[i] = cnt
		if cnt == sum_a_3:
			l.append(i)
		if sum_a - cnt == sum_a_3:
			r.append(i + 1)
	j = 0
	for i in l:
		while j < len(r) and i + 2 > r[j]:
			j += 1
		if j < len(r):
			res += len(r) - j
print(res)


from collections import defaultdict, Counter
n = int(input())
arr = [int(i) for i in input().split()]
prefix = []
for (i, num) in enumerate(arr[:-1]):
	if prefix == []:
		prefix.append(num)
	else:
		prefix.append(num + prefix[i - 1])
c = defaultdict(int)
a = sum(arr) // 3
s = sum(arr)
if s % 3 != 0:
	print(0)
	exit(0)
b = 2 * sum(arr) // 3
r = 0
tot = 0
cnt = 0
for p in arr[:-1]:
	r += p
	if r == b:
		tot += cnt
	if r == a:
		cnt += 1
print(tot)


n = int(input())
a = list(map(int, input().split()))
tot = sum(a)
if tot % 3:
	print(0)
else:
	(x, y) = (tot // 3, 2 * tot // 3)
	b = [0] * (n + 1)
	ss = 0
	for i in range(n - 1, -1, -1):
		ss += a[i]
		b[i] = b[i + 1]
		if ss == x:
			b[i] += 1
	ss = 0
	ans = 0
	for i in range(n - 2):
		ss += a[i]
		if ss == x:
			ans += b[i + 2]
	print(ans)


from __future__ import print_function
import sys
from collections import *
from heapq import *
from functools import *
INF = float('inf')
NINF = float('-inf')
try:
	input = raw_input
except:
	pass

def read_string():
	return input()

def read_string_line():
	return [x for x in input().split(' ')]

def read_int_line():
	return [int(x) for x in input().split(' ')]

def read_int():
	return int(input())

class TreeNode(object):

	def __init__(self, x):
		self.val = x
		self.left = None
		self.right = None

	def __str__(self):
		q = deque()
		ans = []
		q.append(self)
		ans.append(self.val)
		while q:
			cur = q.popleft()
			ans.append(cur.left.val if cur.left else 'null')
			ans.append(cur.right.val if cur.right else 'null')
			if cur.left:
				q.append(cur.left)
			if cur.right:
				q.append(cur.right)
		while ans and ans[-1] == 'null':
			ans.pop()
		return str(ans).replace("'null'", 'null')

def toTreeNode(s):
	l = eval(s.replace('null', 'None'))
	n = len(l)
	if not n:
		return None
	root = TreeNode(l[0])
	q = deque()
	q.append(root)
	i = 1
	while q and i < n:
		cur = q.popleft()
		if l[i] != None:
			cur.left = TreeNode(l[i])
			q.append(cur.left)
		i += 1
		if i >= n:
			break
		if l[i] != None:
			cur.right = TreeNode(l[i])
			q.append(cur.right)
		i += 1
	return root

class Djs:

	def __init__(self, n):
		self.w = [1] * n
		self.p = [-1] * n

	def find(self, x):
		p = self.p[x]
		if p == -1:
			return x
		else:
			self.p[x] = self.find(p)
			return self.p[x]

	def union(self, x, y):
		(x, y) = (self.find(x), self.find(y))
		if x == y:
			return
		if self.w[x] > self.w[y]:
			(x, y) = (y, x)
		self.p[x] = y
		self.w[y] += self.w[x]

	def roots(self):
		return [x for (x, y) in enumerate(self.p) if y == -1]

def mat3d(x, y, z, fill=0):
	return [[[fill] * z for _ in range(y)] for _ in range(x)]

def mat2d(h, w, fill=0):
	return [[fill] * w for _ in range(h)]

def matmul(m1, m2, P=0):
	(h1, w1) = (len(m1), len(m1[0]))
	(h2, w2) = (len(m2), len(m2[0]))
	assert w1 == h2
	ans = mat2d(h1, w2)
	if P:
		for i in range(h1):
			for j in range(w2):
				for k in range(w1):
					ans[i][j] = (ans[i][j] + m1[i][k] * m2[k][j]) % P
	else:
		for i in range(h1):
			for j in range(w2):
				for k in range(w1):
					ans[i][j] = ans[i][j] + m1[i][k] * m2[k][j]
	return ans

def mati(n):
	ans = mat2d(n, n)
	for i in range(n):
		ans[i][i] = 1
	return ans

def matpow(m, p, P):
	(h, w) = (len(m), len(m[0]))
	assert h == w
	ans = mati(h)
	base = m
	while p:
		if p % 2 == 1:
			ans = matmul(ans, base, P)
		base = matmul(base, base, P)
		p //= 2
	return ans

def doit():
	n = read_int()
	arr = read_int_line()
	w = len(arr)
	dp = [0] * (w + 1)
	sm = sum(arr)
	if sm % 3 != 0:
		print('0')
		return
	avg = sm // 3
	acc = 0
	for (i, a) in enumerate(arr):
		acc += a
		if acc == avg:
			dp[i + 1] = dp[i] + 1
		else:
			dp[i + 1] = dp[i]
	ans = 0
	acc = 0
	for i in range(w - 1, -1, -1):
		acc += arr[i]
		if acc == avg:
			if avg != 0:
				ans += dp[i]
			elif i > 0:
				ans += dp[i - 1]
	print('%d' % ans)
try:
	while True:
		doit()
except EOFError:
	pass


import os, io
from sys import stdout
import collections
import random
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def binomial_coefficient(n, k):
	if 0 <= k <= n:
		ntok = 1
		ktok = 1
		for t in range(1, min(k, n - k) + 1):
			ntok *= n
			ktok *= t
			n -= 1
		return ntok // ktok
	else:
		return 0

def powerOfK(k, max):
	if k == 1:
		return [1]
	if k == -1:
		return [-1, 1]
	result = []
	n = 1
	while n <= max:
		result.append(n)
		n *= k
	return result

def prefixSum(arr):
	for i in range(1, len(arr)):
		arr[i] = arr[i] + arr[i - 1]
	return arr
n = int(input())
a = list(map(int, input().split()))

def bf(n, arr):
	cnt = 0
	for i in range(len(a) - 2):
		for j in range(i + 1, len(a) - 1):
			if sum(arr[:i + 1]) == sum(arr[i + 1:j + 1]) == sum(arr[j + 1:]):
				cnt += 1
	return cnt

def sol(n, a):
	ps = prefixSum(a)
	target = ps[-1]
	if target % 3 != 0:
		return 0
	else:
		f1 = target // 3
		f2 = f1 * 2
		f3 = f1 * 3
		if f1 == f2 == f3:
			return binomial_coefficient(ps.count(f1) - 1, 2)
		else:
			cnt = 0
			p1 = 0
			for e in ps:
				if e == f1:
					p1 += 1
				elif e == f2:
					cnt += p1
			return cnt
print(sol(n, a))


def fun():
	n = int(input())
	a = input()
	a = a.split(' ')
	cnt = [0] * n
	s = 0
	for i in range(n):
		s = s + int(a[i])
	if s % 3 != 0:
		return 0
	s = s / 3
	t = 0
	for i in range(n - 1, -1, -1):
		t = t + int(a[i])
		if t == s:
			cnt[i] = 1
	for i in range(n - 2, -1, -1):
		cnt[i] += cnt[i + 1]
	ans = 0
	ss = 0
	for i in range(0, n - 2, 1):
		ss += int(a[i])
		if ss == s:
			ans = ans + cnt[i + 2]
	return ans
print(fun())


input()
mat = list(map(int, input().split()))
avg = sum(mat)
if avg % 3:
	print(0)
else:
	avg //= 3
	jumper = counter = answer = 0
	for x in mat:
		jumper += x
		if avg == 0:
			if jumper == 0:
				counter += 1
			continue
		if jumper == avg:
			counter += 1
		if jumper == avg * 2:
			answer += counter
	if avg == 0:
		print((counter - 1) * (counter - 2) // 2)
	else:
		print(answer)


n = int(input())
a = list(map(int, input().split()))
total_sum = sum(a)
curr_sum = 0
idx_count = 0
ans = 0
for i in range(n):
	curr_sum += a[i]
	if total_sum != 0:
		if total_sum == 3 * curr_sum:
			idx_count += 1
		elif total_sum * 2 == 3 * curr_sum:
			ans += idx_count
	elif curr_sum == 0 and i != n - 1:
		idx_count += 1
if total_sum == 0:
	print(idx_count * (idx_count - 1) // 2)
else:
	print(ans)


n = int(input())
a = list(map(int, input().split()))
sum = 0
for x in a:
	sum += x
if sum % 3 != 0 or n < 3:
	print(0)
else:
	(ct, ans, s) = (0, 0, 0)
	for i in range(0, n - 1):
		s += a[i]
		if s == 2 * (sum // 3):
			ans += ct
		if s == sum // 3:
			ct += 1
	print(ans)


n = int(input())
a = [int(x) for x in input().split()]
total = sum(a)
sum = 0
b = 0
c = 0
for i in range(0, n - 1):
	sum += a[i]
	if 3 * sum == 2 * total:
		c = c + b
	if 3 * sum == total:
		b = b + 1
print(c)


import sys
n = int(input())
a = [int(a) for a in input().split()]
s = sum(a)
if s % 3 != 0:
	print(0)
	sys.exit()
counter = a[0]
first = 0
firsts = [0]
lasts = []
for i in range(1, n):
	if counter == s // 3:
		first += 1
	firsts.append(first)
	if counter == 2 * s // 3:
		lasts.append(i)
	counter += a[i]
answer = 0
for i in lasts:
	if i >= 2:
		answer += firsts[i - 1]
print(answer)


n = int(input())
s = input().split()
for i in range(len(s)):
	s[i] = int(s[i])
sm = sum(s)
if sm % 3:
	print(0)
else:
	sm /= 3
	q = []
	for i in s:
		q.append(False)
	t = 0
	for j in range(len(s)):
		i = -j - 1
		t += s[i]
		if t == sm:
			q[i] = True
	r = 0
	for j in range(len(q)):
		i = -j - 1
		if q[i]:
			r += 1
		q[i] = r
	res = 0
	t = 0
	for i in range(len(s) - 2):
		t += s[i]
		if t == sm:
			res += q[i + 2]
	print(res)


n = int(input())
ls = list(map(int, input().split()))
for i in range(1, n):
	ls[i] += ls[i - 1]
(ans, c) = (0, 0)
for i in range(n - 1):
	x = ls[i] * 3
	if x == ls[n - 1] * 2:
		ans += c
	if x == ls[n - 1]:
		c += 1
print(ans)


import math

def c(n, k):
	r = 1
	for i in range(k):
		r *= n - i
	return r // math.factorial(k)
n = int(input())
arr = list(map(int, input().split()))
(sumarr, count, s) = (sum(arr), 0, 0)
if sumarr % 3:
	print('0')
else:
	p = sumarr // 3
	if p == 0:
		dp = 0
		for i in range(n - 1):
			s += arr[i]
			if s == 0:
				dp += 1
		print(c(dp, 2))
	else:
		(dp0, dp1) = ([], [])
		for i in range(n - 1):
			s += arr[i]
			if s == p:
				dp0.append(i)
			elif s == 2 * p:
				dp1.append(i)
		f = 0
		for i in dp1:
			count += f
			for j in dp0[f:]:
				if i > j:
					count += 1
					f += 1
		print(count)


from bisect import bisect_left
n = int(input())
a = list(map(int, input().split()))
asum = sum(a)
if asum % 3 == 0:
	pre = [0]
	for ai in a:
		pre.append(pre[-1] + ai)
	del pre[0]
	pre.append(0)
	i_a = []
	j_a = []
	for k in range(n - 1):
		if pre[k] == asum // 3:
			i_a.append(k)
		if pre[k] == 2 * asum // 3:
			j_a.append(k)
	numways = 0
	for i in i_a:
		jmin = bisect_left(j_a, i + 1)
		numways += len(j_a) - jmin
	print(numways)
else:
	print(0)


import bisect

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	sums = {}
	total = 0
	dp = []
	for i in range(n - 1, -1, -1):
		total += arr[i]
		if total not in sums.keys():
			sums[total] = []
		dp.append(total)
		sums[total].append(i)
	for s in sums.keys():
		sums[s].sort()
	dp.reverse()
	ways = 0
	curr = 0
	for i in range(n - 1):
		curr += arr[i]
		if dp[i + 1] == 2 * curr:
			if curr in sums.keys():
				index = bisect.bisect(sums[curr], i + 1)
				ways += len(sums[curr]) - index
	print(ways)
main()


import math
n = int(input())
a = list(map(int, input().split()))
total = sum(a)
third = int(total / 3)

def search(arr, l, r, val):
	ans = -1
	while l <= r:
		mid = math.ceil((l + r) / 2)
		if arr[mid] >= val:
			l = mid + 1
			ans = mid
		else:
			r = mid - 1
	return ans
if n < 3 or total % 3 != 0:
	print(0)
else:
	left = []
	so_far = a[0]
	for i in range(1, n - 1):
		if so_far == third:
			left.append(i)
		so_far += a[i]
	right = []
	a.reverse()
	so_far = a[0]
	for j in range(1, n - 1):
		if so_far == third:
			right.append(n - 1 - j)
		so_far += a[j]
	counter = 0
	len_right = len(right) - 1
	for i in left:
		idx = search(right, 0, len_right, i)
		counter += idx + 1
	print(counter)


n = int(input())
inputlist = list(map(int, input().split()))
s = 0
for j in inputlist:
	s += j
m = s / 3
if s % 3 != 0:
	print(0)
else:
	ans = 0
	icount = 0
	m = int(s / 3)
	possibleis = []
	possiblejs = []
	sum = 0
	for j in range(n - 1):
		sum += inputlist[j]
		if s - sum == m and j > 0:
			possiblejs.append(j)
			ans += icount
		if sum == m:
			possibleis.append(j)
			icount += 1
	print(ans)


n = int(input())
arr = list(map(int, input().split()))
c1 = 0
summ = sum(arr)
if summ % 3 != 0:
	print(0)
else:
	num1 = summ // 3
	num2 = num1 * 2
	sum = 0
	c1 = 0
	ans = 0
	for i in range(len(arr) - 1):
		sum += arr[i]
		if sum == num2:
			ans += c1
		if sum == num1:
			c1 += 1
	print(ans)


n = int(input())
t = list(map(int, input().split()))
s = sum(t)
if s % 3 != 0:
	print(0)
else:
	cnt = [0] * n
	s = s // 3
	ss = 0
	for j in range(n - 1, -1, -1):
		ss += t[j]
		if ss == s:
			cnt[j] = 1
	for k in range(n - 2, -1, -1):
		cnt[k] += cnt[k + 1]
	ss = 0
	ans = 0
	for i in range(0, n - 2):
		ss += t[i]
		if ss == s:
			ans += cnt[i + 2]
	print(ans)


n = int(input())
lis = list(map(int, input().split()))
su = sum(lis)
ways = 0
if su % 3 != 0 or n < 3:
	print(0)
else:
	su /= 3
	(a, b, c, temp) = (0, 0, 0, 0)
	for i in range(len(lis) - 1):
		a += lis[i]
		if a == 2 * su:
			ways += temp
		if a == su:
			temp += 1
	print(ways)


inp = input().split(' ')
n = inp[0]
ss = 0
a = []
g = []
inp1 = input().split(' ')
p = []
z = 0
for i in range(int(n)):
	a.append(int(inp1[i]))
ans = 0
s = 0
for i in range(len(a)):
	s += a[i]
if s % 3 != 0:
	print('0')
else:
	s //= 3
	ss = 0
	k = 0
	l = 0
	for n in a:
		k += n
		if s == 0:
			if k == 0:
				l += 1
				continue
		if k == s:
			l += 1
		if k == s * 2:
			ans += l
	if s == 0:
		print((l - 1) * (l - 2) // 2)
	else:
		print(ans)


from sys import stdin, stdout
cin = stdin.readline
cout = stdout.write
mp = lambda : list(map(int, cin().split()))
(n,) = mp()
a = mp()
summ = sum(a)
if summ % 3 or n < 3:
	cout('0')
else:
	x = summ // 3
	l = [0] * 3
	oneThird = (0, 1)[a[0] == x]
	summ = a[0]
	ans = 0
	for i in range(1, n - 1):
		summ += a[i]
		if summ == x * 2:
			ans += oneThird
		if summ == x:
			oneThird += 1
	cout(str(ans))


import sys
import os
from io import BytesIO, IOBase

def cel(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return defaultdict(int)

def ddl():
	return defaultdict(list)

def ddd():
	return defaultdict(defaultdict(int))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict
n = ii()
a = li()
sm = sum(a)
if sm % 3 != 0 or n < 3:
	print(0)
	sys.exit()
tmp = 0
s = 0
ans = 0
for i in range(n - 1):
	s += a[i]
	if s == 2 * sm // 3:
		ans += tmp
	if s == sm // 3:
		tmp += 1
print(ans)


from math import inf as inf
from math import *
from collections import *
import sys
input = sys.stdin.readline
t = 1
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	pre = 0
	s = 0
	r = 0
	x = sum(a)
	if x % 3:
		print(0)
		exit(0)
	re = x // 3
	for i in range(n - 1):
		pre += a[i]
		if pre == 2 * re:
			r += s
		if pre == re:
			s += 1
	print(r)


from itertools import accumulate, permutations
R = lambda : map(int, input().split())
n = int(input())
arr = list(R())
sm = sum(arr)
larr = list(accumulate((3 * x == sm for x in accumulate(arr))))
rsm = 0
res = 0
for i in range(n - 1, 1, -1):
	rsm += arr[i]
	if rsm * 3 == sm:
		res += larr[i - 2]
print(res)


n = int(input())
l = list(map(int, input().split()))
a = [l[0]]
d = {}
m = 0
x = 0
ch = 1
for i in range(1, n):
	a.append(l[i] + a[i - 1])
j = a[-1]
if j == 0:
	for i in a:
		if i == 0:
			x += 1
	p = (x - 1) * (x - 2)
	p //= 2
	m += p
elif j % 3 == 0:
	j //= 3
	for i in a:
		if i == j:
			x += 1
		if i == 2 * j:
			m += x
print(m)


from collections import deque, Counter, OrderedDict
from heapq import nsmallest, nlargest
from math import ceil, floor, log, log2, sqrt, gcd, factorial, pow, pi

def binNumber(n, size=4):
	return bin(n)[2:].zfill(size)

def iar():
	return list(map(int, input().split()))

def ini():
	return int(input())

def isp():
	return map(int, input().split())

def sti():
	return str(input())

def par(a):
	print(' '.join(list(map(str, a))))

def tdl(outerListSize, innerListSize, defaultValue=0):
	return [[defaultValue] * innerListSize for i in range(outerListSize)]

def sts(s):
	s = list(s)
	s.sort()
	return ''.join(s)

class pair:

	def __init__(self, f, s):
		self.fi = f
		self.se = s

	def __lt__(self, other):
		return (self.fi, self.se) < (other.fi, other.se)
n = ini()
a = iar()
s = sum(a)
if s % 3 != 0:
	print(0)
	quit()
s = s // 3
(ans, cnt) = (0, 0)
x = 0
for i in range(n - 1):
	x += a[i]
	if x == 2 * s:
		ans += cnt
	if x == s:
		cnt += 1
print(ans)


def solve(n, l):
	s = sum(l)
	if s % 3 != 0:
		return 0
	else:
		t = s // 3
		(i_fi, i_se) = ([], [])
		ps = 0
		for i in range(n - 1):
			ps += l[i]
			if ps == t * 1:
				i_fi.append(i)
			if ps == t * 2:
				i_se.append(i)
		if t == 0:
			return len(i_fi) * (len(i_fi) - 1) // 2
		else:
			out = 0
			i = 0
			for x in i_fi:
				while i < len(i_se) and i_se[i] <= x:
					i += 1
				out += len(i_se) - i
			return out
n = int(input())
l = list(map(int, input().split()))
print(solve(n, l))


n = int(input())
a = list(map(int, input().strip().split(' ')))
sum_part = [a[0]] * n
for i in range(1, n):
	sum_part[i] = sum_part[i - 1] + a[i]
suma = sum_part[n - 1]
if n < 3:
	print(0)
elif suma % 3 != 0:
	print(0)
else:
	(q, ans) = (0, 0)
	for j in range(n - 1):
		if 3 * sum_part[j] == 2 * suma:
			ans += q
		if 3 * sum_part[j] == suma:
			q += 1
	print(ans)


n = int(input())
a = list(map(int, input().split()))
s = []
si = 0
for i in range(n):
	si += a[i]
	s.append(si)
if s[-1] % 3 != 0 or n <= 2:
	print(0)
elif s[n - 1] != 0:
	l = [0] * n
	for j in range(n):
		if s[j] == s[n - 1] // 3:
			l[j] = 1
		if s[j] == s[n - 1] * 2 // 3:
			l[j] = 2
	p = 0
	q = 0
	for k in range(n):
		if l[-1 - k] == 2:
			p += 1
		if l[-1 - k] == 1:
			q += p
	print(q)
else:
	c = s.count(0) - 1
	print(c * (c - 1) // 2)


n = int(input())
arr = [int(i) for i in input().split()]
total = sum(arr)
splits = 0
k = 0
l = 0
a = []
b = []
if total % 3 != 0:
	pass
else:
	for i in range(n):
		k += arr[i]
		if k == total / 3 and i != n - 1:
			a.append(i)
		if k == 2 * total / 3 and i != n - 1:
			b.append(i)
	for i in a:
		while l < len(b) and i >= b[l]:
			l += 1
		splits += len(b) - l
print(splits)


n = int(input())
alist = [int(x) for x in input().split()]
psum = 0
l = 0
lim = sum(alist) // 3
ways = 0
if sum(alist) % 3 != 0:
	print(0)
	quit()
for i in range(n - 1):
	psum += alist[i]
	if psum == 2 * lim:
		ways += l
	if psum == lim:
		l += 1
print(ways)


n = int(input())
a = list(map(int, input().split()))
s = 0
for i in a:
	s += i
if s % 3 == 0:
	(ans, num, one) = (0, 0, 0)
	for i in range(0, len(a)):
		num += a[i]
		if num == int(2 * s / 3) and i > 0 and (i < n - 1):
			ans += one
		if num == int(s / 3):
			one += 1
	print(ans)
else:
	print(0)


n = int(input())
a = list((int(i) for i in input().split()))
s = sum(a)
ans = 0
x = 0
if s % 3 == 0:
	c = s // 3
	b = s - c
	d = 0
	for i in range(n - 1):
		x = x + a[i]
		if x == b:
			ans += d
		if x == c:
			d += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
flag = [0 for i in range(n)]
c = [0 for i in range(n)]
if s % 3 != 0:
	print(0)
else:
	s = s // 3
	t = 0
	for i in range(n - 1, -1, -1):
		t += a[i]
		if t == s:
			flag[i] = 1
	c[n - 1] = flag[n - 1]
	for i in range(n - 2, -1, -1):
		c[i] = c[i + 1] + flag[i]
	t = 0
	ans = 0
	for i in range(n - 2):
		t += a[i]
		if t == s:
			ans += c[i + 2]
	print(ans)


import bisect
n = int(input())
a = [int(x) for x in input().split()]
(s, p, k) = (sum(a), a[0], sum(a) / 3)
(b, c) = ([], [])
for i in range(len(a)):
	if s == k:
		c.append(i)
	if p == k:
		b.append(i)
	s -= a[i]
	if i < n - 1:
		p += a[i + 1]
if len(b) != 0 and b[-1] == n - 1:
	b = b[:-1]
if len(c) != 0 and c[0] == 0:
	c = c[1:]
res = 0
e = len(c)
for i in b:
	res += e - bisect.bisect_left(c, i + 2)
print(res)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
c = [0] * n
if n <= 2 or s % 3:
	print(0)
else:
	(cnt1, cnt2) = (0, 0)
	s1 = s // 3
	s2 = 2 * s1
	cs = 0
	out = 0
	for i in range(len(a)):
		cs += a[i]
		if i < n - 1 and cs == s2:
			out += cnt1
		if cs == s1:
			cnt1 += 1
	print(out)


from itertools import accumulate
n = int(input())
a = list(map(int, input().split()))
r_a = list(reversed(a))
if sum(a) % 3 != 0 or n < 3:
	print(0)
else:
	s = sum(a) // 3
	ans = 0
	c = list(reversed(list(accumulate([1 if e == s else 0 for e in accumulate(r_a)]))))
	for (i, e) in enumerate(accumulate(a)):
		if e == s:
			if i + 2 < n:
				ans += c[i + 2]
	print(ans)


n = int(input())
s = list(map(int, input().split()))
su = sum(s)
p = a = b = 0
if su % 3:
	print(0)
else:
	m = su / 3
	mm = m * 2
	for i in range(n - 1):
		p += s[i]
		if p == mm:
			b += a
		if p == m:
			a += 1
	print(b)


import sys
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split(' ')))

def countways(a, n):
	cnt = [0 for i in range(n)]
	s = 0
	s = sum(a)
	if s % 3 != 0:
		return 0
	s //= 3
	ss = 0
	for i in range(n - 1, -1, -1):
		ss += a[i]
		if ss == s:
			cnt[i] = 1
	for i in range(n - 2, -1, -1):
		cnt[i] += cnt[i + 1]
	ans = 0
	ss = 0
	for i in range(0, n - 2):
		ss += a[i]
		if ss == s:
			ans += cnt[i + 2]
	return ans
print(countways(a, n))


n = int(input())
A = list(map(int, input().split()))
T = [0 for _ in range(n)]
T[n - 1] = A[n - 1]
I = []
G = [0 for _ in range(n)]
for i in range(n - 2, -1, -1):
	T[i] = T[i + 1] + A[i]
if T[0] % 3:
	print(0)
else:
	for i in range(2, n):
		if T[i] == T[0] // 3:
			G[i] = 1
	T[0] = A[0]
	for i in range(1, n):
		T[i] = T[i - 1] + A[i]
	for i in range(n - 2):
		if T[i] == T[n - 1] // 3:
			I += [i]
	s = [0 for _ in range(n)]
	s[n - 1] = G[n - 1]
	for i in range(n - 2, -1, -1):
		s[i] = s[i + 1] + G[i]
	print(sum([s[i + 2] for i in I]))


n = int(input())
a = [int(x) for x in input().split(' ')]
s = sum(a)
if s % 3 == 0:
	(part_1, part_2) = (0, 0)
	curr_sum = 0
	for i in range(len(a) - 1):
		curr_sum += a[i]
		if curr_sum == 2 * s // 3:
			part_2 += part_1
		if curr_sum == s // 3:
			part_1 += 1
	print(part_2)
else:
	print(0)


n = int(input())
a = list(map(int, input().split()))
prefixSum = [0] * n
suffixSum = [0] * n
prefixSum[0] = a[0]
suffixSum[n - 1] = a[n - 1]
sumTotal = 0
i = 1
j = n - 2
while i < n:
	prefixSum[i] = prefixSum[i - 1] + a[i]
	suffixSum[j] = suffixSum[j + 1] + a[j]
	j -= 1
	i += 1
sumPart = prefixSum[n - 1] // 3
startIndices = [0] * n
for i in range(n):
	if i == 0:
		if a[0] == sumPart:
			startIndices[0] = 1
		else:
			startIndices[0] = 0
	else:
		startIndices[i] = startIndices[i - 1]
		if prefixSum[i] == sumPart:
			startIndices[i] += 1
answer = 0
for j in range(n - 1, 1, -1):
	if suffixSum[j] == sumPart:
		answer += startIndices[j - 2]
if sumPart * 3 == prefixSum[n - 1]:
	print(answer)
else:
	print(0)


n = int(input())
arr = list(map(int, input().split()))
fwd = []
s = 0
for i in arr:
	s = s + i
	fwd.append(s)
s = sum(arr)
if s % 3 != 0 or n <= 2:
	print(0)
elif s == 0:
	cc = fwd.count(0)
	if cc == 3:
		print(1)
	elif cc > 3:
		cc = cc - 2
		print(int(cc / 2 * (cc + 1)))
	else:
		print(0)
else:
	ans = 0
	count = 0
	flag = 0
	for i in range(0, n - 1):
		if fwd[i] == s / 3 and flag == 1:
			count = 1
			flag = 0
		elif fwd[i] == 2 * s / 3:
			ans = ans + count
			flag = 1
		if fwd[i] == s / 3:
			count += 1
	print(ans)


n = int(input())
if n < 3:
	print(0)
	exit()
l = [int(x) for x in input().split()]
sl = sum(l)
s = int(sl / 3)
if 3 * s != sl:
	print(0)
	exit()
(a, c) = (l[0], l[-1])
b = 3 * s - a - c
total = 0
left = []
sum = 0
for x in range(n - 2):
	sum += l[x]
	if sum == s:
		left += [x]
if len(left) == 0:
	print(0)
	exit()
sum = 0
for y in range(n - 1, 1, -1):
	sum += l[y]
	if sum == s:
		while len(left) > 0 and y - left[-1] <= 1:
			left.pop()
		if len(left) == 0:
			break
		total += len(left)
print(total)


from sys import stdin
n = int(input())
a = list(map(int, stdin.readline().rstrip().split(' ')))
r = [0] * n
s = 0
z = sum(a)
if z % 3:
	print(0)
else:
	z = z // 3
	for i in range(n - 1, -1, -1):
		s += a[i]
		if s == z:
			r[i] = 1
	for i in range(n - 2, -1, -1):
		r[i] += r[i + 1]
	ans = 0
	ss = 0
	for i in range(n - 2):
		ss += a[i]
		if ss == z:
			ans += r[i + 2]
	print(ans)


n = int(input())
nums = list(map(int, input().split()))
t = sum(nums)
c = 0
o = 0
if t % 3 != 0:
	print(0)
else:
	s = 0
	for i in range(n - 1):
		s += nums[i]
		if s * 3 == t * 2:
			o += c
		if s * 3 == t and i != n - 2:
			c += 1
	print(o)


def calc(arr, n, total):
	if n < 2 or total % 3:
		return 0
	s = total // 3
	s2 = s * 2
	ret = 0
	t = 0
	c = 0
	for num in arr[:-1]:
		c += num
		if c == s2:
			ret += t
		if c == s:
			t += 1
	return ret
n = int(input())
arr = list(map(int, input().split()))
total = sum(arr)
print(calc(arr, n, total))


n = int(input())
li = list(map(int, input().split()))
s = sum(li) / 3
presum = 0
ans = 0
cnt = 0
for i in range(len(li) - 1):
	presum += li[i]
	if presum == 2 * s:
		ans += cnt
	if presum == s:
		cnt += 1
print(ans)


n = int(input())
arr = list(map(int, input().split()))
sums = sum(arr)
two = 0
if sums % 3 == 0:
	s = sums / 3
	cur = 0
	k = s * 2
	one = 0
	for x in arr[:-1]:
		cur = cur + x
		if cur == k:
			two += one
		if cur == s:
			one += 1
print(two)


l = int(input())
a = list(map(int, input().split()))

def res():
	s = sum(a)
	if s % 3 != 0:
		return 0
	else:
		s3 = s // 3
		r = set()
		rsum = 0
		for i in range(l - 1, 0, -1):
			rsum += a[i]
			if rsum == s3:
				r.add(i)
		lsum = 0
		ans = 0
		for i in range(l):
			lsum += a[i]
			if i + 1 in r:
				r.remove(i + 1)
			if lsum == s3:
				ans += len(r)
		return ans
print(res())


n = int(input())
arr = list(map(int, input().split(' ')))
sumArr = []
mSum = 0
for (index, elem) in enumerate(arr):
	mSum += elem
	sumArr.append((index, mSum))
mSum = sumArr[-1][1]
if mSum % 3 != 0:
	print(0)
else:
	mSum = int(mSum / 3)
	theSum = 0
	cnt = 0
	cntArr = []
	mlen = len(arr)
	for i in range(mlen):
		theSum += arr[mlen - 1 - i]
		if theSum == mSum:
			cnt += 1
		cntArr.append(cnt)
	sumArr = filter(lambda x: x[1] == mSum, sumArr)
	theSum = 0
	variants = 0
	for elem in sumArr:
		if elem[0] < mlen - 2:
			variants += cntArr[mlen - elem[0] - 3]
	print(variants)


n = int(input())
arr = list(map(int, input().split()))
s = sum(arr)
w = 0
if not s % 3:
	s = s / 3
	t = s * 2
	(u, g) = (0, 0)
	for x in arr[:-1]:
		u += x
		if u == t:
			w += g
		if u == s:
			g += 1
print(w)


n = int(input())
a = [int(x) for x in input().split()]
s = sum(a)
if s % 3 != 0:
	print(0)
else:
	s1 = s / 3
	s2 = 2 * s1
	tick1 = tick2 = num = 0
	for i in range(n - 1):
		tick2 += a[i]
		if tick2 == s2:
			num += tick1
		if tick2 == s1:
			tick1 += 1
	print(num)


def fun(n):
	return int(n * (n + 1) // 2)
n = int(input())
arr = [int(x) for x in input().split()]
if sum(arr) % 3 == 0:
	th = int(sum(arr) / 3)
	pre = [arr[0]]
	suf = [arr[-1]]
	for i in range(1, n):
		pre.append(pre[-1] + arr[i])
	for i in range(n - 2, -1, -1):
		suf.append(suf[-1] + arr[i])
	suf = suf[::-1]
	preT = [0]
	if pre[0] == th:
		preT[0] = 1
	sufT = [0]
	if suf[-1] == th:
		sufT[0] = 1
	for i in range(1, n):
		preT.append(preT[-1])
		if pre[i] == th:
			preT[-1] += 1
	for i in range(n - 2, -1, -1):
		sufT.append(sufT[-1])
		if suf[i] == th:
			sufT[-1] += 1
	sufT = sufT[::-1]
	ways = 0
	for i in range(n - 2):
		if pre[i] == th:
			ways += sufT[i + 2]
	print(ways)
else:
	print(0)


def main():
	import sys
	n = int(sys.stdin.readline())
	t = tuple(map(int, sys.stdin.readline().split()))
	acc = sum(t)
	if acc % 3:
		sys.stdout.write('0')
	else:
		cnt = []
		const = acc // 3
		ss = 0
		for i in t:
			if acc - ss == const:
				cnt.append(1)
			else:
				cnt.append(0)
			ss += i
		for i in range(n - 2, -1, -1):
			cnt[i] += cnt[i + 1]
		ans = 0
		ss = 0
		for i in range(n - 2):
			ss += t[i]
			if ss == const:
				ans += cnt[i + 2]
		sys.stdout.write(str(ans) + '\n')
main()


def main():
	from sys import stdin, stdout
	it = iter(map(int, stdin.read().split()))
	n = next(it)
	cumSum = [0] * (n + 3)
	for i in range(1, n + 1):
		cumSum[i] = cumSum[i - 1] + next(it)
	totSum = cumSum[n]
	if n < 3 or totSum % 3 != 0:
		ans = 0
	elif totSum == 0:
		zeroCnt = 0
		for x in cumSum[1:n + 1]:
			if x == 0:
				zeroCnt += 1
		zeroCnt -= 2
		ans = int(zeroCnt * (zeroCnt + 1) / 2) if zeroCnt > 0 else 0
	else:
		segSum = totSum // 3
		segSumTwice = segSum * 2
		cnt = 0
		ans = 0
		for i in range(1, n):
			if cumSum[i] == segSum:
				cnt += 1
			if cumSum[i] == segSumTwice:
				ans += cnt
	stdout.write(str(ans) + '\n')
main()


n = int(input())
arr = list(map(int, input().split()))
if sum(arr) % 3 != 0:
	print(0)
else:
	s = sum(arr) // 3
	cnt = 0
	answer = 0
	sum1 = 0
	for i in range(0, n - 1):
		sum1 = sum1 + arr[i]
		if sum1 == 2 * s:
			answer = answer + cnt
		if sum1 == s:
			cnt = cnt + 1
	print(answer)


import sys, math, itertools, random, bisect
from collections import defaultdict
INF = 10 ** 18

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()
mod = 10 ** 9 + 7
test = 1
for _ in range(test):
	(n,) = get_ints()
	a = get_array()
	tot = sum(a)
	if tot % 3 != 0:
		print(0)
	else:
		tot = tot // 3
		cur = 0
		cnt = [0] * n
		for i in range(n - 1, -1, -1):
			cur += a[i]
			if cur == tot:
				cnt[i] = 1
		for i in range(n - 2, -1, -1):
			cnt[i] += cnt[i + 1]
		ans = 0
		cur = 0
		for i in range(n - 2):
			cur += a[i]
			if cur == tot:
				ans += cnt[i + 2]
		print(ans)


n = int(input())
a = [0]
a.extend([int(x) for x in input().split()])
prefix_sum = [0] * (n + 1)
for i in range(1, n + 1):
	prefix_sum[i] = prefix_sum[i - 1] + a[i]
if prefix_sum[n] % 3 != 0:
	print(0)
else:
	s = ans = 0
	for i in range(1, n + 1):
		if i > 1 and i < n and (prefix_sum[i] == prefix_sum[n] * 2 // 3):
			ans += s
		if prefix_sum[i] == prefix_sum[n] // 3:
			s += 1
	print(ans)


n = int(input())
c = list(map(int, input().split()))
k = sum(c)
if k % 3 == 0 and n >= 3:
	k = k // 3
	a = [0] * n
	b = [0] * n
	s = 0
	for i in range(n - 1, 1, -1):
		s += c[i]
		if s == k:
			a[i] = 1
	b[-1] = a[-1]
	for i in range(n - 2, -1, -1):
		b[i] = b[i + 1] + a[i]
	s = 0
	l = 0
	for i in range(n - 2):
		s += c[i]
		if s == k:
			l += b[i + 2]
	print(l)
else:
	print(0)


n = int(input())
s = list(map(int, input().split()))
sum = 0
for i in s:
	sum += i
if sum % 3 != 0:
	print(0)
else:
	x = sum // 3
	ans = 0
	v = 0
	y = [0] * n
	y[0] = s[0]
	for i in range(1, n):
		y[i] = y[i - 1] + s[i]
	for i in range(n - 1):
		if y[i] == sum - x:
			ans += v
		if y[i] == x:
			v += 1
	print(ans)


n = int(input())
c = list(map(int, input().split()))
F = []
F.append(c[0])
for i in range(1, n):
	F.append(c[i] + F[i - 1])
sum = F[n - 1]
if sum % 3 != 0:
	print(0)
elif sum != 0:
	temp = 0
	count = 0
	for i in range(0, n - 2):
		if F[i] == sum // 3:
			temp = temp + 1
		if F[i] == 2 * sum // 3:
			count = count + temp
	if F[n - 2] == 2 * sum // 3:
		count = count + temp
	print(count)
else:
	temp = 0
	for i in range(0, n - 1):
		if F[i] == 0:
			temp = temp + 1
	temp = temp * (temp - 1) // 2
	print(temp)


n = int(input())
arr = list(map(int, input().split()))
psum = [0] * n
t = 0
for i in range(n):
	t += arr[i]
	psum[i] = t
if psum[-1] % 3 != 0:
	print(0)
elif psum[-1] == 0:
	cnt = psum.count(0)
	cnt -= 1
	if cnt < 2:
		print(0)
	elif cnt == 2:
		print(1)
	else:
		print(cnt * (cnt - 1) // 2)
else:
	ans = 0
	x = psum[-1] // 3
	x2 = x * 2
	x3 = x * 3
	(xc, x2c, x3c) = (0, 0, 0)
	for i in range(n):
		if psum[i] == x:
			xc += 1
		if psum[i] == x2:
			ans += xc
	print(ans)


pp = int(input())
a = [int(i) for i in input().split()]
b = [0 for i in range(pp)]
s = 0
k = sum(a)
a1 = k // 3
s = 0
ss = 0
sss = 0
if k % 3 == 0:
	for i in range(pp - 1):
		s = s + a[i]
		if s == 2 * a1:
			ss = sss + ss
		if s == a1:
			sss = sss + 1
print(ss)


n = int(input())
a = []
p = 0
for i in map(int, input().split()):
	a.append(p + i)
	p += i
cnt = 0
cnt = 0
ans = 0
for i in range(n - 1):
	if a[i] * 3 == a[n - 1] * 2:
		ans += cnt
	if a[i] * 3 == a[n - 1]:
		cnt += 1
print(ans)


from itertools import accumulate
n = int(input())
(a, ans, c) = (list(accumulate(map(int, input().split()))), 0, 0)
if a[n - 1] % 3 == 0:
	for i in range(n - 1):
		if a[i] == 2 * a[n - 1] // 3:
			ans += c
		if a[i] == a[n - 1] // 3:
			c += 1
print(ans)


n = input()
a = [int(i) for i in input().split()]
k = 0
b = 0
c = 0
s = 0
if sum(a) % 3 == 0:
	k = sum(a) / 3
	for i in a[:-1]:
		s += i
		if s == 2 * k:
			b += c
		if s == k:
			c += 1
print(b)


def main():
	n = int(input())
	a = list(map(int, input().split()))
	s = sum(a)
	if s % 3 != 0:
		print(0)
		return
	else:
		cnt = [0 for _ in range(n)]
		cnts = 0
		for i in range(n - 1, -1, -1):
			cnts += a[i]
			if cnts == s // 3:
				cnt[i] = 1
		for i in range(n - 2, -1, -1):
			cnt[i] += cnt[i + 1]
		cnts = 0
		ans = 0
		for i in range(n):
			cnts += a[i]
			if i + 2 < n and cnts == s // 3:
				ans += cnt[i + 2]
		print(ans)
		return
main()


n = int(input())
a = list(map(int, input().split()))
if sum(a) % 3 != 0:
	print(0)
else:
	req = sum(a) // 3
	(f, s) = ([], [])
	pref = [0] * n
	for i in range(n):
		if i == 0:
			pref[i] = a[i]
		else:
			pref[i] = pref[i - 1] + a[i]
		if pref[i] == req:
			f.append(i)
		if pref[i] == 2 * req and i != n - 1:
			s.append(i)
	(i, j, ans) = (0, 0, 0)
	while i < len(f):
		while j < len(s) and s[j] <= f[i]:
			j += 1
		ans += len(s) - j
		i += 1
	print(ans)


import sys
inf = float('inf')
(mod, MOD) = (1000000007, 998244353)

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
n = int(input())
Arr = get_array()
sumx = sum(Arr)
ans = 0
if sumx % 3 == 0:
	s = 0
	count = 0
	lsum = sumx // 3
	rsum = 2 * lsum
	for i in Arr[:-1]:
		s += i
		if s == rsum:
			ans += count
		if s == lsum:
			count += 1
print(ans)


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase

def main():
	n = int(input())
	numbers = tuple(map(lambda x: int(x), input().split()))
	suffix = [0] * n
	accumulated = 0
	for i in range(n - 1, -1, -1):
		accumulated += numbers[i]
		suffix[i] = accumulated
	numOfWays = 0
	accumulated = numbers[0]
	amount = {accumulated: 1}
	for i in range(1, n - 1):
		accumulated += numbers[i]
		if accumulated / 2 == suffix[i + 1] and accumulated / 2 in amount:
			numOfWays += amount[accumulated / 2]
		if accumulated in amount:
			amount[accumulated] += 1
		else:
			amount[accumulated] = 1
	print(numOfWays)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
input = lambda : sys.stdin.readline().rstrip('\r\n')

def print(*args, **kwargs):
	sep = kwargs.pop('sep', ' ')
	file = kwargs.pop('file', sys.stdout)
	atStart = True
	for x in args:
		if not atStart:
			file.write(sep)
		file.write(str(x))
		atStart = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
main()


n = int(input())
L = list(map(int, input().split(' ')))
ans = 0
for i in range(n - 1):
	L[i + 1] = L[i + 1] + L[i]
if L[n - 1] % 3 == 0:
	c = 0
	s = L[n - 1] // 3
	v = 2 * s
	ans = 0
	for i in range(0, n - 1):
		if L[i] == v:
			ans = ans + c
		if L[i] == s:
			c = c + 1
print(ans)


def find_f(j, t, start):
	for i in range(start, len(t)):
		if t[i] > j:
			return i
	return -1
n = int(input())
if n > 2:
	sums = [0 for i in range(n)]
	st = input().split()
	sums[0] = int(st[0])
	for i in range(n - 1):
		sums[i + 1] = sums[i] + int(st[i + 1])
	s = sums[n - 1] / 3.0
	s_end = sums[n - 1] * 2.0 / 3.0
	q = []
	t = []
	fin = 0
	start = 0
	k = 0
	if s == 0:
		for i in range(n):
			if sums[i] == 0:
				k += 1
		print(int((k - 2) * (k - 1) / 2))
	else:
		for i in range(n):
			if s == sums[i]:
				q.append(i)
			if s_end == sums[i]:
				t.append(i)
		t_length = len(t)
		for j in q:
			g = find_f(j, t, start)
			if g == -1:
				break
			else:
				fin += t_length - g
				start = g
		print(fin)
else:
	print('0')


import sys
n = int(sys.stdin.readline())
a = [int(x) for x in sys.stdin.readline().split()]
s = [a[0]]
sucet = a[0]
for i in range(1, len(a)):
	s.append(s[-1] + a[i])
	sucet += a[i]
if sucet % 3 != 0:
	print(0)
else:
	moznosti = 0
	dvoj = 2 * (sucet // 3)
	kdedvoj = [(0, 1)[s[0] == dvoj]]
	for i in range(1, len(s)):
		kdedvoj.append(kdedvoj[-1] + (0, 1)[s[i] == dvoj])
	if sucet == 0:
		if kdedvoj[-1] - 2 < 1:
			print(0)
		elif kdedvoj[-1] - 2 == 1:
			print(1)
		else:
			print((kdedvoj[-1] ** 2 - 3 * kdedvoj[-1] + 2) // 2)
	else:
		j = n
		for i in range(len(a)):
			if s[i] == sucet // 3:
				j = i
				break
		k = -1
		for i in range(len(a) - 1, -1, -1):
			if s[i] == sucet:
				k = i
				break
		for i in range(j, k):
			if s[i] == sucet // 3:
				moznosti += kdedvoj[-1] - kdedvoj[i]
		print(moznosti)


n = int(input())
a = list(map(int, input().split()))
if a == [0] * n:
	print(int((n - 2) * (n - 1) / 2))
else:
	(b, c, d, e) = (sum(a), 0, 0, 0)
	f = []
	if b % 3 != 0:
		print(0)
	else:
		for i in range(n - 2):
			c += a[i]
			if c == b / 3:
				d = i + 1
				c = 0
				break
		if d == 0:
			print(0)
		else:
			for j in range(n - d - 1):
				c += a[j + d]
				if c == b / 3:
					f += [j + d + 1]
					e += 1
			if e == 0:
				print(0)
			else:
				c = 0
				g = e
				for i in range(n - d - 2):
					c += a[i + d]
					if c == 0:
						for j in range(e):
							if 1 + i + d < f[j]:
								g += e - j
								break
				print(g)


n = int(input())
a = [int(i) for i in input().split()]
s = sum(a)
if s % 3 != 0:
	print(0)
	exit()
count = [0] * n
s //= 3
ss = 0
for i in range(n - 1, -1, -1):
	ss += a[i]
	if ss == s:
		count[i] = 1
sums = [0] * n
sums[-1] = count[-1]
for i in range(n - 2, -1, -1):
	sums[i] = sums[i + 1] + count[i]
ans = 0
ss = 0
for i in range(n - 2):
	ss += a[i]
	if ss == s:
		ans += sums[i + 2]
print(ans)


t = int(input())
nums = [int(i) for i in input().split()]
SUM = sum(nums)
if SUM % 3 != 0:
	print(0)
else:
	one_third = SUM // 3
	two_third = one_third * 2
	count = 0
	answer = 0
	temp_sum = 0
	for i in range(len(nums) - 1):
		temp_sum += nums[i]
		if temp_sum == two_third:
			answer += count
		if temp_sum == one_third:
			count += 1
	print(answer)


import sys
import itertools
import math
import collections
from collections import Counter

def pow(x, y, mod):
	r = 1
	x = x % mod
	while y > 0:
		if y & 1:
			r = r * x % mod
		y = y >> 1
		x = x * x % mod
	return r

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	prime[0] = prime[1] = False
	r = [p for p in range(n + 1) if prime[p]]
	return r

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return collections.defaultdict(int)
n = ii()
d = li()
if sum(d) % 3 != 0:
	exit(print(0))
t = sum(d) // 3
pre = [d[0]]
for i in range(1, n):
	pre.append(pre[-1] + d[i])
h = [int(pre[0] == 2 * t)]
for i in range(1, n - 1):
	h.append(h[-1] + (pre[i] == 2 * t))
res = 0
for i in range(n - 2):
	if pre[i] == t:
		res += h[n - 2] - h[i]
print(res)


n = int(input())
line = list(map(int, input().split()))
m = sum(line)
if m % 3 != 0:
	print(0)
	exit(0)
for i in range(n):
	if i == 0:
		continue
	line[i] += line[i - 1]
a = b = 0
for i in line[:-1]:
	if i == m // 3:
		a += 1
	if i == m // 3 * 2:
		b += a
if m == 0:
	print(a * (a - 1) // 2)
else:
	print(b)


n = input()
l = input()
l = l.split(' ')
x = 0
m = 0
for i in range(0, len(l)):
	l[i] = int(l[i])
	m += l[i]
	l[i] = m
m = m / 3
ans = 0
for i in range(0, len(l) - 2):
	if l[i] == m:
		x += 1
	if l[i + 1] == 2 * m:
		ans += x
print(ans)


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt, pi, cos, sin, ceil, log2
from bisect import bisect_right, bisect_left
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)

def sumDigits(no):
	return 0 if no == 0 else int(no % 10) + sumDigits(int(no / 10))
MOD = 1000000007
PMOD = 998244353
N = 10 ** 9 + 7
T = 1
for _ in range(T):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().rstrip().split()))
	p = [0] * n
	p[0] = a[0]
	for i in range(1, n):
		p[i] = p[i - 1] + a[i]
	if p[-1] % 3 != 0:
		print(0)
		continue
	di = {}
	dj = {}
	for i in range(n):
		if 3 * p[i] == p[-1]:
			di[i] = 1
	for j in range(n):
		if 3 * p[j] == 2 * p[-1]:
			if j != n - 1:
				dj[j] = 1
	ja = list(dj.keys())
	ia = list(di.keys())
	ia.sort()
	ja.sort()
	nj = len(ja)
	ans = 0
	for i in ia:
		ix = bisect_right(ja, i)
		ans += nj - ix
	print(ans)