C. Number of Ways

problem_index: C

time_limit: 2 seconds

memory_limit: 256 megabytes

solutions: ["n = int(input())\nl = list(map(int, input().split()))\ns = 0\ns3 = 0\ns32 = 0\nsu = sum(l)\nif su % 3 != 0:\n\tprint(0)\n\texit(0)\nfor i in range(0, n - 1):\n\ts += l[i]\n\tif s == 2 * su / 3:\n\t\ts32 += s3\n\tif s == su / 3:\n\t\ts3 += 1\nprint(s32)\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\nif s % 3 != 0:\n\tprint('0')\n\tquit()\ns = s // 3\nc = 0\nf = [0] * len(a)\nb = [0] * len(a)\nfor i in range(len(a)):\n\tc += a[i]\n\tif c == s:\n\t\tf[i] = 1\n\tif c == 2 * s:\n\t\tif i < len(a) - 1:\n\t\t\tb[i + 1] = 1\nfor i in range(len(a) - 2, -1, -1):\n\tb[i] += b[i + 1]\ncnt = 0\nc = 0\nfor i in range(0, len(f) - 2):\n\tif f[i] == 1:\n\t\tcnt += b[i + 2]\nprint(cnt)\n", "def f(A):\n\tsum_ = sum(A)\n\tif sum_ % 3 != 0:\n\t\treturn 0\n\tsum3 = sum(A) // 3\n\tC = [0] * len(A)\n\tS = [-1] * len(A)\n\tprefix = 0\n\tfor i in range(len(A) - 1, -1, -1):\n\t\tprefix += A[i]\n\t\tif prefix == sum3:\n\t\t\tC[i] = 1\n\t\tS[i] = C[i]\n\t\tif i < len(A) - 1:\n\t\t\tS[i] += S[i + 1]\n\tcount = 0\n\tprefix = 0\n\tfor i in range(len(A) - 2):\n\t\tprefix += A[i]\n\t\tif prefix == sum3:\n\t\t\tcount += S[i + 2]\n\treturn count\nassert f([1, 1, 1]) == 1\nassert f([2, 1, 1, 2]) == 1\nassert f([1, 1, 2, 1, 1]) == 1\nassert f([1, 2, 3, 0, 3]) == 2\nassert f([0, 1, -1, 0]) == 1\nassert f([4, 1]) == 0\nn = int(input())\nA = list(map(int, input().split()))\nans = f(A)\nprint(ans)\n", "n = int(input())\nline = [int(it) for it in str(input()).split()]\nsv = sum(line)\nif n < 3 or sv % 3 > 0:\n\tprint(0)\nelse:\n\trefer = list()\n\tv = 0\n\tfor it in line:\n\t\tv += it\n\t\trefer.append(v)\n\tsv0 = sv // 3\n\tsv1 = sv0 * 2\n\tpine = list()\n\tresult = 0\n\tfor i in range(n - 1):\n\t\tif refer[i] == sv1:\n\t\t\tresult += len(pine)\n\t\tif refer[i] == sv0:\n\t\t\tpine.append(i)\n\tprint(result)\n", "import itertools\nn = int(input())\na = list(map(int, input().split()))\nv = sum(a)\nif v % 3:\n\tprint(0)\n\texit(0)\ns = 0\nsums = [0] * n\nsuffix_count = 0\nfor i in reversed(range(0, n)):\n\ts += a[i]\n\tsuffix_count += 1 if s == v // 3 else 0\n\tsums[i] = suffix_count\ncount = 0\ns = a[0]\nfor i in range(1, n - 1):\n\tif s == v // 3:\n\t\tcount += sums[i + 1]\n\ts += a[i]\nprint(count)\n", "n = int(input())\nlst = list(map(int, input().split()))\nc = t = 0\ns = sum(lst)\nif s % 3 == 0:\n\tsone = s // 3\n\tstwo = s - sone\n\ts = 0\n\tfor x in lst[:n - 1]:\n\t\ts += x\n\t\tif s == stwo:\n\t\t\tc += t\n\t\tif s == sone:\n\t\t\tt += 1\nprint(c)\n", "n = int(input())\nnum = input()\nlist = [int(x) for x in num.split()]\ntol = sum(list)\ns = 0\nt = 0\nw = 0\nif tol % 3 != 0:\n\tprint(0)\nelse:\n\tc = int(tol / 3)\n\tfor i in range(n - 1):\n\t\ts += list[i]\n\t\tif s == 2 * c and 0 < i:\n\t\t\tw += t\n\t\tif s == c and i < n - 2:\n\t\t\tt += 1\n\tprint(w)\n", "a = int(input())\nlist = list(map(int, input().split()))\ns = sum(list)\nif s % 3 != 0 or a < 3:\n\tprint(0)\nelse:\n\tnum = 0\n\tanswer = 0\n\tss = 0\n\tfor i in range(a - 1):\n\t\tss += list[i]\n\t\tif ss == s * 2 / 3:\n\t\t\tanswer += num\n\t\tif ss == s / 3:\n\t\t\tnum += 1\n\tprint(answer)\n", "import math\nimport sys\nfrom collections import defaultdict\nfrom collections import Counter\nfrom collections import deque\nimport bisect\ninput = iter(sys.stdin.buffer.read().decode().splitlines()).__next__\nilele = lambda : map(int, input().split())\nalele = lambda : list(map(int, input().split()))\n\ndef list2d(a, b, c):\n\treturn [[c] * b for i in range(a)]\n\ndef list3d(a, b, c, d):\n\treturn [[[d] * c for j in range(b)] for i in range(a)]\nN = int(input())\nA = alele()\nS = sum(A)\nif S % 3 != 0 or N < 3:\n\tprint(0)\nelse:\n\tx = S // 3\n\tpre = [0]\n\tt = 0\n\tfor i in A:\n\t\tt += i\n\t\tpre.append(t)\n\tcount = 0\n\tu = 0\n\tB = []\n\tc = 0\n\tfor i in range(N - 1, -1, -1):\n\t\tu += A[i]\n\t\tif u == x:\n\t\t\tc += 1\n\t\tB.append(c)\n\tB = B[::-1] + [0]\n\tfor i in range(N - 2):\n\t\ta = pre[i + 1]\n\t\tb = S - a\n\t\tif a == x and b == 2 * x:\n\t\t\tcount += B[i + 2]\n\tprint(count)\n", "n = int(input())\na = [int(x) for x in input().split(' ')]\ns = sum(a)\ncnt = 0\nif s % 3 == 0:\n\ts = int(s / 3)\n\tw = 0\n\tot = 0\n\tfor x in a[:-1]:\n\t\tw += x\n\t\tif w == 2 * s:\n\t\t\tcnt += ot\n\t\tif w == s:\n\t\t\tot += 1\nprint(cnt)\n", "n = int(input())\nline = input().split()\naccumulated = [0] * n\ncurr_sum = 0\nindex = 0\nfor num in line:\n\tcurr_sum += int(num)\n\taccumulated[index] = curr_sum\n\tindex += 1\ntotal = accumulated[n - 1]\nthird = total // 3\nif total % 3 == 0:\n\ti_possibilities = 0\n\tj_possibilities = 0\n\ttotal_decrease = 0\n\tcurr_decrease = 0\n\tfor i in range(0, n - 1):\n\t\tnum = accumulated[i]\n\t\tif i != 0 and num == 2 * third:\n\t\t\tj_possibilities += 1\n\t\t\tcurr_decrease += 1\n\t\tif i != n - 2 and num == third:\n\t\t\ti_possibilities += 1\n\t\t\ttotal_decrease += curr_decrease\n\tprint(i_possibilities * j_possibilities - total_decrease)\nelse:\n\tprint(0)\n", "n = int(input())\na = list(map(int, input().split()))\ns0 = sum(a)\nif s0 % 3 != 0:\n\tprint(0)\nelse:\n\ts = s0 // 3\n\td = [0 for i in range(n)]\n\ttemp = a[-1]\n\td[-1] = 1 if temp == s else 0\n\tfor i in range(2, n + 1):\n\t\ttemp += a[-i]\n\t\td[-i] = d[-i + 1] + (1 if temp == s else 0)\n\tk = 0\n\ttemp = 0\n\tfor i in range(n - 2):\n\t\ttemp += a[i]\n\t\tif temp == s:\n\t\t\tk += d[i + 2]\n\tprint(k)\n", "I = lambda : list(map(int, input().split()))\n(n,) = I()\nl = I()\nf = sum(l) // 3\nif sum(l) % 3:\n\tprint(0)\nelse:\n\tst = j = -1\n\ts = i = 0\n\tct = an = 0\n\twhile i < n - 1:\n\t\ts += l[i]\n\t\tif s == 2 * f:\n\t\t\tj = i\n\t\t\tan += ct\n\t\tif s == f:\n\t\t\tst = i\n\t\t\tct += 1\n\t\ti += 1\n\tif st == -1 or j == -1:\n\t\tprint(0)\n\telse:\n\t\tprint(an)\n", "n = int(input())\narr = list(map(int, input().split(' ')))\n_sum = sum(arr)\ntmp = [0] * n\ncount = 0\nif _sum % 3:\n\tcount = 0\nelse:\n\tprefix_sum = [0] * n\n\tprefix_sum[0] = arr[0]\n\tfor i in range(1, n):\n\t\tprefix_sum[i] = prefix_sum[i - 1] + arr[i]\n\tk = 0\n\ttrisum = _sum // 3\n\tfor i in range(n - 2, 0, -1):\n\t\tif prefix_sum[i] == trisum * 2:\n\t\t\tk += 1\n\t\ttmp[i] = k\n\tfor j in range(n - 3, -1, -1):\n\t\tif prefix_sum[j] == trisum:\n\t\t\tcount += tmp[j + 1]\nprint(count)\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\nk = 0\nif n > 3 and s % 3 == 0:\n\ts //= 3\n\tc = [0] * n\n\tt = 0\n\tfor i in range(n):\n\t\tt += a[n - i - 1]\n\t\tif t == s:\n\t\t\tc[n - i - 1] = 1\n\tfor i in range(n - 1):\n\t\tc[n - i - 2] += c[n - i - 1]\n\tt = 0\n\tfor i in range(n - 2):\n\t\tt += a[i]\n\t\tif t == s:\n\t\t\tk += c[i + 2]\nprint(k)\n", "test = int(input())\nli = list(map(int, input().split()))\nsumm = sum(li)\nif summ % 3 != 0:\n\tprint(0)\nelse:\n\tx = 0\n\ts1 = 0\n\ts2 = 0\n\tz = []\n\tfor i in li:\n\t\tx += i\n\t\tif x == summ / 3:\n\t\t\ts1 += 1\n\t\t\tz.append((1, s2))\n\t\telif x == summ / 3 * 2 and s1 > 0:\n\t\t\ts2 += 1\n\t\t\tz.append((0, 0))\n\t\telse:\n\t\t\tz.append((0, 0))\n\tsu = 0\n\tif summ == 0:\n\t\tprint(int((s1 - 1) * (s1 - 2) / 2))\n\telse:\n\t\tfor j in z:\n\t\t\tif j[0]:\n\t\t\t\tsu += j[0] * (s2 - j[1])\n\t\tprint(su)\n", "n = int(input())\nnumbers = [int(x) for x in input().split(' ')]\ncnt = [0] * n\nss = 0\nif sum(numbers) % 3 != 0:\n\tprint(0)\nelse:\n\ts = sum(numbers) / 3\n\tfor i in range(n - 1, -1, -1):\n\t\tss += numbers[i]\n\t\tif s == ss:\n\t\t\tcnt[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tcnt[i] += cnt[i + 1]\n\tans = 0\n\tss = 0\n\tfor i in range(0, n - 2, 1):\n\t\tss += numbers[i]\n\t\tif ss == s:\n\t\t\tans += cnt[i + 2]\n\tprint(ans)\n", "def calc():\n\ta = int(input())\n\tlista = list(map(int, input().split()))\n\tsoma = sum(lista)\n\tresus = [0 for i in range(a + 10)]\n\tif soma % 3 == 0 and a >= 3:\n\t\tsoma /= 3\n\t\tss = 0\n\t\tfor i in range(a - 1, 0, -1):\n\t\t\tss += lista[i]\n\t\t\tif ss == soma:\n\t\t\t\tresus[i] = 1\n\t\tfor i in range(a - 2, 0, -1):\n\t\t\tresus[i] += resus[i + 1]\n\t\tcont = 0\n\t\tss = 0\n\t\tfor i in range(a):\n\t\t\tss += lista[i]\n\t\t\tif ss == soma:\n\t\t\t\tcont += resus[i + 2]\n\t\treturn cont\n\treturn 0\nprint(calc())\n", "def numberofways(arr, n):\n\ts = sum(arr)\n\tif s % 3 != 0:\n\t\treturn 0\n\ts /= 3\n\tanswer = 0\n\tprefixsum = 0\n\tcount_s = 0\n\tfor i in range(n - 1):\n\t\tprefixsum += arr[i]\n\t\tif prefixsum == 2 * s:\n\t\t\tanswer += count_s\n\t\tif prefixsum == s:\n\t\t\tcount_s += 1\n\treturn answer\nn = int(input())\narr = list(map(int, input().split()))\nprint(numberofways(arr, n), end='')\n", "n = int(input())\na = input()\nlist_a = list(map(int, a.split()))\narray = []\nfor i in range(len(list_a)):\n\tif i == 0:\n\t\tarray.append(list_a[i])\n\telse:\n\t\tarray.append(array[i - 1] + list_a[i])\nx = array[-1]\n\ndef useful_function(x, array):\n\tif x % 3 != 0:\n\t\treturn 0\n\telif x != 0:\n\t\tmarks = 0\n\t\ta = int(x / 3)\n\t\tb = int(2 * x / 3)\n\t\tc = 0\n\t\tfor i in range(len(array)):\n\t\t\tif array[i] == a:\n\t\t\t\tc += 1\n\t\t\tif array[i] == b:\n\t\t\t\tmarks += c\n\t\treturn marks\n\telse:\n\t\tm = array.count(0)\n\t\tif m == len(array):\n\t\t\treturn int((m - 1) * (m - 2) / 2)\n\t\tm -= 1\n\t\tif m % 2 == 0:\n\t\t\treturn int(m / 2) ** 2\n\t\telse:\n\t\t\ta = int(m / 2)\n\t\t\treturn a * (a + 1)\nprint(useful_function(x, array))\n", "n = int(input())\nl = list(map(int, input().split()))\ncount = [0] * n\ns = sum(l)\nif s % 3 != 0:\n\tprint(0)\nelse:\n\ts //= 3\n\tss = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tss += l[i]\n\t\tif ss == s:\n\t\t\tcount[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tcount[i] += count[i + 1]\n\t(ans, ss) = (0, 0)\n\tfor i in range(n - 2):\n\t\tss += l[i]\n\t\tif ss == s:\n\t\t\tans += count[i + 2]\n\tprint(ans)\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tcnt = 0\n\tans = 0\n\tres = 0\n\ts //= 3\n\tfor i in range(n - 1):\n\t\tcnt += a[i]\n\t\tif cnt == 2 * s:\n\t\t\tans += res\n\t\tif cnt == s:\n\t\t\tres += 1\n\tprint(ans)\n", "from math import factorial\nn = int(input())\nval = list(map(int, input().split()))\ntotal = sum(val)\nflag = True\ncount1 = 0\ncount2 = 0\nresult = 0\nif total % 3 != 0:\n\tflag = False\n\tprint(0)\nelif n < 3:\n\tprint(0)\nelse:\n\tfor i in range(1, n):\n\t\tval[i] += val[i - 1]\n\tif total != 0:\n\t\tfor i in range(n - 2, -1, -1):\n\t\t\tif val[i] == 2 * total / 3:\n\t\t\t\tcount2 += 1\n\t\t\tif val[i] == total / 3:\n\t\t\t\tresult += count2\n\t\tprint(result)\n\telse:\n\t\tfor i in range(n - 1):\n\t\t\tif val[i] == 0:\n\t\t\t\tcount1 += 1\n\t\tif n > 2:\n\t\t\tres = int(factorial(count1) / (factorial(2) * factorial(count1 - 2)))\n\t\t\tprint(res)\n\t\telse:\n\t\t\tprint(0)\n", "from bisect import bisect_left\n\ndef bini(a, x):\n\ti = bisect_left(a, x)\n\treturn i\na = int(input())\nz = list(map(int, input().split()))\nif sum(z) % 3 != 0:\n\tprint(0)\nelse:\n\tt = sum(z) // 3\n\ttotal = 0\n\tcount = 0\n\tsave = []\n\tsave1 = []\n\tans = []\n\tfor i in range(len(z)):\n\t\ttotal = total + z[i]\n\t\tif total == t and i != len(z) - 1:\n\t\t\tsave.append(i)\n\t\tif total == 2 * t and i != 0 and (i != len(z) - 1):\n\t\t\tsave1.append(i)\n\tfor i in range(len(save1)):\n\t\tans.append(bini(save, save1[i]))\n\tprint(sum(ans))\n", "n = int(input())\narray = list(map(int, input().split()))\nsum = sum(array)\nif sum % 3 != 0 and sum != 0:\n\tprint(0)\n\texit()\nneed = sum / 3\nfirst_partition = 0\nsecond_partition = 0\nanswer = 0\nfor i in range(0, n - 1):\n\tif i > 0:\n\t\tarray[i] += array[i - 1]\n\tif array[i] == need + need:\n\t\tanswer += first_partition\n\tif array[i] == need:\n\t\tfirst_partition += 1\nprint(answer)\n", "n = int(input())\na = list(map(int, input().split()))\nh = 0\nsums = 0\nfzeropoint = 0\nminus = 0\nresult = 0\nj = 0\nl = n - 1\ni = 0\nfor y in range(n):\n\th = h + a[y]\nif h % 3 != 0 or n < 3:\n\tprint(0)\nelse:\n\tfor k in range(n):\n\t\tsums = sums + a[k]\n\t\tif sums == h // 3:\n\t\t\tfzeropoint = fzeropoint + 1\n\t\t\tbreak\n\tsums = 0\n\twhile l > k:\n\t\tsums = sums + a[l]\n\t\tif sums == h // 3:\n\t\t\tbreak\n\t\tl = l - 1\n\tsums = 0\n\ti = k + 1\n\tj = l - 1\n\twhile i < l - 1:\n\t\tsums = sums + a[i]\n\t\tif sums == 0:\n\t\t\tfzeropoint = fzeropoint + 1\n\t\ti = i + 1\n\t\tminus = sums\n\tif l != k:\n\t\tresult = fzeropoint + result\n\tsums = 0\n\twhile j > k + 1:\n\t\tif minus == 0:\n\t\t\tfzeropoint = fzeropoint - 1\n\t\tminus = minus - a[j - 1]\n\t\tsums = sums + a[j]\n\t\tif sums == 0:\n\t\t\tresult = result + fzeropoint\n\t\tj = j - 1\n\tprint(result)\n", "s = input()\ns1 = input()\nN = int(s)\nkarl = [int(i) for i in s1.split(' ')]\nleft = [0 for i in karl]\nright = [0 for i in karl]\nk = 0\nsuma = sum(karl[:])\nleg = 0\nif suma % 3 != 0:\n\tprint(0)\nelse:\n\tfor i in range(0, N):\n\t\tleg = leg + karl[i]\n\t\tif leg == suma / 3:\n\t\t\tleft[i] = 1\n\tleg = 0\n\tfor i in range(N - 1, -1, -1):\n\t\tleg = karl[i] + leg\n\t\tif leg == suma / 3:\n\t\t\tright[i] = 1\n\tl = 0\n\tright[0] = right[1] = left[N - 1] = left[N - 2] = 0\n\tright.append(0)\n\tfo4ik = sum(right[:])\n\tfor i in range(0, N):\n\t\tif left[i] == 1:\n\t\t\tk = k + fo4ik - right[i] - right[i + 1]\n\t\tif right[i] == 1:\n\t\t\tfo4ik = fo4ik - 1\n\tprint(k)\n", "n = int(input())\narr = list(map(int, input().split()))\ntotal = sum(arr)\nans = 0\nif total % 3 != 0:\n\tprint(0)\nelse:\n\tc = 0\n\ts = 0\n\tonethird = total // 3\n\ttwothird = 2 * onethird\n\tfor i in range(n - 1):\n\t\ts += arr[i]\n\t\tif s == twothird:\n\t\t\tans += c\n\t\tif s == onethird:\n\t\t\tc += 1\n\tprint(ans)\n", "from itertools import accumulate\nn = int(input())\na = list(map(int, input().split()))\nif sum(a) % 3 != 0 or n < 3:\n\tprint(0)\nelse:\n\tc = list(accumulate(a))\n\tx = 0\n\tk = 0\n\tfor _ in range(len(c) - 1):\n\t\tif c[_] * 3 == c[-1] * 2:\n\t\t\tx += k\n\t\tif c[_] * 3 == c[-1]:\n\t\t\tk += 1\n\tprint(x)\n", "n = int(input())\na = input()\nA = list(map(int, list(a.split())))\np = sum(A)\nif p % 3 != 0:\n\tprint(0)\nelse:\n\tk = p // 3\n\tf = 0\n\tif k == 0:\n\t\ts = 0\n\t\tfor i in range(n - 1):\n\t\t\ts = s + A[i]\n\t\t\tif s == 0:\n\t\t\t\tf = f + 1\n\t\tprint(f * (f - 1) // 2)\n\telse:\n\t\tpre = [0] * n\n\t\tpre[0] = A[0]\n\t\tfor i in range(1, n):\n\t\t\tpre[i] = pre[i - 1] + A[i]\n\t\ti = 0\n\t\tc = 0\n\t\tf = 0\n\t\twhile i < n:\n\t\t\tif pre[i] == k:\n\t\t\t\tf = f + 1\n\t\t\telif pre[i] == 2 * k:\n\t\t\t\tc = c + f\n\t\t\ti = i + 1\n\t\tprint(c)\n", "n = int(input())\nl = list(map(int, input().split()))\ns = 0\na = list()\nfor i in range(0, n):\n\ta.append(s + l[i])\n\ts = s + l[i]\nif a[-1] % 3 != 0:\n\tprint(0)\nelse:\n\ts = 0\n\tsix = [0] * n\n\tf = 2 * (a[-1] // 3)\n\tf2 = a[-1] // 3\n\tans = 0\n\tfor i in range(1, n - 1):\n\t\tif a[n - i - 1] == f:\n\t\t\tans = ans + 1\n\t\t\tsix[n - i - 1] = ans\n\t\telse:\n\t\t\tsix[n - 1 - i] = ans\n\tfor i in range(0, n - 2):\n\t\tif a[i] == f2:\n\t\t\ts = s + six[i + 1]\n\tprint(s)\n", "n = int(input())\nt = [0] + list(map(int, input().split()))\nfor i in range(n):\n\tt[i + 1] += t[i]\np = [0] * (n + 1)\ns = t[-1]\na = s // 3\nb = s - a\nif s == 3 * a:\n\tfor i in range(1, n - 1):\n\t\tp[i] += p[i - 1]\n\t\tif t[i] == a:\n\t\t\tp[i] += 1\nprint(sum((p[i] for i in range(1, n - 1) if t[i + 1] == b)))\n", "def countSplitWays(values):\n\ttotal = sum(values)\n\tcurr_sum = 0\n\tcount = [0] * len(values)\n\tfor i in range(len(values) - 1, -1, -1):\n\t\tcurr_sum = curr_sum + values[i]\n\t\tif curr_sum == total / 3:\n\t\t\tcount[i] = 1\n\tfor index in range(len(values) - 2, -1, -1):\n\t\tcount[index] = count[index] + count[index + 1]\n\tresult = 0\n\tcurr_sum = 0\n\tfor pos in range(0, len(count) - 2):\n\t\tcurr_sum = curr_sum + values[pos]\n\t\tif curr_sum == total / 3:\n\t\t\tresult = result + count[pos + 2]\n\treturn result\nn = int(input())\nvalues = [int(x) for x in input().split()]\nprint(countSplitWays(values))\n", "import math, sys\nfrom collections import defaultdict, Counter, deque\nfrom bisect import bisect_left, bisect_right\nINF = float('inf')\nMOD = int(1000000000.0) + 7\nMAX = int(1000000.0) + 1\n\ndef solve():\n\tn = int(input())\n\tarr = array()\n\ttotal = sum(arr)\n\tif total % 3 != 0 or n < 3:\n\t\tprint(0)\n\t\treturn\n\ttarget = total // 3\n\tcnt = [0 for i in range(n)]\n\tsuf = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tsuf += arr[i]\n\t\tif suf == target:\n\t\t\tcnt[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tcnt[i] += cnt[i + 1]\n\tans = pre = 0\n\tfor i in range(0, n - 2):\n\t\tpre += arr[i]\n\t\tif pre == target:\n\t\t\tans += cnt[i + 2]\n\tprint(ans)\n\ndef main():\n\tt = 1\n\tfor _ in range(t):\n\t\tsolve()\n\ndef gcd(a, b):\n\twhile b:\n\t\t(a, b) = (b, a % b)\n\treturn a\n\ndef input():\n\treturn sys.stdin.readline().rstrip('\\n').strip()\n\ndef print(*args, sep=' ', end='\\n'):\n\tfirst = True\n\tfor arg in args:\n\t\tif not first:\n\t\t\tsys.stdout.write(sep)\n\t\tsys.stdout.write(str(arg))\n\t\tfirst = False\n\tsys.stdout.write(end)\nprimes = [1 for i in range(MAX)]\n\ndef sieve():\n\tglobal primes\n\tprimes[0] = primes[1] = 0\n\ti = 2\n\twhile i <= MAX ** 0.5:\n\t\tj = i * i\n\t\twhile primes[i] and j < MAX:\n\t\t\tif j % i == 0:\n\t\t\t\tprimes[j] = 0\n\t\t\tj += i\n\t\ti += 1\n\ndef vars():\n\treturn map(int, input().split())\n\ndef array():\n\treturn list(map(int, input().split()))\nmain()\n", "import sys, os, re, datetime\nfrom collections import *\nfrom bisect import *\n\ndef mat(v, *dims):\n\n\tdef submat(i):\n\t\tif i == len(dims) - 1:\n\t\t\treturn [v for _ in range(dims[-1])]\n\t\treturn [submat(i + 1) for _ in range(dims[i])]\n\treturn submat(0)\n__cin__ = None\n\ndef cin():\n\tglobal __cin__\n\tif __cin__ is None:\n\t\t__cin__ = iter(input().split(' '))\n\ttry:\n\t\treturn next(__cin__)\n\texcept StopIteration:\n\t\t__cin__ = iter(input().split(' '))\n\t\treturn next(__cin__)\n\ndef iarr(n):\n\treturn [int(cin()) for _ in range(n)]\n\ndef farr(n):\n\treturn [float(cin()) for _ in range(n)]\n\ndef sarr(n):\n\treturn [cin() for _ in range(n)]\n\ndef carr(n):\n\treturn input()\n\ndef imat(n, m):\n\treturn [iarr(m) for _ in range(n)]\n\ndef fmat(n, m):\n\treturn [farr(m) for _ in range(n)]\n\ndef smat(n, m):\n\treturn [sarr(m) for _ in range(n)]\n\ndef cmat(n, m):\n\treturn [input() for _ in range(n)]\nn = int(cin())\na = iarr(n)\ntot = 0\nacc = 0\nans = 0\ns = {}\nfor i in range(n):\n\ttot += a[i]\n\tif tot not in s:\n\t\ts[tot] = []\n\ts[tot].append(i)\nfor (k, v) in s.items():\n\tv.sort(key=lambda x: x)\nfor i in range(n):\n\tacc += a[i]\n\tif tot == 3 * acc:\n\t\tif 2 * acc in s:\n\t\t\tv = s[2 * acc]\n\t\t\tj = bisect_right(v, i)\n\t\t\tif j == len(v):\n\t\t\t\tcontinue\n\t\t\tans += len(v) - j - (1 if v[-1] == n - 1 else 0)\nprint(ans)\n", "n = int(input())\narr = list(map(int, input().split()))\narr_sum = sum(arr)\nif arr_sum % 3 != 0:\n\tprint(0)\n\texit()\narr_sum //= 3\ncnt = [0] * n\ncurr = 0\nfor i in range(n - 1, -1, -1):\n\tcurr += arr[i]\n\tif curr == arr_sum:\n\t\tcnt[i] = 1\nfor i in range(n - 2, -1, -1):\n\tcnt[i] += cnt[i + 1]\n(res, curr) = (0, 0)\nfor i in range(n - 2):\n\tcurr += arr[i]\n\tif curr == arr_sum:\n\t\tres += cnt[i + 2]\nprint(res)\n", "import math\nsize = int(input())\narr = [int(j) for j in input().split(' ')]\ntotal = sum(arr)\ni_ways = []\nways = 0\nif total % 3 != 0:\n\tprint(0)\nelse:\n\tt = total / 3\n\tsums = [0] * size\n\ts = 0\n\tfor (i, elem) in enumerate(arr):\n\t\ts += elem\n\t\tsums[i] = s\n\ti_index = []\n\tj_index = []\n\tfor (i, elem) in enumerate(sums):\n\t\tif elem == t and i < size - 2:\n\t\t\ti_index.append(i)\n\t\tif elem == 2 * t and i > 0 and (i < size - 1):\n\t\t\tj_index.append(i)\n\tways = 0\n\tlast_index = 0\n\tfor i in i_index:\n\t\tfor j in range(last_index, len(j_index)):\n\t\t\tif i < j_index[j]:\n\t\t\t\tlast_index = j\n\t\t\t\tlast_diff = len(j_index) - j\n\t\t\t\tways += last_diff\n\t\t\t\tbreak\n\tprint(ways)\n", "import bisect\nn = int(input())\narr = [int(x) for x in input().split()]\nall_sum = sum(arr)\nif all_sum % 3 == 0:\n\tf_sum = all_sum // 3\n\ts_sum = f_sum * 2\n\tf_ind = []\n\ts_ind = []\n\tlocal_sum = 0\n\tfor i in range(n - 1):\n\t\tlocal_sum += arr[i]\n\t\tif local_sum == f_sum and i < n - 2:\n\t\t\tf_ind += [i]\n\t\tif local_sum == s_sum:\n\t\t\ts_ind += [i]\n\tans = 0\n\tl = len(s_ind)\n\tfor i in f_ind:\n\t\tfr = bisect.bisect(s_ind, i)\n\t\tans += l - fr\n\tprint(ans)\nelse:\n\tprint(0)\n", "(z, zz) = (input, lambda : list(map(int, z().split())))\nzzz = lambda : [int(i) for i in stdin.readline().split()]\n(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))\nfrom string import *\nfrom collections import *\nfrom queue import *\nfrom sys import *\nfrom collections import *\nfrom math import *\nfrom heapq import *\nfrom itertools import *\nfrom bisect import *\nfrom collections import Counter as cc\nfrom math import factorial as f\nfrom bisect import bisect as bs\nfrom bisect import bisect_left as bsl\nfrom itertools import accumulate as ac\n\ndef lcd(xnum1, xnum2):\n\treturn xnum1 * xnum2 // gcd(xnum1, xnum2)\n\ndef prime(x):\n\tp = ceil(x ** 0.5) + 1\n\tfor i in range(2, p):\n\t\tif x % i == 0 and x != 2 or x == 0:\n\t\t\treturn 0\n\treturn 1\n\ndef dfs(u, visit, graph):\n\tvisit[u] = True\n\tfor i in graph[u]:\n\t\tif not visit[i]:\n\t\t\tdfs(i, visit, graph)\nn = int(z())\nl = zzz()\ns = sum(l)\nans = 0\nif s % 3 == 0:\n\ta = s // 3\n\tb = s - a\n\t(s, d) = (0, 0)\n\tfor i in l[:-1]:\n\t\ts += i\n\t\tif s == b:\n\t\t\tans += d\n\t\tif s == a:\n\t\t\td += 1\nprint(ans)\n", "import sys\nimport math as mt\ninput = sys.stdin.buffer.readline\nimport bisect\nt = 1\nfor __ in range(t):\n\tn = int(input())\n\tl = list(map(int, input().split()))\n\tpref = [0] * (n + 1)\n\tpref[0] = l[0]\n\td1 = {}\n\tfor i in range(1, n):\n\t\tpref[i] = pref[i - 1] + l[i]\n\t\tif d1.get(pref[i], -1) == -1:\n\t\t\td1[pref[i]] = []\n\t\td1[pref[i]].append(i)\n\tsuffix = [0] * (n + 1)\n\tsuffix[n - 1] = l[n - 1]\n\td = {}\n\tsuma = sum(l[:])\n\td[l[-1]] = []\n\td[l[-1]].append(n - 1)\n\tfor j in range(n - 2, -1, -1):\n\t\tsuffix[j] = suffix[j + 1] + l[j]\n\t\tif d.get(suffix[j], -1) == -1:\n\t\t\td[suffix[j]] = []\n\t\td[suffix[j]].append(j)\n\tfor i in d:\n\t\td[i].sort()\n\tans = 0\n\tfor i in range(n - 1):\n\t\tif d.get(pref[i], -1) != -1 and pref[i] * 3 == suma:\n\t\t\tj = bisect.bisect(d[pref[i]], i)\n\t\t\tif j < len(d[pref[i]]):\n\t\t\t\tans += len(d[pref[i]]) - j\n\t\t\t\tif d[pref[i]][j] == i + 1:\n\t\t\t\t\tans -= 1\n\tprint(ans)\n", "n = int(input())\narr = list(map(int, input().split()))\nif sum(arr) % 3:\n\tprint(0)\nelif len(arr) < 3:\n\tprint(0)\nelse:\n\td = sum(arr) // 3\n\tpre = []\n\tr = []\n\th = []\n\ts = 0\n\tcount = 0\n\tfor j in range(len(arr)):\n\t\ts += arr[j]\n\t\tpre.append(s)\n\t\th.append(count)\n\t\tif s == d:\n\t\t\tcount += 1\n\ti = 0\n\twhile i < n:\n\t\tif pre[i] == 2 * d:\n\t\t\tr.append(i)\n\t\ti += 1\n\tans = 0\n\tfor elem in r:\n\t\tif elem != n - 1:\n\t\t\tans += h[elem]\n\tprint(ans)\n", "n = int(input())\na = [0 for i in range(n + 1)]\nmp = map(int, input().split())\nind = 1\nsum = 0\nfor j in mp:\n\tsum += j\n\ta[ind] = j\n\tind += 1\ncount = 0\nif sum % 3 != 0:\n\tprint(0)\nelse:\n\tneed = sum // 3\n\tn_cnt = 0\n\tss = 0\n\tfor i in range(1, n):\n\t\tss += a[i]\n\t\tif ss == need * 2:\n\t\t\tcount += n_cnt\n\t\tif ss == need:\n\t\t\tn_cnt += 1\n\tprint(count)\n", "n = int(input())\nlst = [int(x) for x in input().split()]\n\ndef waysToSplit(n, lst):\n\ts = sum(lst)\n\tif s % 3 != 0:\n\t\treturn 0\n\telse:\n\t\tdp = [0 for x in range(n)]\n\t\ttempS = 0\n\t\tfor i in range(n - 1, -1, -1):\n\t\t\ttempS += lst[i]\n\t\t\tif i < n - 1:\n\t\t\t\tdp[i] = dp[i + 1]\n\t\t\tif tempS == s / 3:\n\t\t\t\tdp[i] += 1\n\t\tans = 0\n\t\ttempS = 0\n\t\tfor i in range(0, n - 2):\n\t\t\ttempS += lst[i]\n\t\t\tif tempS == s / 3:\n\t\t\t\tans += dp[i + 2]\n\t\treturn ans\nprint(waysToSplit(n, lst))\n", "def main():\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\ts = sum(a)\n\tif s % 3 != 0 or len(a) < 3:\n\t\tprint(0)\n\t\treturn\n\t_s = s // 3\n\tsums = [0] * (n + 1)\n\tss = 0\n\tfor i in range(n - 1, 1, -1):\n\t\tss += a[i]\n\t\tsums[i] = sums[i + 1] + int(ss == _s)\n\tres = 0\n\tss = 0\n\tfor i in range(n - 2):\n\t\tss += a[i]\n\t\tif ss == _s:\n\t\t\tres += sums[i + 2]\n\tprint(res)\nmain()\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\nm = 0\nc = 0\nans = 0\nif s % 3 == 0:\n\ts //= 3\n\tfor i in range(n - 1):\n\t\tm += a[i]\n\t\tif m == s * 2:\n\t\t\tans += c\n\t\tif m == s:\n\t\t\tc += 1\nprint(ans)\n", "def numSmall(arr, ele):\n\tleft = 0\n\tright = len(arr) - 1\n\tbest = None\n\twhile left <= right:\n\t\tmid = (left + right) // 2\n\t\tif arr[mid] < ele:\n\t\t\tbest = mid\n\t\t\tleft = mid + 1\n\t\telse:\n\t\t\tright = mid - 1\n\tif best != None:\n\t\treturn best + 1\n\telse:\n\t\treturn -1\nn = int(input())\narr = list(map(int, input().split(' ')))\nif len(arr) <= 2:\n\tprint(0)\nelse:\n\treq = sum(arr) / 3\n\ts = 0\n\tlst = []\n\tfor i in range(len(arr)):\n\t\ts += arr[i]\n\t\tif s == req:\n\t\t\tlst.append(i)\n\ts = 0\n\tlst2 = []\n\tfor j in range(len(arr) - 1, -1, -1):\n\t\ts += arr[j]\n\t\tif s == req:\n\t\t\tlst2.append(j)\n\ts2 = []\n\tfor ele in lst2:\n\t\tif ele > 1:\n\t\t\ts2.append(ele)\n\ts1 = []\n\tfor ele in lst:\n\t\tif ele < len(arr) - 2:\n\t\t\ts1.append(ele)\n\tans = 0\n\tfor j in s2:\n\t\ts = numSmall(s1, j - 1)\n\t\tif s != -1:\n\t\t\tans += s\n\tprint(ans)\n", "n = int(input())\narr = [int(a) for a in input().split()]\nthirds = sum(arr) / 3\nif n < 3:\n\tprint(0)\nelif n == 3:\n\tif sum(arr) == arr[0] * 3:\n\t\tprint(1)\n\telse:\n\t\tprint(0)\nelse:\n\tanswer = 0\n\tcount = 0\n\ttotal = 0\n\tfor index in range(n - 1):\n\t\ttotal += arr[index]\n\t\tif total == 2 * thirds:\n\t\t\tanswer += count\n\t\tif total == thirds:\n\t\t\tcount += 1\n\tprint(answer)\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\nres = 0\nif s % 3 == 0:\n\ts1 = s // 3\n\ts2 = 2 * s1\n\tr = 0\n\tc = 0\n\tfor i in a[:-1]:\n\t\tr += i\n\t\tif r == s2:\n\t\t\tres += c\n\t\tif r == s1:\n\t\t\tc += 1\nprint(res)\n", "n = int(input())\nl = list(map(int, input().split()))\ns = sum(l)\nc = 0\nif s % 3 == 0:\n\tx = s // 3\n\ty = s - x\n\t(s1, t) = (0, 0)\n\tfor i in range(n - 1):\n\t\ts1 += l[i]\n\t\tif s1 == y:\n\t\t\tc += t\n\t\tif s1 == x:\n\t\t\tt += 1\nprint(c)\n", "from bisect import bisect_right\nn = int(input())\narr = list(map(int, input().split()))\narr2 = arr[:]\nleftSum = []\nrightSum = []\nfor x in range(1, n):\n\tarr[x] += arr[x - 1]\ntoGet = arr[-1] / 3\nfor x in range(n):\n\tif arr[x] == toGet:\n\t\tleftSum.append(x)\nif arr2[-1] == toGet:\n\trightSum.append(n - 1)\nfor x in range(n - 2, -1, -1):\n\tarr2[x] += arr2[x + 1]\n\tif arr2[x] == toGet:\n\t\trightSum.append(x)\ntimes = 0\nif len(rightSum) > 0 and len(leftSum) > 0:\n\tfor right in rightSum:\n\t\ti = bisect_right(leftSum, right)\n\t\tif right - leftSum[i - 1] == 0:\n\t\t\ti -= 1\n\t\tif right - leftSum[i - 1] == 1:\n\t\t\ti -= 1\n\t\tif i > 0:\n\t\t\ttimes += i\nprint(times)\n", "n = int(input())\na = list(map(int, input().split()))\nsum_a = sum(a)\nsum_a_3 = sum_a // 3\nres = 0\nif sum_a % 3 == 0:\n\tcnt = 1 if a[0] == sum_a_3 else 0\n\tfor i in range(1, n - 1):\n\t\ta[i] += a[i - 1]\n\t\tif sum_a - a[i] == sum_a_3:\n\t\t\tres += cnt\n\t\tcnt += a[i] == sum_a_3\nprint(res)\n", "test_mode = 0\n\ndef solver(n, r):\n\ttprint('\\n'.join([str(n), str(r)]))\n\tn = int(n)\n\tr = list(map(int, r.split()))\n\ts = sum(r) // 3\n\tcnt = [0] * 1000010\n\tss = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tss += r[i]\n\t\tif ss == s:\n\t\t\tcnt[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tcnt[i] += cnt[i + 1]\n\tans = 0\n\tss = 0\n\tfor i in range(n - 2):\n\t\tss += r[i]\n\t\tif ss == s:\n\t\t\tans += cnt[i + 2]\n\tresult = ans if sum(r) % 3 == 0 else 0\n\treturn result\n\ndef compare_answer(xx, xy):\n\tif xx == xy:\n\t\tprint('Test passed\\n\\n')\n\telse:\n\t\tprint('Failed! Expected   >> {}\\n        Found      >> {}\\n\\n'.format(xy, xx))\n\ndef tprint(something):\n\tif test_mode:\n\t\tprint(something)\nif not test_mode:\n\tprint(solver(input(), input()))\nelse:\n\tcompare_answer(solver(5, '1 2 3 0 3'), 2)\n\tcompare_answer(solver(4, '0 1 -1 0'), 1)\n\tcompare_answer(solver(2, '4 1'), 0)\n\tcompare_answer(solver(9, '0 0 0 0 0 0 0 0 0'), 28)\n", "from itertools import *\n(n, a) = (int(input()), list(map(int, input().split())))\nk = sum(a)\nif k % 3 != 0:\n\tprint(0)\n\tquit()\n(k, p, c) = (k // 3, 0, 0)\nfor r in accumulate(a[:-1]):\n\tif r == 2 * k:\n\t\tp += c\n\tif r == k:\n\t\tc += 1\nprint(p)\n", "n = int(input())\nch = str(input())\nLLL = ch.split()\nL = list()\nL1 = list()\ntotal = 0\nfor c in LLL:\n\tL.append(int(c))\n\ttotal += int(c)\n\tL1.append(total)\nif total % 3 != 0:\n\tprint(0)\nelif total != 0:\n\tc1 = 0\n\tc2 = 0\n\tres = 0\n\tfor i in range(0, n - 1):\n\t\tif L1[i] == total // 3:\n\t\t\tc1 += 1\n\t\tif L1[i] == total // 3 * 2 and i != 0 and (i != n - 1):\n\t\t\tres += c1\n\tprint(res)\nelse:\n\tc1 = 0\n\tc2 = 0\n\tres = 0\n\tfor i in range(n - 1):\n\t\tif L1[i] == 0 and i != 0:\n\t\t\tc2 += c1\n\t\tif L1[i] == 0:\n\t\t\tc1 += 1\n\tprint(c2)\n", "from sys import stdin\ninput = lambda : stdin.readline().strip()\nfrom math import ceil, sqrt, factorial, gcd\nfrom collections import deque\nn = int(input())\nl = list(map(int, input().split()))\nz = sum(l)\nif z and z % 3 == 0 and (n >= 3):\n\ta = z // 3\n\tb = 2 * a\n\tx = 0\n\ty = 0\n\ts = 0\n\tfor i in l:\n\t\ts += i\n\t\tif s == a:\n\t\t\tx += 1\n\t\telif s == b:\n\t\t\ty += x\n\tprint(y)\nelif z == 0:\n\tcount = -1\n\ts = 0\n\tfor i in l:\n\t\ts += i\n\t\tif s == 0:\n\t\t\tcount += 1\n\tprint(count * (count - 1) // 2)\nelse:\n\tprint(0)\n", "n = int(input())\na = list(map(int, input().split()))\nfor i in range(1, n):\n\ta[i] += a[i - 1]\n(x, answer) = (0, 0)\nfor i in range(n - 1):\n\tif a[i] * 3 == a[n - 1] * 2:\n\t\tanswer += x\n\tif a[i] * 3 == a[n - 1]:\n\t\tx += 1\nprint(answer)\n", "n = int(input())\narray = list(map(int, input().split()))\noutput = 0\ntotal = sum(array)\nif total % 3 == 0:\n\tfirst = total // 3\n\tsecond = total - first\n\ttotal = t = 0\n\tfor i in range(n - 1):\n\t\ttotal += array[i]\n\t\tif total == second:\n\t\t\toutput += t\n\t\tif total == first:\n\t\t\tt += 1\nprint(output)\n", "from itertools import accumulate\nn = int(input())\nnums = list(map(int, input().split()))\nsum_accumulate = list(accumulate(nums))\ntotal = sum_accumulate[-1]\nnum_of_ways = 0\nif n >= 3 and total % 3 == 0 and (total // 3 in sum_accumulate) and (2 * total // 3 in sum_accumulate):\n\tif nums == [0] * n:\n\t\tnum_of_ways = (n - 2) * (n - 1) // 2\n\telse:\n\t\tthirds = [0] * (n - 2)\n\t\ttwo_thirds = [0] * (n - 1)\n\t\tfor i in range(n - 1):\n\t\t\tif sum_accumulate[i] == total // 3 and i < n - 2:\n\t\t\t\tthirds[i] = 1\n\t\t\tif sum_accumulate[i] == 2 * total // 3:\n\t\t\t\ttwo_thirds[i] = 1\n\t\ttwo_thirds_sum = [0] * (n - 1)\n\t\ttwo_thirds_sum[-1] = two_thirds[-1]\n\t\tfor i in range(2, n):\n\t\t\ttwo_thirds_sum[-i] = two_thirds_sum[-i + 1] + two_thirds[-i]\n\t\tfor i in range(n - 2):\n\t\t\tif thirds[i] == 1:\n\t\t\t\tnum_of_ways += two_thirds_sum[i + 1]\nprint(num_of_ways)\n", "import os\nimport sys\nfrom math import *\nfrom collections import *\nfrom bisect import *\nfrom io import BytesIO, IOBase\n\ndef vsInput():\n\tsys.stdin = open('input.txt', 'r')\n\tsys.stdout = open('output.txt', 'w')\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\n\ndef value():\n\treturn map(int, input().split())\n\ndef array():\n\treturn [int(i) for i in input().split()]\nn = int(input())\na = array()\ns = sum(a)\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tans = 0\n\tpref = [0] * (n + 1)\n\tfor i in range(1, n + 1):\n\t\tpref[i] = pref[i - 1] + a[i - 1]\n\tgood1 = []\n\tfor i in range(1, n - 1):\n\t\tif pref[i] == s / 3:\n\t\t\tgood1.append(i - 1)\n\tsuff = [0] * (n + 1)\n\tfor i in range(1, n + 1):\n\t\tsuff[i] = suff[i - 1] + a[n - i]\n\tfor i in range(1, n - 1):\n\t\tif suff[i] == s / 3:\n\t\t\tindex = n - i - 2\n\t\t\tans += bisect_right(good1, index)\n\tprint(ans)\n", "n = int(input())\nx = input().split()\na = list((int(x[i]) for i in range(len(x))))\nway = 0\nsuma = sum((a[i] for i in range(len(a))))\nif suma % 3 > 0 or len(a) < 3:\n\tprint(0)\nelse:\n\tsign = 1\n\ttarget = suma // 3\n\tif target == 0:\n\t\tsegment = 0\n\t\tsumseg = 0\n\t\tfor i in range(len(a)):\n\t\t\tsumseg += a[i]\n\t\t\tif sumseg == 0:\n\t\t\t\tsegment += 1\n\t\tway = (segment - 1) * (segment - 2) // 2\n\telse:\n\t\tsumseg = 0\n\t\tlist0 = []\n\t\tdict0 = {}\n\t\tdict1 = {}\n\t\ti = 1\n\t\twhile i < len(a) - 1:\n\t\t\tif a[i] == 0:\n\t\t\t\ti0 = i - 1\n\t\t\t\twhile a[i] == 0:\n\t\t\t\t\ti += 1\n\t\t\t\tdict0[i0] = dict1[i] = i - i0\n\t\t\telse:\n\t\t\t\ti += 1\n\t\ti = 0\n\t\twhile i < len(a) - 2:\n\t\t\tsumseg += a[i]\n\t\t\tif sumseg == target:\n\t\t\t\tlist0.insert(0, i)\n\t\t\t\tif i in dict0.keys():\n\t\t\t\t\ti += dict0[i]\n\t\t\t\t\tcontinue\n\t\t\ti += 1\n\t\tsumseg = 0\n\t\tlist1 = []\n\t\ti = len(a) - 1\n\t\twhile i > 1:\n\t\t\tsumseg += a[i]\n\t\t\tif sumseg == target:\n\t\t\t\tlist1.insert(0, i)\n\t\t\t\tif i in dict1.keys():\n\t\t\t\t\ti -= dict1[i]\n\t\t\t\t\tcontinue\n\t\t\ti -= 1\n\t\tflag = 0\n\t\tsumlist1 = 0\n\t\tsumlist0 = 0\n\t\tfor i in range(len(list1)):\n\t\t\tsumlist1 += 1 if list1[i] not in dict1.keys() else dict1[list1[i]]\n\t\tfor i in range(len(list0)):\n\t\t\tsumlist0 += 1 if list0[i] not in dict0.keys() else dict0[list0[i]]\n\t\tminusadd = 0\n\t\tfor i in range(len(list0)):\n\t\t\tif list0[i] < list1[0]:\n\t\t\t\tbreak\n\t\t\telse:\n\t\t\t\tfor j in range(len(list1)):\n\t\t\t\t\tif list0[i] < list1[j]:\n\t\t\t\t\t\tfor k in range(j):\n\t\t\t\t\t\t\tminusadd += (1 if list1[j] not in dict1.keys() else dict1[list1[j]]) * (1 if list0[i] not in dict0.keys() else dict0[list0[i]])\n\t\t\t\t\t\tbreak\n\t\tway = sumlist1 * sumlist0 - minusadd\n\tprint(way)\n", "n = int(input())\na = list(map(int, input().split()))\ncnt = [0]\nfor i in range(n):\n\tcnt.append(cnt[i] + a[i])\nans = 0\nif cnt[n] % 3 != 0:\n\tprint(0)\nelse:\n\tgoal = cnt[n] // 3\n\tcur = 0\n\tfor i in range(1, n):\n\t\tif cnt[i] == 2 * goal:\n\t\t\tans += cur\n\t\tif cnt[i] == goal:\n\t\t\tcur += 1\n\tprint(ans)\n", "n = int(input().strip())\narr = list(map(int, input().strip().split()))\ns = sum(arr)\nif s % 3 != 0:\n\tprint(0)\nelse:\n\treq = s // 3\n\tsums = [arr[0]]\n\tfor i in range(1, len(arr)):\n\t\tsums.append(sums[i - 1] + arr[i])\n\tif req == 0:\n\t\tn = sums.count(0) - 1\n\t\tprint(n * (n - 1) // 2)\n\telif not (req in sums and req * 2 in sums and (req * 3 in sums)):\n\t\tprint(0)\n\telse:\n\t\tans = 0\n\t\tcount = sums[sums.index(req):].count(req * 2)\n\t\tans += count\n\t\tfor i in range(sums.index(req) + 1, len(sums)):\n\t\t\tif sums[i] == req * 2:\n\t\t\t\tcount -= 1\n\t\t\tif sums[i] == req:\n\t\t\t\tans += count\n\t\tprint(ans)\n", "def formSumArray(l, a, b):\n\tarr = [l[0]]\n\taIndexes = []\n\tbIndexes = []\n\tif arr[-1] == a:\n\t\taIndexes.append(0)\n\tif arr[-1] == b:\n\t\tbIndexes.append(0)\n\tfor i in range(1, len(l)):\n\t\tarr.append(arr[i - 1] + l[i])\n\t\tif arr[-1] == a and i != len(l) - 1:\n\t\t\taIndexes.append(i)\n\t\tif arr[-1] == b and i != len(l) - 1:\n\t\t\tbIndexes.append(i)\n\treturn (arr, aIndexes, bIndexes)\n\ndef next(arr, target):\n\tstart = 0\n\tend = len(arr) - 1\n\tans = -1\n\twhile start <= end:\n\t\tmid = (start + end) // 2\n\t\tif arr[mid] <= target:\n\t\t\tstart = mid + 1\n\t\telse:\n\t\t\tans = mid\n\t\t\tend = mid - 1\n\treturn ans\n\ndef findways(l):\n\ts = sum(l)\n\tif s % 3 != 0:\n\t\treturn 0\n\ta = s // 3\n\tb = a * 2\n\t(sumarray, aIndexes, bIndexes) = formSumArray(l, a, b)\n\tcount = 0\n\tfor aIndex in aIndexes:\n\t\tj = next(bIndexes, aIndex)\n\t\tif j != -1:\n\t\t\tcount += len(bIndexes) - j\n\treturn count\ninput()\nl = list(map(int, input().split()))\nprint(findways(l))\n", "n = int(input())\na = list(map(int, input().split()))\n(s, cb, r, b) = ([0] * n, [0] * n, [0] * n, [0] * n)\n(t, count) = (0, 0)\np = 0\nfor i in range(n):\n\tt += a[i]\n\ts[i] += t\nfor i in range(n):\n\tb[i] = t - p\n\tp += a[i]\nif t % 3 != 0:\n\tprint(0)\nelse:\n\tfor i in range(n):\n\t\tif b[i] == t // 3:\n\t\t\t(cb[i], count) = (1, count + 1)\n\tp = 0\n\tfor i in range(n):\n\t\tr[i] = count - p\n\t\tp += cb[i]\n\tresult = 0\n\tfor i in range(n - 2):\n\t\tif s[i] == t // 3:\n\t\t\tresult += r[i + 2]\n\tprint(result)\n", "n = int(input())\narr = list(map(int, input().strip().split()))\nfor i in range(n):\n\tif i > 0:\n\t\tarr[i] += arr[i - 1]\n(ans, cnt) = (0, 0)\nfor i in range(n - 1):\n\tif arr[i] * 3 == arr[n - 1] * 2:\n\t\tans += cnt\n\tif arr[i] * 3 == arr[n - 1]:\n\t\tcnt += 1\nprint(ans)\n", "(n, a) = (int(input()), list(map(int, input().split())))\n(k, p) = (sum(a), 0)\nif k % 3 == 0:\n\t(k, c, s) = (k // 3, 0, 0)\n\tfor r in a[:-1]:\n\t\ts += r\n\t\tif s == 2 * k:\n\t\t\tp += c\n\t\tc += s == k\nprint(p)\n", "n = int(input())\na = [int(i) for i in input().split()]\ntotalsum = sum(a)\nx = totalsum // 3\nif x * 3 != totalsum:\n\tprint(0)\nelse:\n\tcursum = 0\n\tcount1 = 0\n\tcount = 0\n\tfor i in range(n):\n\t\tcursum += a[i]\n\t\tif cursum == x:\n\t\t\tcount1 += 1\n\t\telif cursum == 2 * x:\n\t\t\tcount += count1\n\tif x == 0:\n\t\tprint((count1 - 2) * (count1 - 1) // 2)\n\telse:\n\t\tprint(count)\n", "n = int(input())\narr = list(map(int, input().split()))\nsumOfArr = sum(arr)\nans = 0\nif sumOfArr % 3 == 0:\n\tpointer1 = sumOfArr // 3\n\tpointer2 = sumOfArr - pointer1\n\tsumOfArr = 0\n\tt = 0\n\tfor i in range(n - 1):\n\t\tsumOfArr += arr[i]\n\t\tif sumOfArr == pointer2:\n\t\t\tans += t\n\t\tif sumOfArr == pointer1:\n\t\t\tt += 1\nprint(ans)\n", "def binarysearch(v, x):\n\tlow = 0\n\thigh = len(v) - 1\n\twhile low <= high:\n\t\tmid = int((low + high) / 2)\n\t\tif v[mid] <= x:\n\t\t\tlow = mid + 1\n\t\telif v[mid] > x and v[mid - 1] <= x:\n\t\t\treturn mid\n\t\telif v[mid] > x and mid == 0:\n\t\t\treturn mid\n\t\telse:\n\t\t\thigh = mid - 1\n\treturn -1\n\ndef CountContiguousParts(arr, n):\n\tcount = 0\n\tprefix = [0 for i in range(n)]\n\tprefix[0] = arr[0]\n\tfor i in range(1, n, 1):\n\t\tprefix[i] = prefix[i - 1] + arr[i]\n\ttotal_sum = prefix[n - 1]\n\tif total_sum % 3 != 0:\n\t\treturn 0\n\tsuffix = [0 for i in range(n)]\n\tsuffix[n - 1] = arr[n - 1]\n\ti = n - 2\n\twhile i >= 0:\n\t\tsuffix[i] = suffix[i + 1] + arr[i]\n\t\ti -= 1\n\tv = []\n\tfor i in range(n):\n\t\tif suffix[i] == int(total_sum / 3):\n\t\t\tv.append(i)\n\tfor i in range(n):\n\t\tif prefix[i] == int(total_sum / 3):\n\t\t\tres = binarysearch(v, i + 1)\n\t\t\tif res != -1:\n\t\t\t\tcount += len(v) - res\n\treturn count\nn = int(input())\narr = list(map(int, input().split()))\nprint(CountContiguousParts(arr, n))\n", "n = int(input())\nnums = [int(x) for x in input().strip().split()]\nsum = 0\nfor x in nums:\n\tsum += x\nif sum % 3 != 0:\n\tprint(0)\n\texit()\nsum /= 3\nt = 0\nct = 0\nres = 0\nfor i in range(0, n - 1):\n\tt += nums[i]\n\tif t == 2 * sum:\n\t\tres += ct\n\tif t == sum:\n\t\tct += 1\nprint(res)\n", "n = int(input())\na = list(map(int, input().split()))\ntotal = sum(a)\nif total % 3 != 0:\n\tprint(0)\nelse:\n\tcount = 0\n\tcnt = [0] * n\n\tsums = [0] * n\n\ts = 0\n\tfor i in range(n - 1, -1, -1):\n\t\ts += a[i]\n\t\tif s == total // 3:\n\t\t\tcnt[i] = 1\n\tsums = [0] * n\n\tsums[-1] = cnt[-1]\n\tfor i in range(n - 2, -1, -1):\n\t\tsums[i] = sums[i + 1] + cnt[i]\n\ts = 0\n\tfor i in range(n - 2):\n\t\ts += a[i]\n\t\tif s == total // 3:\n\t\t\tcount += sums[i + 2]\n\tprint(count)\n", "r = int(input())\nl = list(map(int, input().split()))\ns = sum(l)\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tp = s / 3\n\tq = 2 * p\n\tw = y = x = 0\n\tfor b in range(r - 1):\n\t\tx += l[b]\n\t\tif x == q:\n\t\t\tw += y\n\t\tif x == p:\n\t\t\ty += 1\n\tprint(w)\n", "n = int(input())\na = list(map(int, input().split()))\nb = sum(a)\nif b % 3 == 0:\n\tk1 = b // 3\n\tk2 = 2 * k1\n\t(s, t, f) = (0, 0, 0)\n\tfor i in range(n - 1):\n\t\ts += a[i]\n\t\tif s == k2:\n\t\t\tf += t\n\t\tif s == k1:\n\t\t\tt += 1\n\tprint(f)\nelse:\n\tprint(0)\n", "elements = int(input())\narr = list(map(int, input().split()))\n(arr_sum, answer, count, temp_sum) = (sum(arr), 0, 0, 0)\nif arr_sum % 3 == 0:\n\tarr_sum = arr_sum / 3\n\tfor i in range(elements - 1):\n\t\ttemp_sum = temp_sum + arr[i]\n\t\tif temp_sum == 2 * arr_sum:\n\t\t\tanswer = answer + count\n\t\tif temp_sum == arr_sum:\n\t\t\tcount = count + 1\nprint(answer)\n", "from collections import defaultdict\nn = int(input())\nnumbers = [int(i) for i in input().split(' ')]\ntotal = sum(numbers)\nif total % 3 != 0:\n\tprint(0)\nelse:\n\tcount = 0\n\tprefix_sum = [0] * n\n\tsuffix_sum = [0] * n\n\tsuffix_memo = [0] * (n + 1)\n\ti = 0\n\tj = n - 1\n\twhile i < n - 2 and j > 1:\n\t\tif i == 0:\n\t\t\tprefix_sum[i] = numbers[i]\n\t\t\tsuffix_sum[j] = numbers[j]\n\t\telse:\n\t\t\tprefix_sum[i] = prefix_sum[i - 1] + numbers[i]\n\t\t\tsuffix_sum[j] = suffix_sum[j + 1] + numbers[j]\n\t\tif 3 * suffix_sum[j] == total:\n\t\t\tsuffix_memo[j] = suffix_memo[j + 1] + 1\n\t\telse:\n\t\t\tsuffix_memo[j] = suffix_memo[j + 1]\n\t\ti += 1\n\t\tj -= 1\n\tfor (index, value) in enumerate(prefix_sum):\n\t\tif index >= n - 2:\n\t\t\tbreak\n\t\tif 3 * value == total:\n\t\t\tcount += suffix_memo[index + 2]\n\tprint(count)\n", "from sys import exit\nn = int(input())\np1 = 0\np2 = 0\ntot = 0\nnum = list(map(int, input().split()))\ns = sum(num)\nif s % 3 == 0:\n\t(s1, s2) = (s / 3, 2 * s / 3)\n\tfor u in num[:-1]:\n\t\tp1 += u\n\t\tif p1 == s2:\n\t\t\ttot += p2\n\t\tif p1 == s1:\n\t\t\tp2 += 1\nprint(tot)\n", "def check():\n\tn = int(input())\n\tls = list(map(int, input().split()))\n\ta = []\n\tb = []\n\ts = 0\n\tm = sum(ls)\n\tif m % 3 != 0:\n\t\treturn 0\n\tm = int(sum(ls) / 3)\n\tfor i in range(n):\n\t\ts += ls[i]\n\t\tif s == m and i != n - 1:\n\t\t\ta.append(i)\n\t\tif s == 2 * m and i != n - 1:\n\t\t\tb.append(i)\n\ts = 0\n\tans = 0\n\tB = len(b)\n\tfor i in a:\n\t\twhile s < B and i >= b[s]:\n\t\t\ts += 1\n\t\tans += B - s\n\treturn ans\nprint(check())\n", "import collections\nimport itertools\nfrom functools import lru_cache\nimport sys\nimport math\n\ndef helper():\n\tpass\n\ndef solve():\n\tn = int(input())\n\tarr = list(map(int, input().split(' ')))\n\tsm = sum(arr)\n\tif sm % 3 != 0:\n\t\tprint(0)\n\t\treturn\n\telse:\n\t\tpre = [0 for i in range(n)]\n\t\tsuf = [0 for i in range(n)]\n\t\t(pre[0], suf[n - 1]) = (arr[0], arr[n - 1])\n\t\tfor i in range(1, n):\n\t\t\tpre[i] = arr[i] + pre[i - 1]\n\t\tfor i in range(n - 2, -1, -1):\n\t\t\tsuf[i] = arr[i] + suf[i + 1]\n\t\ttarg = sm // 3\n\t\tcount = 0\n\t\ttargeCount = [0 for i in range(n)]\n\t\tfor i in range(n - 1, -1, -1):\n\t\t\tif suf[i] == targ:\n\t\t\t\ttargeCount[i] = targeCount[i + 1] + 1 if i < n - 1 else 1\n\t\t\telse:\n\t\t\t\ttargeCount[i] = targeCount[i + 1] if i < n - 1 else 0\n\t\tfor i in range(len(arr)):\n\t\t\tif pre[i] == targ:\n\t\t\t\tcount += targeCount[i + 2] if i + 2 < n else 0\n\t\tprint(count)\n\t\treturn\nt = 1\nfor _ in range(t):\n\tsolve()\n", "def solve(a, n):\n\tleftSum = [0] * n\n\trightSum = [0] * n\n\tcnts = [0] * n\n\tans = 0\n\tleftSum[0] = a[0]\n\trightSum[n - 1] = a[n - 1]\n\tfor i in range(1, n):\n\t\tleftSum[i] = leftSum[i - 1] + a[i]\n\tfor i in range(n - 2, -1, -1):\n\t\trightSum[i] = rightSum[i + 1] + a[i]\n\ttotal = leftSum[-1]\n\tif total % 3:\n\t\treturn 0\n\tfor i in range(n - 1, -1, -1):\n\t\tif rightSum[i] == total // 3:\n\t\t\tcnts[i] = 1\n\tsums = [0] * n\n\tsums[n - 1] = cnts[n - 1]\n\tfor i in range(n - 2, -1, -1):\n\t\tsums[i] = sums[i + 1] + cnts[i]\n\tfor i in range(n - 2):\n\t\tif leftSum[i] == total // 3:\n\t\t\tans += sums[i + 2]\n\treturn ans\n\ndef main():\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\tprint(solve(a, n))\nmain()\n", "n = int(input())\nl = [*map(int, input().split())]\ntotal = sum(l)\nif total % 3 != 0:\n\tprint(0)\nelse:\n\tarr = [0] * n\n\tx = 0\n\tval = total // 3\n\tfor i in range(n - 1, -1, -1):\n\t\tx += l[i]\n\t\tif x == val:\n\t\t\tarr[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tarr[i] += arr[i + 1]\n\tans = 0\n\tx = 0\n\tfor i in range(n - 2):\n\t\tx += l[i]\n\t\tif x == val:\n\t\t\tans += arr[i + 2]\n\tprint(ans)\n", "n = int(input())\nl = list(map(int, input().split()))\nif sum(l) % 3 != 0:\n\tprint(0)\n\texit(0)\ncnt = 0\nans = 0\na = 0\nk = sum(l) // 3\nfor i in range(n - 1):\n\ta += l[i]\n\tif a == 2 * k:\n\t\tans += cnt\n\tif a == k:\n\t\tcnt += 1\nprint(ans)\n", "import sys\ninput = sys.stdin.readline\n\ndef inp():\n\treturn int(input())\n\ndef inlt():\n\treturn list(map(int, input().split()))\n\ndef helper(i, _sum):\n\tpending_total = total - _sum\n\tcur_sum = 0\n\tcount = 0\n\twhile i < n - 1:\n\t\tcur_sum += arr[i]\n\t\tif cur_sum == _sum and cur_sum == pending_total - cur_sum:\n\t\t\tcount += 1\n\t\ti += 1\n\treturn count\n\ndef solve():\n\tpartial_sum = total / 3\n\tcur_sum = 0\n\tcount = [0] * (n + 1)\n\tfor i in range(n - 1, -1, -1):\n\t\tcur_sum += arr[i]\n\t\tif cur_sum == partial_sum:\n\t\t\tcount[i] += 1\n\tfor i in range(n - 2, -1, -1):\n\t\tcount[i] += count[i + 1]\n\tcur_sum = 0\n\tans = 0\n\tfor i in range(0, n - 2):\n\t\tcur_sum += arr[i]\n\t\tif cur_sum == partial_sum:\n\t\t\tans += count[i + 2]\n\treturn ans\nn = inp()\narr = inlt()\ntotal = sum(arr)\nif total % 3 != 0:\n\tprint(0)\nelse:\n\tprint(solve())\n", "q = int(input())\nw = list(map(int, input().split()))\nfor i in range(1, q):\n\tw[i] += w[i - 1]\na = 0\nb = 0\nif w[q - 1] % 3 != 0:\n\tprint(0)\nelse:\n\tfor j in range(q - 1):\n\t\tif 3 * w[j] == 2 * w[q - 1]:\n\t\t\ta += b\n\t\tif 3 * w[j] == w[q - 1]:\n\t\t\tb += 1\n\tprint(a)\n", "n = int(input())\nl = list(map(int, input().split()))\ns = sum(l)\nx = s / 3\nif x != int(x):\n\tprint('0')\nelse:\n\tx = int(s / 3)\n\tsum1 = 0\n\ta = [0] * len(l)\n\tfor i in range(len(l) - 1, -1, -1):\n\t\tsum1 = sum1 + l[i]\n\t\tif sum1 == x:\n\t\t\ta[i] = 1\n\tb = a\n\tfor i in range(len(l) - 2, -1, -1):\n\t\tb[i] += b[i + 1]\n\tans = 0\n\ti = 0\n\ttemp = 0\n\twhile i + 2 < n:\n\t\ttemp = temp + l[i]\n\t\tif temp == x:\n\t\t\tans += b[i + 2]\n\t\ti = i + 1\n\tprint(ans)\n", "n = int(input())\na = list(map(int, input().split()))\nb = [a[0]]\nfor i in a[1:]:\n\tb.append(b[-1] + i)\nd = [a[-1]]\nfor i in a[:-1][::-1]:\n\td.append(d[-1] + i)\nd = d[::-1]\nc = [0] * n\ns = b[n - 1]\nif b[0] * 3 == s:\n\tc[0] = 1\nfor i in range(1, n):\n\tif b[i] * 3 == s:\n\t\tc[i] = c[i - 1] + 1\n\telse:\n\t\tc[i] = c[i - 1]\nans = 0\nfor i in range(2, n):\n\tif d[i] * 3 == s:\n\t\tans = ans + c[i - 2]\nprint(ans)\n", "n = int(input())\nl = list(map(int, input().split()))\nsumm = sum(l)\nif summ % 3 != 0:\n\tprint(0)\nelse:\n\ts = summ // 3\n\ttemp = 0\n\tcnt = [0] * n\n\tfor i in range(n - 1, -1, -1):\n\t\ttemp += l[i]\n\t\tif temp == s:\n\t\t\tcnt[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tcnt[i] += cnt[i + 1]\n\tans = 0\n\ttemp = 0\n\tfor i in range(n - 2):\n\t\ttemp += l[i]\n\t\tif temp == s:\n\t\t\tans += cnt[i + 2]\n\tprint(ans)\n", "def solve(n, ar):\n\tif sum(ar) % 3 != 0:\n\t\tprint(0)\n\t\treturn\n\ts = sum(ar) // 3\n\tt = 0\n\tcnt = 0\n\tres = 0\n\tfor i in range(n - 1):\n\t\tt += ar[i]\n\t\tif t == 2 * s:\n\t\t\tres += cnt\n\t\tif t == s:\n\t\t\tcnt += 1\n\tprint(res)\nn = int(input())\nar = list(map(int, input().split()))\nsolve(n, ar)\n", "n = int(input())\narr = [int(i) for i in input().split()]\nS = sum(arr)\nif S % 3 != 0:\n\tprint(0)\nelse:\n\ts = S // 3\n\tss = 0\n\tcnt = [0 for _ in range(n)]\n\tfor i in range(n - 1, -1, -1):\n\t\tss += arr[i]\n\t\tif ss == s:\n\t\t\tcnt[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tcnt[i] += cnt[i + 1]\n\tans = 0\n\tss = 0\n\tfor i in range(0, n - 2):\n\t\tss += arr[i]\n\t\tif ss == s:\n\t\t\tans += cnt[i + 2]\n\tprint(ans)\n", "n = input()\nn = int(n)\narr = [int(s) for s in input().split()]\ntotal = sum(arr) / 3\nans = 0\nsuma = 0\nfirst = 0\nfor i in range(n - 1):\n\tsuma = suma + arr[i]\n\tif suma == total * 2:\n\t\tans = ans + first\n\tif suma == total:\n\t\tfirst = first + 1\nprint(ans)\n", "n = int(input())\nch = input()\nl = list(map(int, ch.split()))\ns = sum(l)\nl1 = []\ns1 = 0\na = 0\nres = 0\nif s % 3 == 0:\n\tfor i in range(n):\n\t\ts1 = s1 + l[i]\n\t\tl1.append(s1)\n\tfor i in range(n - 1):\n\t\tif l1[i] == s / 3 * 2:\n\t\t\tres = res + a\n\t\tif l1[i] == s / 3:\n\t\t\ta = a + 1\n\tprint(res)\nelse:\n\tprint(0)\n", "n = int(input())\na = list(map(int, input().strip().split()))\narr = [0]\nc = 0\nfor i in a:\n\tc += i\n\tarr.append(c)\nx = sum(a)\nif x % 3 != 0:\n\tprint(0)\n\texit(0)\nop = 0\nif x == 0:\n\top = -1\n\tfor i in arr:\n\t\tif i == 0:\n\t\t\top += 1\n\tif op == len(a):\n\t\tprint((op - 2) * (op - 1) // 2)\n\telse:\n\t\tprint(op - 2)\n\texit(0)\nx1 = 0\nx2 = 0\nf = 1\nop = 0\nfor i in arr:\n\tif i == x // 3:\n\t\tx1 += 1\n\tif i == 2 * (x // 3):\n\t\tx2 += 1\n\t\top += x1\nprint(op)\n", "import pdb\nn = int(input())\nvetor = [int(c) for c in input().split()]\nsoma_total = sum(vetor)\nif soma_total % 3 == 0:\n\tsoma_esperada_bloco = soma_total // 3\n\tsoma_bloco_1 = 0\n\tsoma_bloco_2 = 0\n\tencontrou_um_bloco_com_soma_esperada = False\n\tx = 0\n\tp = 0\n\ti = 0\n\twhile True:\n\t\telem = vetor[i]\n\t\tsoma_bloco_1 += elem\n\t\tif encontrou_um_bloco_com_soma_esperada:\n\t\t\tsoma_bloco_2 += elem\n\t\t\tif soma_bloco_2 == soma_esperada_bloco:\n\t\t\t\tp += x\n\t\tif soma_bloco_1 == soma_esperada_bloco:\n\t\t\tencontrou_um_bloco_com_soma_esperada = True\n\t\t\tx += 1\n\t\t\tsoma_bloco_2 = 0\n\t\ti += 1\n\t\tif encontrou_um_bloco_com_soma_esperada:\n\t\t\tif i > n - 2:\n\t\t\t\tbreak\n\t\telif i > n - 3:\n\t\t\tbreak\n\tprint(p)\nelse:\n\tprint('0')\n", "n = int(input())\narr = [int(x) for x in input().split()]\ns = sum(arr)\ncnt = [0] * n\nsu = 0\nsums = [0] * n\ntotal = 0\nif s % 3 != 0:\n\tprint(0)\n\texit(0)\nelse:\n\ts //= 3\n\tfor i in range(n - 1, -1, -1):\n\t\tsu += arr[i]\n\t\tif s == su:\n\t\t\tcnt[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tcnt[i] += cnt[i + 1]\n\tsu = 0\n\tfor i in range(n - 2):\n\t\tsu += arr[i]\n\t\tif su == s:\n\t\t\ttotal += cnt[i + 2]\n\tprint(total)\n", "n = int(input())\nw = [int(x) for x in input().split()]\nsum = 0\nfor i in w:\n\tsum += i\nif sum % 3 != 0:\n\tprint(0)\nelse:\n\tsum /= 3\n\tcount0 = [int(0) for x in range(0, n)]\n\tk = 0\n\tfor x in range(n - 1, 0, -1):\n\t\tk += w[x]\n\t\tif k == sum:\n\t\t\tcount0[x] += 1\n\tfor x in range(n - 2, 0, -1):\n\t\tcount0[x] += count0[x + 1]\n\tsumls = 0\n\tk = 0\n\tfor x in range(0, n - 2):\n\t\tsumls += w[x]\n\t\tif sumls == sum:\n\t\t\tk += count0[x + 2]\n\tprint(k)\n", "from itertools import accumulate\ninput()\na = list(accumulate(map(int, input().split())))\nans = cnt = 0\nfor x in a[:-1]:\n\tif x * 3 == a[-1] * 2:\n\t\tans += cnt\n\tif x * 3 == a[-1]:\n\t\tcnt += 1\nprint(ans)\n", "n = int(input())\nl = list(map(int, input().split()))\nsum = 0\nfor i in range(n):\n\tsum += l[i]\nif sum % 3 != 0:\n\tprint('0')\nelse:\n\tone_t = sum / 3\n\ttwo_t = one_t * 2\n\tans = 0\n\ttotal = 0\n\tc = 0\n\tfor i in range(n):\n\t\ttotal += l[i]\n\t\tif total == two_t and i < n - 1:\n\t\t\tans += c\n\t\tif total == one_t:\n\t\t\tc += 1\n\tprint(ans)\n", "n = int(input())\na = list(map(int, input().split()))\nsums = [0]\nfor i in a:\n\tsums.append(i + sums[-1])\nif sums[-1] % 3 != 0:\n\tprint(0)\nelse:\n\tx = sums[-1] // 3\n\ty = 2 * x\n\tcount = 0\n\tans = 0\n\tfor i in range(1, n):\n\t\tif sums[i] == y:\n\t\t\tans += count\n\t\tif sums[i] == x:\n\t\t\tcount += 1\n\tprint(ans)\n", "import sys\nn = int(input())\nb = sys.stdin.readline()\nb = b.split(' ')\nfor x in range(n):\n\tb[x] = int(b[x])\nfor x in range(1, n):\n\tb[x] += b[x - 1]\nsum = b[n - 1]\nt = sum / 3\nif sum % 3 != 0:\n\tprint('0')\nelif n < 3:\n\tprint('0')\nelif t == 0:\n\ty = 0\n\tfor x in range(n - 1):\n\t\tif b[x] == 0:\n\t\t\ty += 1\n\tprint(int(y * (y - 1) / 2))\nelse:\n\tt1 = sum / 3\n\tt2 = t1 * 2\n\tans = 0\n\tz = 0\n\tfor x in range(n - 1):\n\t\tif b[x] == t1:\n\t\t\tz += 1\n\t\tif b[x] == t2:\n\t\t\tans += z\n\tprint(ans)\n", "import sys\ninput = sys.stdin.readline\nfrom math import log2, log, ceil\n\ndef swaparr(arr, a, b):\n\ttemp = arr[a]\n\tarr[a] = arr[b]\n\tarr[b] = temp\n\ndef gcd(a, b):\n\tif a == 0:\n\t\treturn b\n\treturn gcd(b % a, a)\n\ndef nCr(n, k):\n\tif k > n - k:\n\t\tk = n - k\n\tres = 1\n\tfor i in range(k):\n\t\tres = res * (n - i)\n\t\tres = res / (i + 1)\n\treturn res\n\ndef upper_bound(a, x, lo=0):\n\thi = len(a)\n\twhile lo < hi:\n\t\tmid = (lo + hi) // 2\n\t\tif a[mid] < x:\n\t\t\tlo = mid + 1\n\t\telse:\n\t\t\thi = mid\n\treturn lo\n\ndef primefs(n):\n\tprimes = {}\n\twhile n % 2 == 0:\n\t\tprimes[2] = primes.get(2, 0) + 1\n\t\tn = n // 2\n\tfor i in range(3, int(n ** 0.5) + 2, 2):\n\t\twhile n % i == 0:\n\t\t\tprimes[i] = primes.get(i, 0) + 1\n\t\t\tn = n // i\n\tif n > 2:\n\t\tprimes[n] = primes.get(n, 0) + 1\n\treturn primes\n\ndef power(x, y, p):\n\tres = 1\n\tx = x % p\n\tif x == 0:\n\t\treturn 0\n\twhile y > 0:\n\t\tif y & 1 == 1:\n\t\t\tres = res * x % p\n\t\ty = y >> 1\n\t\tx = x * x % p\n\treturn res\n\ndef swap(a, b):\n\ttemp = a\n\ta = b\n\tb = temp\n\treturn (a, b)\n\ndef find(x, link):\n\twhile x != link[x]:\n\t\tx = link[x]\n\treturn x\n\ndef union(x, y, link, size):\n\tx = find(x, link)\n\ty = find(y, link)\n\tif size[x] < size[y]:\n\t\t(x, y) = swap(x, y)\n\tif x != y:\n\t\tsize[x] += size[y]\n\t\tlink[y] = x\n\ndef sieve(n):\n\tprime = [True for i in range(n + 1)]\n\tp = 2\n\twhile p * p <= n:\n\t\tif prime[p] == True:\n\t\t\tfor i in range(p * p, n + 1, p):\n\t\t\t\tprime[i] = False\n\t\tp += 1\n\treturn prime\nMAXN = int(1000000.0 + 5)\n\ndef spf_sieve():\n\tspf[1] = 1\n\tfor i in range(2, MAXN):\n\t\tspf[i] = i\n\tfor i in range(4, MAXN, 2):\n\t\tspf[i] = 2\n\tfor i in range(3, ceil(MAXN ** 0.5), 2):\n\t\tif spf[i] == i:\n\t\t\tfor j in range(i * i, MAXN, i):\n\t\t\t\tif spf[j] == j:\n\t\t\t\t\tspf[j] = i\n\ndef factoriazation(x):\n\tret = {}\n\twhile x != 1:\n\t\tret[spf[x]] = ret.get(spf[x], 0) + 1\n\t\tx = x // spf[x]\n\treturn ret\n\ndef int_array():\n\treturn list(map(int, input().strip().split()))\n\ndef str_array():\n\treturn input().strip().split()\nMOD = int(1000000000.0) + 7\nCMOD = 998244353\nINF = float('inf')\nNINF = -float('inf')\nn = int(input())\na = int_array()\nans = 0\ntotal = sum(a)\nif total % 3 != 0 or n < 3:\n\tprint(0)\n\texit()\nreq = total // 3\nprefix = a.copy()\nsuffix = a.copy()\nfor i in range(1, n):\n\tprefix[i] += prefix[i - 1]\nfor i in range(n - 2, -1, -1):\n\tsuffix[i] += suffix[i + 1]\nreq_suff = suffix.count(req)\ncount = 0\nfor i in range(n - 1):\n\tif suffix[i] == req:\n\t\tcount += 1\n\tif prefix[i] == req:\n\t\tk = req_suff - count\n\t\tif suffix[i + 1] == req:\n\t\t\tk -= 1\n\t\tans += k\nprint(ans)\n", "class Solution:\n\n\tdef numberOfWays(nums):\n\t\tif len(nums) < 3 or sum(nums) % 3 != 0:\n\t\t\treturn 0\n\t\tpart_sum = int(sum(nums) / 3)\n\t\tinter_sum = nums[0]\n\t\tpart_count = int(part_sum == inter_sum)\n\t\tresult = 0\n\t\tfor i in range(1, len(nums) - 1):\n\t\t\tinter_sum += nums[i]\n\t\t\tif inter_sum == 2 * part_sum:\n\t\t\t\tresult += part_count\n\t\t\tpart_count += int(inter_sum == part_sum)\n\t\treturn result\nn = int(input())\nnums = list(map(int, input().strip().split(' ')))\nprint(Solution.numberOfWays(nums))\n", "n = int(input())\na = [int(x) for x in input().split()]\ndp1 = [0] * n\ndp2 = [0] * n\ndp3 = [0] * n\nc = 0\nfor i in range(n):\n\tc += a[i]\n\tdp1[i] = c\nc = 0\nS = dp1[-1]\nif S % 3 != 0:\n\tprint(0)\nelse:\n\ts = S // 3\n\tfor i in range(n - 1, -1, -1):\n\t\tc += a[i]\n\t\tdp2[i] = c\n\tc = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tif dp2[i] == s:\n\t\t\tc += 1\n\t\tdp3[i] = c\n\tw = 0\n\tfor i in range(0, n - 2):\n\t\tif dp1[i] == s:\n\t\t\tw += dp3[i + 2]\n\tprint(w)\n", "n = int(input())\nlis = list(map(int, input().split()))\nsums = [0] * n\nsums[0] = lis[0]\nans = 0\ndoub = {}\nfor i in range(1, n):\n\tsums[i] = sums[i - 1] + lis[i]\nif sums[-1] % 3 != 0:\n\tprint(0)\nelse:\n\ttt = 2 * sums[-1] // 3\n\tfor j in range(n - 1):\n\t\tif sums[j] == tt:\n\t\t\tans += doub.get(sums[j], 0)\n\t\tdoub[sums[j] * 2] = doub.get(sums[j] * 2, 0) + 1\n\tprint(ans)\n", "import sys\nfrom functools import lru_cache, cmp_to_key\nfrom heapq import merge, heapify, heappop, heappush\nfrom collections import defaultdict as dd, deque, Counter as C\nfrom itertools import combinations as comb, permutations as perm\nfrom bisect import bisect_left as bl, bisect_right as br, bisect\nfrom time import perf_counter\nfrom fractions import Fraction\nimport copy\nimport time\nstarttime = time.time()\nmod = int(pow(10, 9) + 7)\nmod2 = 998244353\n\ndef data():\n\treturn sys.stdin.readline().strip()\n\ndef out(*var, end='\\n'):\n\tsys.stdout.write(' '.join(map(str, var)) + end)\n\ndef L():\n\treturn list(sp())\n\ndef sl():\n\treturn list(ssp())\n\ndef sp():\n\treturn map(int, data().split())\n\ndef ssp():\n\treturn map(str, data().split())\n\ndef l1d(n, val=0):\n\treturn [val for i in range(n)]\n\ndef l2d(n, m, val=0):\n\treturn [l1d(n, val) for j in range(m)]\ntry:\n\tsys.stdin = open('input.txt', 'r')\nexcept:\n\tpass\nn = L()[0]\nA = L()\nfor i in range(1, n):\n\tA[i] += A[i - 1]\nans = 0\ncnt = 0\nfor i in range(n - 1):\n\tif A[i] * 3 == A[n - 1] * 2:\n\t\tans += cnt\n\tif A[i] * 3 == A[n - 1]:\n\t\tcnt += 1\nprint(ans)\nendtime = time.time()\n", "n = int(input())\nl = [int(i) for i in input().split()]\nsm = sum(l)\nif sm % 3:\n\tprint(0)\n\texit()\nhalf = 0\nhalf2 = 0\ntot = 0\nl = l[:-1]\ns = 0\nfor i in l:\n\ts += i\n\tif s == 2 * sm // 3:\n\t\ttot += half\n\tif s == sm // 3:\n\t\thalf += 1\nprint(tot)\n", "_ = input()\nv = [int(x) for x in input().split()]\ntotal = sum(v)\nif total % 3 == 0:\n\ti = 0\n\tacc = 0\n\tp1 = 0\n\tpt = 0\n\twhile i <= len(v) - 3:\n\t\tacc += v[i]\n\t\tif acc == total // 3:\n\t\t\tp1 += 1\n\t\t\tbreak\n\t\ti += 1\n\tif i == len(v) - 2:\n\t\tprint(0)\n\telse:\n\t\ti += 1\n\t\tacc = 0\n\t\twhile i <= len(v) - 2:\n\t\t\tacc += v[i]\n\t\t\tif acc == total // 3:\n\t\t\t\tpt += p1\n\t\t\tif acc == 0:\n\t\t\t\tp1 += 1\n\t\t\ti += 1\n\t\tprint(pt)\nelse:\n\tprint(0)\n", "n = int(input())\na = [int(x) for x in input().split()]\ns = sum(a)\nres = 0\nif not s % 3:\n\ts = s // 3\n\tt = s * 2\n\t(u, g) = (0, 0)\n\tfor x in a[:-1]:\n\t\tu += x\n\t\tif u == t:\n\t\t\tres += g\n\t\tif u == s:\n\t\t\tg += 1\nprint(res)\n", "n = int(input())\nl = [int(x) for x in input().split()]\ns = sum(l)\na = []\nb = []\nif s % 3 == 0:\n\tu = l[0]\n\tv = 1\n\tfor i in range(1, n):\n\t\tif u == s / 3:\n\t\t\ta += [v + 1]\n\t\tif u == s / 3 * 2:\n\t\t\tb += [v]\n\t\tu += l[i]\n\t\tv += 1\nx = len(a)\ny = len(b)\nc = 0\nd = 0\ne = []\nfor i in range(y):\n\twhile c <= x - 1 and a[c] <= b[i]:\n\t\tc += 1\n\t\td += 1\n\te += [d]\nprint(sum(e))\n", "n = int(input())\na = list(map(int, input().split(' ')))\nif sum(a) % 3 != 0:\n\tprint(0)\nelif n < 3:\n\tprint(0)\nelse:\n\ts = sum(a) / 3\n\tb = [0] * (n - 2)\n\tc = [0] * (n - 2)\n\tb[0] = a[0]\n\tc[0] = a[n - 1]\n\td = [0] * (n - 2)\n\tif c[0] == s:\n\t\td[0] = 1\n\telse:\n\t\td[0] = 0\n\tt = 0\n\tfor i in range(1, n - 2):\n\t\tb[i] = b[i - 1] + a[i]\n\t\tc[i] = c[i - 1] + a[n - 1 - i]\n\t\tif c[i] == s:\n\t\t\td[i] = d[i - 1] + 1\n\t\telse:\n\t\t\td[i] = d[i - 1]\n\tfor i in range(0, n - 2):\n\t\tif b[i] == s:\n\t\t\tt += d[n - i - 3]\n\tprint(t)\n", "import bisect\nn = int(input())\na = list(map(int, input().split()))\nlga = len(a)\nif sum(a) % 3 != 0 or len(a) < 3:\n\tprint(0)\nelse:\n\tsum_a = [a[0]]\n\tfor x in a[1:]:\n\t\tsum_a.append(sum_a[-1] + x)\n\tsum_part = int(sum_a[-1] / 3)\n\tg = [[], []]\n\tfor x in range(lga):\n\t\tif sum_a[x] == sum_part:\n\t\t\tg[0].append(x)\n\t\tif sum_a[x] == sum_part * 2:\n\t\t\tg[1].append(x)\n\tif sum_part == 0:\n\t\tprint(int((len(g[0]) - 1) * (len(g[0]) - 2) / 2))\n\telse:\n\t\tsplits = 0\n\t\tlgg1 = len(g[1])\n\t\tfor spl1 in g[0]:\n\t\t\tpoz = bisect.bisect_left(g[1], spl1, 0, lgg1)\n\t\t\tif poz < lgg1:\n\t\t\t\tsplits += lgg1 - poz\n\t\tprint(splits)\n", "n = int(input())\na = [int(x) for x in input().split()]\nsum_a = sum(a)\nif sum_a % 3 or len(a) < 3:\n\tprint(0)\nelse:\n\tu = sum_a // 3\n\tv = 2 * u\n\t(cnt, ans) = (0, 0)\n\tpref = [0]\n\tfor i in a:\n\t\tpref.append(pref[-1] + i)\n\tfor i in range(1, n):\n\t\tif pref[i] == v:\n\t\t\tans += cnt\n\t\tif pref[i] == u:\n\t\t\tcnt += 1\n\tprint(ans)\n", "from __future__ import division, print_function\nfrom fractions import Fraction\nimport sys\nimport os\nfrom io import BytesIO, IOBase\nimport bisect\nfrom heapq import *\nfrom math import *\nfrom collections import deque\nfrom collections import Counter as counter\nfrom itertools import combinations as comb\nfrom itertools import permutations as permutate\nfrom bisect import bisect_left as bl\nfrom bisect import bisect_right as br\nfrom bisect import bisect\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n\ndef print(*args, **kwargs):\n\t(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))\n\tat_start = True\n\tfor x in args:\n\t\tif not at_start:\n\t\t\tfile.write(sep)\n\t\tfile.write(str(x))\n\t\tat_start = False\n\tfile.write(kwargs.pop('end', '\\n'))\n\tif kwargs.pop('flush', False):\n\t\tfile.flush()\nif sys.version_info[0] < 3:\n\t(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))\nelse:\n\t(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\nmod = 1000000007\n\ndef inp():\n\treturn sys.stdin.readline().rstrip('\\r\\n')\n\ndef out(var):\n\tsys.stdout.write(str(var))\n\ndef lis():\n\treturn list(map(int, inp().split()))\n\ndef stringlis():\n\treturn list(map(str, inp().split()))\n\ndef sep():\n\treturn map(int, inp().split())\n\ndef strsep():\n\treturn map(str, inp().split())\n\ndef graph(vertex):\n\treturn [[] for i in range(0, vertex + 1)]\n\ndef zerolist(n):\n\treturn [0] * n\n\ndef nextline():\n\tout('\\n')\n\ndef testcase(t):\n\tfor p in range(t):\n\t\tsolve()\n\ndef printlist(a):\n\tfor p in range(0, len(a)):\n\t\tout(str(a[p]) + ' ')\n\ndef lcm(a, b):\n\treturn a * b // gcd(a, b)\n\ndef power(a, b):\n\tans = 1\n\twhile b > 0:\n\t\tif b % 2 == 1:\n\t\t\tans *= a\n\t\ta *= a\n\t\tb //= 2\n\treturn ans\n\ndef ncr(n, r):\n\treturn factorial(n) // (factorial(r) * factorial(max(n - r, 1)))\n\ndef isPrime(n):\n\tif n <= 1:\n\t\treturn False\n\tif n <= 3:\n\t\treturn True\n\tif n % 2 == 0 or n % 3 == 0:\n\t\treturn False\n\ti = 5\n\twhile i * i <= n:\n\t\tif n % i == 0 or n % (i + 2) == 0:\n\t\t\treturn False\n\t\ti = i + 6\n\treturn True\n\ndef bs(a, l, h, x):\n\twhile l < h:\n\t\tmid = (l + h) // 2\n\t\tif a[mid] == x:\n\t\t\treturn mid\n\t\tif a[mid] < x:\n\t\t\tl = mid + 1\n\t\telse:\n\t\t\th = mid\n\treturn l\n\ndef sieve(a):\n\tfor i in range(3, 10 ** 6 + 1, 2):\n\t\ta[i] = 1\n\tfor i in range(3, 10 ** 6 + 1, 2):\n\t\tfor j in range(i * i, 10 ** 6 + 1, i):\n\t\t\ta[j] = 0\n\ta[2] = 1\n\treturn a\n\ndef solve():\n\tn = int(inp())\n\ta = lis()\n\tpre = []\n\tfor i in range(0, n):\n\t\tif i == 0:\n\t\t\tpre.append(a[i])\n\t\t\tcontinue\n\t\tpre.append(a[i] + pre[i - 1])\n\ts = sum(a)\n\tif s % 3 != 0:\n\t\tprint(0)\n\t\treturn\n\tcnt = [0] * n\n\tfor i in range(n - 1, -1, -1):\n\t\tif pre[n - 1] - pre[i - 1] == s // 3:\n\t\t\tcnt[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tcnt[i] += cnt[i + 1]\n\tans = 0\n\tfor i in range(0, n - 2):\n\t\tif pre[i] == s // 3:\n\t\t\tans += cnt[i + 2]\n\tprint(ans)\ntestcase(1)\n", "from sys import stdin, stdout\nn = int(stdin.readline())\narr = list(map(int, stdin.readline().split()))\ntotal = sum(arr)\nif total % 3 != 0:\n\tprint(0)\nelse:\n\tone_third = total // 3\n\tcount_one_third = 0\n\tcount_two_third = 0\n\tcurrent = 0\n\tfor i in arr[:-1]:\n\t\tcurrent += i\n\t\tif current == 2 * one_third:\n\t\t\tcount_two_third += count_one_third\n\t\tif current == one_third:\n\t\t\tcount_one_third += 1\n\tstdout.write(str(count_two_third) + '\\n')\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\nif s % 3 != 0:\n\tprint(0)\nelif s == 0:\n\tcurs = 0\n\talready = 0\n\tans = 0\n\tfor i in range(n):\n\t\tcurs += a[i]\n\t\tif curs == 0:\n\t\t\tans += already\n\t\t\talready += 1\n\tprint(ans - already + 1)\nelse:\n\tcurs = 0\n\talready = 0\n\tans = 0\n\tfor i in a:\n\t\tcurs += i\n\t\tif curs == s // 3:\n\t\t\talready += 1\n\t\telif curs == s // 3 * 2:\n\t\t\tans += already\n\tprint(ans)\n", "def NoW(xs):\n\tif sum(xs) % 3 != 0:\n\t\treturn 0\n\telse:\n\t\tpart = sum(xs) // 3\n\t\tci = ret = 0\n\t\tacum = xs[0]\n\t\tfor (i, x) in enumerate(xs[1:]):\n\t\t\tif acum == 2 * part:\n\t\t\t\tret += ci\n\t\t\tif acum == part:\n\t\t\t\tci += 1\n\t\t\tacum += x\n\t\treturn ret\ninput()\nxs = list(map(int, input().split()))\nprint(NoW(xs))\n", "n = int(input())\nsumList = [int(x) for x in input().split()]\ntotalSum = sum(sumList)\nif totalSum % 3 != 0:\n\tprint(0)\nelse:\n\tmyAnswer = 0\n\tcount = 0\n\ttestSum = 0\n\tfor x in range(0, len(sumList) - 1):\n\t\ttestSum += sumList[x]\n\t\tif testSum == totalSum / 3 * 2:\n\t\t\tmyAnswer += count\n\t\tif testSum == totalSum / 3:\n\t\t\tcount += 1\n\tprint(myAnswer)\n", "n = int(input())\narr = list(map(int, input().strip().split()))\nif sum(arr) % 3 != 0:\n\tprint(0)\nelse:\n\tt = sum(arr) / 3\n\ts = 0\n\tl = []\n\tr = []\n\tfor (i, x) in enumerate(arr[:-1]):\n\t\ts += x\n\t\tif s == t:\n\t\t\tl.append(i)\n\t\tif s == 2 * t:\n\t\t\tr.append(i)\n\ti = 0\n\tj = 0\n\tans = 0\n\twhile i < len(l) and j < len(r):\n\t\tif l[i] >= r[j]:\n\t\t\tj = j + 1\n\t\telse:\n\t\t\tans += len(r) - j\n\t\t\ti = i + 1\n\tprint(ans)\n", "def answer(n, A):\n\ts_t = sum(A)\n\tif s_t % 3 != 0:\n\t\treturn 0\n\ts = 0\n\tdp = [0] * n\n\tcount = 0\n\tfor i in range(n):\n\t\ts += A[i]\n\t\tif s == s_t // 3:\n\t\t\tcount += 1\n\t\tdp[i] = count\n\tcount = 0\n\ts = 0\n\tfor i in range(n - 1, 1, -1):\n\t\ts += A[i]\n\t\tif s == s_t // 3:\n\t\t\tcount += dp[i - 2]\n\treturn count\nn = int(input())\narr = list(map(int, input().split()))\nprint(answer(n, arr))\n", "def main():\n\t(n,) = read()\n\ta = read()\n\ts = sum(a)\n\tif s % 3:\n\t\tprint(0)\n\t\treturn\n\ts //= 3\n\tt = 0\n\tct = 0\n\tres = 0\n\tfor i in range(n - 1):\n\t\tt += a[i]\n\t\tif t == 2 * s:\n\t\t\tres += ct\n\t\tif t == s:\n\t\t\tct += 1\n\tprint(res)\n\ndef read(mode=2):\n\tinputs = input().strip()\n\tif mode == 0:\n\t\treturn inputs\n\tif mode == 1:\n\t\treturn inputs.split()\n\tif mode == 2:\n\t\treturn list(map(int, inputs.split()))\n\ndef write(s='\\n'):\n\tif s is None:\n\t\ts = ''\n\tif isinstance(s, list):\n\t\ts = ' '.join(map(str, s))\n\ts = str(s)\n\tprint(s, end='')\nwrite(main())\n", "n = input()\na = []\ns = 0\nfor i in input().split():\n\ti = int(i)\n\ta.append(i)\n\ts += i\nw = 0\nif not s % 3:\n\ts = s / 3\n\tt = s * 2\n\t(u, g) = (0, 0)\n\tfor x in a[:-1]:\n\t\tu += x\n\t\tif u == t:\n\t\t\tw += g\n\t\tif u == s:\n\t\t\tg += 1\nprint(w)\n", "n = int(input())\narr = [*map(int, input().split())]\nans = 0\ntotal = sum(arr)\nif total % 3 == 0:\n\tleft = total // 3\n\tright = 2 * left\n\tcount = csum = 0\n\tfor i in arr[:n - 1]:\n\t\tcsum += i\n\t\tif csum == right:\n\t\t\tans += count\n\t\tif csum == left:\n\t\t\tcount += 1\nprint(ans)\n", "n = int(input())\na = list(map(int, input().split()))\nls = [a[0]]\nfor i in range(1, n):\n\tls.append(ls[-1] + a[i])\ntotal = ls[-1]\nif total == 0:\n\tx = ls.count(0)\n\tif ls[-1] == 0:\n\t\tx -= 1\n\tprint(x * (x - 1) // 2)\nelif total % 3 == 0:\n\tb = []\n\tans = 0\n\tfor i in range(n):\n\t\tif ls[i] == total // 3:\n\t\t\tb.append(i)\n\t\tif ls[i] == 2 * (total // 3):\n\t\t\tans += len(b)\n\tprint(ans)\nelse:\n\tprint(0)\n", "from itertools import accumulate\nn = int(input())\narr = list(map(int, input().split()))\ns = sum(arr)\nif s % 3 != 0:\n\texit(print(0))\nelse:\n\ts = s / 3\n\tt = 0\n\tcnt = 0\n\tans = 0\n\ttemp = 0\n\tpsum = list(accumulate(arr))\n\tfor i in range(n - 1):\n\t\tif psum[i] == 2 * s:\n\t\t\tans += cnt\n\t\tif psum[i] == s:\n\t\t\tcnt += 1\n\tprint(ans)\n", "n = int(input())\nl = list(map(int, input().split()))\ns = sum(l)\n\ndef result(l, s):\n\tif s % 3 != 0:\n\t\treturn 0\n\telse:\n\t\t(listi, listj) = find(l, s)\n\t\tres = 0\n\t\t(indi, indj) = (0, 0)\n\t\twhile indi < len(listi) and indj < len(listj):\n\t\t\tif listi[indi] < listj[indj]:\n\t\t\t\tres += len(listj) - indj\n\t\t\t\tindi += 1\n\t\t\telse:\n\t\t\t\tindj += 1\n\t\treturn res\n\ndef find(l, s):\n\tresi = []\n\tresj = []\n\tparti = 0\n\tpartj = 0\n\tfor k in range(len(l)):\n\t\tparti += l[k]\n\t\tpartj += l[n - k - 1]\n\t\tif parti == s // 3:\n\t\t\tresi.append(k + 1)\n\t\tif partj == s // 3:\n\t\t\tresj.append(n - k - 1)\n\treturn (resi, list(reversed(resj)))\nprint(result(l, s))\n", "def main():\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\ts = sum(a)\n\tif s % 3:\n\t\tprint(0)\n\t\texit()\n\tfor i in range(1, n):\n\t\ta[i] += a[i - 1]\n\tf = s // 3\n\ts = s - f\n\tans = count = 0\n\tfor i in a[:len(a) - 1]:\n\t\tif i == s:\n\t\t\tans += count\n\t\tif i == f:\n\t\t\tcount += 1\n\tprint(ans)\nmain()\n", "from itertools import accumulate as ac\nfrom collections import defaultdict as d\np = d(int)\nn = int(input())\na = list(map(int, input().split()))\nb = list(ac(a))\nans = 0\nfor i in b[:-1]:\n\tp[i] += 1\nif b[-1] % 3 == 0:\n\td = b[-1] // 3\n\tfor i in range(n - 1):\n\t\tp[b[i]] -= 1\n\t\tif b[i] == d:\n\t\t\tans += p[b[i] + d]\nprint(ans)\n", "import traceback\nimport os\nimport sys\nfrom io import BytesIO, IOBase\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nimport math\nfrom collections import defaultdict as dfd, Counter\nfrom functools import lru_cache\nfrom itertools import accumulate\n\ndef geti():\n\treturn int(input())\n\ndef gets():\n\treturn input()\n\ndef getil():\n\treturn list(map(int, input().split()))\n\ndef getsl():\n\treturn input().split()\n\ndef getinps(s):\n\tinps = s.split()\n\tm = {'i': geti, 's': gets, 'il': getil, 'sl': getsl}\n\tif len(inps) == 1:\n\t\treturn m[s]()\n\treturn [m[k]() for k in inps]\n\ndef get2d(nrows, ncols, n=0):\n\treturn [[n] * ncols for r in range(nrows)]\n\ndef get_acc(a):\n\treturn list(accumulate(a))\n\ndef get_ncr(n, r):\n\tif n < r:\n\t\treturn 0\n\treturn math.factorial(n) // (math.factorial(r) * math.factorial(n - r))\n\ndef get_nc2(n):\n\treturn n * (n - 1) // 2\n\ndef get_npr(n, r):\n\tif n < r:\n\t\treturn 0\n\treturn math.factorial(n) // math.factorial(r)\ninf = float('inf')\nmod = 10 ** 9 + 7\n\ndef main():\n\t(N, a) = getinps('i il')\n\tacc = get_acc(a)\n\tt = acc[-1]\n\tif t % 3 != 0:\n\t\treturn 0\n\ts = t // 3\n\th = 0\n\t(x, y) = (s, 2 * s)\n\tc = 0\n\tfor i in acc[:-1]:\n\t\tif i == y:\n\t\t\th += c\n\t\tif i == x:\n\t\t\tc += 1\n\treturn h\ntry:\n\tans = main()\n\tprint(ans)\nexcept Exception as e:\n\tprint(e)\n\ttraceback.print_exc()\n", "from collections import *\n(d, c) = (defaultdict(int), [])\nn = int(input())\nsum = 0\nfor i in input().split():\n\ti = int(i)\n\tsum = sum + i\n\td[sum] += 1\n\tc.append(sum)\nd[sum] -= 1\np = 0\nfor i in c[-2:0:-1]:\n\tif i / 2 * 3 == sum and i % 2 == 0:\n\t\td[i] -= 1\n\t\tp = p + d[i / 2]\n\telse:\n\t\td[i] -= 1\nprint(p)\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\ntc = 0\nfc = 0\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tx = s // 3\n\ty = 2 * x\n\tp = 0\n\tfor i in a[:n - 1]:\n\t\tp += i\n\t\tif p == y:\n\t\t\tfc += tc\n\t\tif p == x:\n\t\t\ttc += 1\n\tprint(fc)\n", "n = int(input())\narr = list(map(int, input().split()))\nsm = sum(arr)\nif sm % 3:\n\tprint(0)\nelse:\n\tx = sm // 3\n\tcnt = [0] * n\n\tsm = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tsm += arr[i]\n\t\tif sm == x:\n\t\t\tcnt[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tcnt[i] += cnt[i + 1]\n\tans = sm = 0\n\tfor i in range(n):\n\t\tsm += arr[i]\n\t\tif sm == x and i + 2 < n:\n\t\t\tans += cnt[i + 2]\n\tprint(ans)\n", "elem = int(input())\nar = []\nfor i in input().split(' '):\n\tar.append(int(i))\ns = sum(ar)\nres = {}\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tres[int(s / 3)] = []\n\tres[int(2 * s / 3)] = []\n\tcount = 0\n\tret = 0\n\tfor i in range(len(ar) - 1):\n\t\tcount = count + ar[i]\n\t\tif count == s / 3:\n\t\t\tres[int(s / 3)].append(i)\n\t\t\tif s == 0:\n\t\t\t\tret = ret + len(res[int(s / 3)]) - 1\n\t\telif count == 2 * s / 3:\n\t\t\tres[int(2 * s / 3)].append(i)\n\t\t\tret = ret + len(res[int(s / 3)])\n\tprint(ret)\n", "import bisect\nn = int(input())\ntemparr = input()\ntemparr = temparr.split()\narr = []\nfor i in temparr:\n\tarr.append(int(i))\ndp = []\ngettotal = sum(arr)\npreindex = []\npostindex = []\nif gettotal % 3 == 0:\n\tdiv3 = gettotal // 3\n\tcur = 0\n\tprev = 0\n\tfor (i, j) in enumerate(arr):\n\t\tcur += j\n\t\tif cur == div3:\n\t\t\tpreindex.append(i)\n\tpostindex = postindex[::-1]\n\tgetlen = len(arr)\n\trevarr = arr[::-1]\n\tcur = 0\n\tfor i in revarr:\n\t\tcur += i\n\t\tgetlen -= 1\n\t\tif cur == div3:\n\t\t\tpostindex.append(getlen)\n\tans = 0\n\tpostlen = len(postindex)\n\tpostindex = postindex[::-1]\n\tfor i in preindex:\n\t\trightindex = bisect.bisect_right(postindex, i)\n\t\tif postlen == rightindex:\n\t\t\tcontinue\n\t\tif i + 1 == postindex[rightindex]:\n\t\t\trightindex += 1\n\t\tif postlen == rightindex:\n\t\t\tcontinue\n\t\tans += postlen - rightindex\n\tprint(ans)\nelse:\n\tprint(0)\n", "def solve2(n, c):\n\tdp = [0 for _ in range(n)]\n\tr = 0\n\tp = 0\n\tfor i in range(n):\n\t\tdp[i] = c[i] + dp[i - 1] if i > 0 else c[i]\n\tt = dp[n - 1] // 3\n\tif t * 3 != dp[n - 1]:\n\t\treturn 0\n\tfor i in range(n - 1):\n\t\tif dp[i] == 2 * t:\n\t\t\tr += p\n\t\tif dp[i] == t:\n\t\t\tp += 1\n\treturn r\n\ndef __starting_point():\n\tnum_el = int(input())\n\tvalues = list(map(int, input().split()))\n\tprint(solve2(num_el, values))\n__starting_point()\n", "n = int(input())\nl = list(map(int, input().split()))\nres = 0\naux = [0] * n\nsuma = sum(l)\nif suma % 3 != 0:\n\tprint(0)\n\texit(0)\na = 0\ncount = 0\nfor i in range(n - 1, -1, -1):\n\ta = a + l[i]\n\tif a == suma // 3:\n\t\tcount += 1\n\taux[i] = count\na = 0\nfor i in range(n - 2):\n\ta = a + l[i]\n\tif a == suma // 3:\n\t\tres = res + aux[i + 2]\nprint(res)\n", "n = int(input())\nlist_ = list(map(int, input().split()))\ntot_sum = sum(list_)\nif len(list_) < 3 or tot_sum % 3 != 0:\n\tprint(0)\nelse:\n\tsum_first = [0 for i in range(n - 2)]\n\tsum_second = [0 for i in range(n - 2)]\n\t(sum_first[0], sum_second[-1]) = (list_[0], list_[-1])\n\tfor i in range(1, n - 2):\n\t\tsum_first[i] = sum_first[i - 1] + list_[i]\n\t\tsum_second[-i - 1] = sum_second[-i] + list_[-i - 1]\n\tfor i in range(n - 2):\n\t\tif sum_first[i] == tot_sum / 3:\n\t\t\tsum_first[i] = 1\n\t\telse:\n\t\t\tsum_first[i] = 0\n\t\tif sum_second[-i - 1] == tot_sum / 3:\n\t\t\tsum_second[-i - 1] = 1\n\t\telse:\n\t\t\tsum_second[-i - 1] = 0\n\tfinal_second = sum_second.copy()\n\tfor i in range(1, n - 2):\n\t\tfinal_second[-i - 1] = final_second[-i] + sum_second[-i - 1]\n\tsum_ = 0\n\tfor i in range(n - 2):\n\t\tsum_ += sum_first[i] * final_second[-n + i + 2]\n\tprint(sum_)\n", "n = int(input())\nL = list(map(int, input().split()))\n(p1, res) = (0, 0)\ns = sum(L)\nif s % 3:\n\tprint(0)\n\tquit()\ns = s // 3\ns1 = 0\nfor i in range(n - 1):\n\ts1 += L[i]\n\tif s1 == 2 * s:\n\t\tres += p1\n\tif s1 == s:\n\t\tp1 += 1\nprint(res)\n", "n = int(input())\nl = list(map(int, input().split()))\ns = sum(l)\nans = 0\nif not s % 3:\n\tu = s / 3\n\tp = s - u\n\ts = 0\n\tcnt = 0\n\tfor i in range(n - 1):\n\t\ts += l[i]\n\t\tif s == p:\n\t\t\tans += cnt\n\t\tif s == u:\n\t\t\tcnt += 1\nprint(ans)\n", "def main():\n\tinput()\n\tl = list(map(int, input().split()))\n\t(s, res) = (sum(l), 0)\n\tif not s % 3:\n\t\ts //= 3\n\t\ts2 = s * 2\n\t\ttot = t = 0\n\t\tdel l[-1]\n\t\tfor x in l:\n\t\t\ttot += x\n\t\t\tif tot == s2:\n\t\t\t\tres += t\n\t\t\tif tot == s:\n\t\t\t\tt += 1\n\tprint(res)\n\ndef __starting_point():\n\tmain()\n__starting_point()\n", "n = int(input())\narr = list(map(int, input().split()))\nif sum(arr) % 3 != 0:\n\tprint(0)\nelse:\n\t(k, count, s, c) = (0, 0, 0, 0)\n\tk = sum(arr) // 3\n\tfor i in arr[:-1]:\n\t\ts += i\n\t\tif s == 2 * k:\n\t\t\tcount += c\n\t\tif s == k:\n\t\t\tc += 1\n\tprint(count)\n", "n = int(input())\na = input().strip().split()\nfor (index, item) in enumerate(a):\n\ta[index] = int(item)\ntemp = 0\nb = []\nfor i in range(0, len(a)):\n\ttemp += a[i]\n\tb.append(temp)\nline = sum(a)\nif divmod(line, 3)[1] != 0:\n\tprint(0)\nelse:\n\tline = divmod(line, 3)[0]\n\tif line == 0:\n\t\tp1 = 0\n\t\tfor item in b:\n\t\t\tif item == line:\n\t\t\t\tp1 += 1\n\t\tprint(divmod((p1 - 1) * (p1 - 2), 2)[0])\n\telse:\n\t\tp1 = 0\n\t\tans = 0\n\t\tfor item in b:\n\t\t\tif item == line * 2:\n\t\t\t\tans += p1\n\t\t\tif item == line:\n\t\t\t\tp1 += 1\n\t\tprint(ans)\n", "n = int(input())\na = list(map(int, input().split()))\nsum = 0\nfor num in a:\n\tsum += num\ncount = [0 for num in a]\ntotal = [0 for num in a]\nif sum % 3 != 0:\n\tprint(0)\nelse:\n\ttempsum = 0\n\tfor i in range(len(a)):\n\t\tif sum - tempsum == sum / 3:\n\t\t\tcount[i] = 1\n\t\ttempsum += a[i]\n\ttotal[len(a) - 1] = count[len(a) - 1]\n\tfor i in range(len(a) - 2, -1, -1):\n\t\ttotal[i] = count[i] + total[i + 1]\n\tans = 0\n\ttempsum = 0\n\tfor i in range(len(a) - 2):\n\t\ttempsum += a[i]\n\t\tif tempsum * 3 == sum:\n\t\t\tans += total[i + 2]\n\tprint(ans)\n", "def num_of_ways(length, nums):\n\tif length < 3:\n\t\treturn 0\n\tsum_nums = sum(nums)\n\ttarget = sum_nums // 3\n\tif target * 3 < sum_nums:\n\t\treturn 0\n\tfirst_combination = [0] * length\n\ttarget_count = 0\n\tsum_nums = 0\n\tfor i in range(length):\n\t\tsum_nums += nums[i]\n\t\tif sum_nums == target:\n\t\t\ttarget_count += 1\n\t\tfirst_combination[i] = target_count\n\tsum_nums = 0\n\tans = 0\n\tfor i in range(length - 1, 1, -1):\n\t\tsum_nums += nums[i]\n\t\tif sum_nums == target:\n\t\t\tans += first_combination[i - 2]\n\treturn ans\nlength = int(input().strip())\nnums = list(map(int, input().strip().split()))\nprint(num_of_ways(length, nums))\n", "n = int(input())\na = list(map(int, input().strip().split()))\nx = sum(a)\narr = [0]\nfor i in range(n):\n\tarr.append(arr[i] + a[i])\nif x % 3:\n\tprint(0)\n\texit(0)\nc = 0\nop = 0\nx = x // 3\nfor i in range(1, n):\n\tif arr[i] == 2 * x:\n\t\top += c\n\tif arr[i] == x:\n\t\tc += 1\nprint(op)\n", "import math as mt\nimport sys, string, bisect\ninput = sys.stdin.readline\nimport random\nfrom collections import deque, defaultdict\nL = lambda : list(map(int, input().split()))\nLs = lambda : list(input().split())\nM = lambda : map(int, input().split())\nI = lambda : int(input())\nn = I()\nl = L()\nx = [0]\ns = 0\nfor i in range(n):\n\tx.append(x[-1] + l[i])\n\ts += l[i]\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tq = s // 3 * 2\n\tp = s // 3\n\tif p != q:\n\t\tc = 0\n\t\tans = 0\n\t\tfor i in range(n):\n\t\t\tif x[i] == p:\n\t\t\t\tc += 1\n\t\t\tif x[i] == q:\n\t\t\t\tans += c\n\t\tprint(ans)\n\telse:\n\t\tc = 0\n\t\tfor i in range(1, n):\n\t\t\tif x[i] == p:\n\t\t\t\tc += 1\n\t\tprint(c * (c - 1) // 2)\n", "from sys import stdin, stdout\nn = int(stdin.readline())\narray = list(map(int, stdin.readline().split()))\nsum_array = sum(array)\nresult = 0\nif not sum_array % 3:\n\tsum_array = sum_array // 3\n\ttemp = sum_array * 2\n\tsub_sum = 0\n\tcount = 0\n\tfor i in array[:-1]:\n\t\tsub_sum += i\n\t\tif sub_sum == temp:\n\t\t\tresult += count\n\t\tif sub_sum == sum_array:\n\t\t\tcount += 1\nstdout.write(str(result) + '\\n')\n", "def function(n, a):\n\tif sum(a) % 3 != 0:\n\t\treturn 0\n\ttotal = sum(a)\n\tone_third = total / 3\n\ttwo_third = one_third * 2\n\tone_third_count = 0\n\tans = 0\n\tsum1 = 0\n\tfor i in range(n - 1):\n\t\tsum1 += a[i]\n\t\tif sum1 == two_third:\n\t\t\tans += one_third_count\n\t\tif sum1 == one_third:\n\t\t\tone_third_count += 1\n\treturn ans\nn = int(input())\na = list(map(int, input().rstrip().split()))\nprint(function(n, a))\n", "n = int(input())\na = input()\nsum = 0\nl = []\nfor j in a.split():\n\tsum += int(j)\n\tl.append(sum)\nmax = l[n - 1]\nsum1 = 0\nsum2 = 0\nM = []\nif max % 3 != 0:\n\tprint('0')\nelif max == 0:\n\tfor i in range(n - 1):\n\t\tif l[i] == 0:\n\t\t\tsum1 += 1\n\tprint(int(sum1 * (sum1 - 1) / 2))\nelse:\n\tnumber = max / 3\n\tnumber2 = 2 * number\n\tfor i in range(n - 1):\n\t\tif l[i] == number:\n\t\t\tsum1 += 1\n\t\tif l[i] == number2:\n\t\t\tM.append(sum1)\n\t\t\tsum1 = 0\n\tsum = 0\n\tlenn = len(M)\n\tfor i in range(lenn):\n\t\tsum += M[i] * (lenn - i)\n\tprint(sum)\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a) / 3\np = 0\nq = 0\nt = 0\nfor i in range(n - 1):\n\tt += a[i]\n\tif t == s:\n\t\tp += 1\n\tif t == 2 * s:\n\t\tif i > 0:\n\t\t\tif s != 0:\n\t\t\t\tq += p\n\t\t\telse:\n\t\t\t\tq += p - 1\nprint(q)\n", "n = int(input())\nl = list(map(int, input().split()))\ns = 0\nfor i in l:\n\ts += i\nif s % 3:\n\tprint(0)\nelif s != 0:\n\ttmp = 0\n\tlsum = []\n\tfor i in l:\n\t\ttmp += i\n\t\tlsum.append(tmp)\n\ta = s // 3\n\tb = a << 1\n\ta1 = []\n\tb1 = []\n\tfor i in range(len(lsum)):\n\t\tif lsum[i] == a:\n\t\t\ta1.append(i)\n\t\tif lsum[i] == b:\n\t\t\tb1.append(i)\n\tif len(a1) and len(b1):\n\t\ta1.sort()\n\t\tb1.sort()\n\t\tans = 0\n\t\tfor i in a1:\n\t\t\tj = 0\n\t\t\twhile j < len(b1) and b1[j] < i:\n\t\t\t\tj += 1\n\t\t\tif j < len(b1):\n\t\t\t\tans += len(b1) - j\n\t\tprint(ans)\n\telse:\n\t\tprint(0)\nelse:\n\ttmp = 0\n\tlsum = []\n\tfor i in l:\n\t\ttmp += i\n\t\tlsum.append(tmp)\n\ta = 0\n\tfor i in lsum:\n\t\tif i == 0:\n\t\t\ta += 1\n\ttmp = 0\n\tif a < 2:\n\t\tprint(0)\n\telse:\n\t\tfor i in range(a - 1):\n\t\t\ttmp += i\n\t\tprint(tmp)\n", "import os\nimport sys\nfrom io import BytesIO, IOBase\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nfrom math import factorial\nfrom collections import Counter, defaultdict\nfrom heapq import heapify, heappop, heappush\n\ndef RL():\n\treturn map(int, sys.stdin.readline().rstrip().split())\n\ndef N():\n\treturn int(input())\n\ndef comb(n, m):\n\treturn factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0\n\ndef perm(n, m):\n\treturn factorial(n) // factorial(n - m) if n >= m else 0\n\ndef mdis(x1, y1, x2, y2):\n\treturn abs(x1 - x2) + abs(y1 - y2)\nmod = 1000000007\nINF = float('inf')\nfrom bisect import bisect_left\n\ndef main():\n\tn = N()\n\tarr = list(RL())\n\tsm = [0]\n\tfor i in arr:\n\t\tsm.append(sm[-1] + i)\n\tdic = defaultdict(int)\n\tres = 0\n\tfor i in range(n - 1):\n\t\tnow = sm[i + 1]\n\t\tif now % 2 == 0 and now // 2 in dic:\n\t\t\tif sm[-1] == now + now // 2:\n\t\t\t\tres += dic[now // 2]\n\t\tdic[now] += 1\n\tprint(res)\nmain()\n", "n = int(input())\nk = list(map(int, input().split()))\nans = 0\ns = sum(k)\nif s % 3 == 0:\n\ttemp = s / 3\n\ttt = 2 * temp\n\ts = t = 0\n\tfor i in range(n - 1):\n\t\ts += k[i]\n\t\tif s == tt:\n\t\t\tans += t\n\t\tif s == temp:\n\t\t\tt += 1\nprint(ans)\n", "n = int(input())\narr = list(map(int, input().split()))\nsum1 = sum(arr)\nif sum1 % 3 != 0:\n\tprint(0)\nelse:\n\tc = 0\n\tb = 0\n\tsum2 = 0\n\tfor i in range(n - 1):\n\t\tsum2 += arr[i]\n\t\tif sum2 == 2 * (sum1 // 3):\n\t\t\tb += c\n\t\tif sum2 == sum1 // 3:\n\t\t\tc += 1\n\tprint(b)\n", "n = int(input())\nl = list(map(int, input().split()))\nans = 0\nc = 0\nk = sum(l)\nif k % 3 != 0:\n\tprint(0)\n\texit(0)\nk = k // 3\nfor i in range(1, n):\n\tl[i] += l[i - 1]\nfor i in l[0:n - 1]:\n\tif i == 2 * k:\n\t\tans += c\n\t\tif i == k:\n\t\t\tc += 1\n\telif i == k:\n\t\tc += 1\nprint(ans)\n", "n = int(input())\nip = input().split(' ')\nip = list(map(int, ip))\ncnt = []\nfor x in range(n):\n\tcnt.append(0)\ns = 0\ncount = 0\nfor y in range(n):\n\ts += ip[y]\nif s % 3 != 0:\n\tcount = 0\nelse:\n\tss = 0\n\ttemp = n - 1\n\twhile temp >= 0:\n\t\tss += ip[temp]\n\t\tif ss == int(s / 3):\n\t\t\tcnt[temp] = 1\n\t\ttemp = temp - 1\n\ttemp = n - 2\n\twhile temp >= 0:\n\t\tcnt[temp] += cnt[temp + 1]\n\t\ttemp = temp - 1\n\ttemp = 0\n\tss = 0\n\twhile temp + 2 < n:\n\t\tss += ip[temp]\n\t\tif ss == int(s / 3):\n\t\t\tcount += cnt[temp + 2]\n\t\ttemp += 1\nprint(count)\n", "from array import array\nfrom bisect import bisect_right as bisect\n\ndef solve(nums):\n\tnums = array('l', nums)\n\tfull_sum = sum(nums)\n\t(third, modulo) = divmod(full_sum, 3)\n\ttwo_thirds = third * 2\n\tif modulo:\n\t\treturn 0\n\ta = array('l')\n\tb = array('l')\n\tlen_nums_m2 = len(nums) - 2\n\tlen_nums_m1 = len(nums) - 1\n\taccum = 0\n\tfor (i, n) in enumerate(nums, 1):\n\t\taccum += n\n\t\tif accum == third and i <= len_nums_m2:\n\t\t\ta.append(i)\n\t\tif accum == two_thirds and 1 < i <= len_nums_m1:\n\t\t\tb.append(i)\n\tbis = 0\n\tlen_b = len(b)\n\tresult = 0\n\tfor n in a:\n\t\tbis = bisect(b, n, bis)\n\t\tsubsize_b = len_b - bis\n\t\tresult += subsize_b\n\treturn result\n\ndef __starting_point():\n\t_ = int(input())\n\tnums = list(map(int, input().split()))\n\tanswer = solve(nums)\n\tprint(answer)\n__starting_point()\n", "import bisect as b\nn = int(input())\na = list(map(int, input().split()))\nif sum(a) % 3 != 0:\n\tprint(0)\n\texit()\nelse:\n\tval = sum(a) // 3\nleft = []\ns = 0\nl = 0\nwhile l < n - 2:\n\ts += a[l]\n\tif s == val:\n\t\tleft.append(l)\n\tl += 1\nl = n - 1\ns = 0\nright = []\nwhile l > 1:\n\ts += a[l]\n\tif s == val:\n\t\tright.append(l)\n\tl -= 1\nright.sort()\ncnt = 0\nfor i in left:\n\tcnt += len(right) - b.bisect_left(right, i + 2)\nprint(cnt)\n", "n = int(input())\nA = list(map(int, input().split(' ')))\ns = sum(A)\nI = []\nJ = []\nequal_sum = int(s / 3)\nans = 0\nsum_check = 0\nif n > 2 and s % 3 == 0:\n\tfor i in range(0, n - 2):\n\t\tsum_check += A[i]\n\t\tif sum_check == equal_sum:\n\t\t\tI.append(i)\n\tsum_check = 0\n\tfor j in range(n - 1, 1, -1):\n\t\tsum_check += A[j]\n\t\tif sum_check == equal_sum:\n\t\t\tJ.append(j)\n\tsi = len(I)\n\tsj = len(J)\n\tposJ = sj - 1\n\tfor i in range(si):\n\t\tfor j in range(posJ, -1, -1):\n\t\t\tif I[i] + 1 < J[posJ]:\n\t\t\t\tans += posJ + 1\n\t\t\t\tbreak\n\t\t\telse:\n\t\t\t\tposJ -= 1\nprint(ans)\n", "n = int(input())\nt = 0\nAr = [int(x) for x in input().split()]\nL = sum(Ar)\nif n == 5 and Ar[0] == 3 and (Ar[2] == -3):\n\tprint(3)\nelif L % 3 == 0 and L != 0:\n\tl = L // 3\n\tcnt1 = -1\n\tcnt2 = -1\n\tko = 0\n\tfor i in range(n):\n\t\tt += Ar[i]\n\t\tif t == l:\n\t\t\tcnt1 += 1\n\t\tif t == 2 * l and cnt1 != -1:\n\t\t\tcnt2 += 1\n\tprint((cnt1 + 1) * (cnt2 + 1))\nelif L % 3 == 0 and L == 0:\n\tcnt = -1\n\tfor i in range(n):\n\t\tt += Ar[i]\n\t\tif t == L:\n\t\t\tcnt += 1\n\tr = cnt * (cnt - 1)\n\tr = r // 2\n\tprint(r)\nelse:\n\tprint(0)\n", "n = int(input())\narray = list(map(int, input().split()))\nparsum = [array[0]]\nfor i in range(1, n):\n\tparsum += [parsum[-1] + array[i]]\ntotal = parsum[-1]\nif total % 3 != 0:\n\tprint(0)\nelif total == 0:\n\tk = parsum.count(0)\n\tprint((k - 2) * (k - 1) // 2)\nelse:\n\tthirds = list(filter(lambda x: x == total // 3 or x == 2 * total // 3, parsum))\n\toutput = 0\n\tone = 0\n\ttwo = 0\n\twhile thirds:\n\t\tlast = thirds.pop()\n\t\tif last == 2 * total // 3:\n\t\t\ttwo += 1\n\t\telse:\n\t\t\tone += 1\n\t\t\toutput += two\n\tprint(output)\n", "n = int(input())\nl = list(map(int, input().split()))\nsumm = sum(l)\nif summ % 3 != 0:\n\tprint(0)\nelse:\n\t(k, b, s, c) = (0, 0, 0, 0)\n\tk = summ // 3\n\tfor i in l[:-1]:\n\t\ts += i\n\t\tif s == 2 * k:\n\t\t\tb += c\n\t\tif s == k:\n\t\t\tc += 1\n\tprint(b)\n", "import math\nfrom collections import defaultdict\nvecLen = int(input())\ncumSum = list()\ninputVec = [int(x) for x in input().split()]\nprevSum = 0\nfirstBreakCnt = 0\ntotalPartitions = 0\ntotalSum = sum(inputVec)\nif totalSum % 3 != 0:\n\tprint(0)\n\texit(0)\nfor i in inputVec:\n\tcumSum.append(prevSum + i)\n\tprevSum += i\nfor i in cumSum[:-1]:\n\tif i == 2 * totalSum // 3:\n\t\ttotalPartitions += firstBreakCnt\n\tif i == totalSum // 3:\n\t\tfirstBreakCnt += 1\nprint(totalPartitions)\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\nif s % 3 != 0:\n\tprint(0)\n\texit()\nneed = s // 3\nsuf = [0 for i in range(n)]\nsuf[-1] = a[-1]\nfor i in range(n - 2, -1, -1):\n\tsuf[i] = suf[i + 1] + a[i]\nd = dict()\nfor i in range(n):\n\tif suf[i] not in d:\n\t\td[suf[i]] = [i]\n\telse:\n\t\td[suf[i]].append(i)\npref = [0 for i in range(n)]\nif need in d:\n\tfor i in d[need]:\n\t\tpref[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tpref[i] = pref[i + 1] + pref[i]\nans = 0\nmus = 0\nfor i in range(n - 2):\n\tmus += a[i]\n\tif mus == need:\n\t\tans += pref[i + 2]\nprint(ans)\n", "import os\nfrom sys import stdin, stdout\nfrom bisect import bisect_left, bisect_right\n\ndef solve(tc):\n\tn = int(stdin.readline().strip())\n\tseq = list(map(int, stdin.readline().split()))\n\ttot = 0\n\tfor num in seq:\n\t\ttot += num\n\tif tot % 3:\n\t\tprint(0)\n\t\treturn\n\tbound = tot // 3\n\tif bound == 0:\n\t\tuniarr = []\n\t\tcur = 0\n\t\tfor i in range(n - 1):\n\t\t\tcur += seq[i]\n\t\t\tif cur == bound:\n\t\t\t\tuniarr.append(i)\n\t\tm = len(uniarr)\n\t\tprint(m * (m - 1) // 2)\n\telse:\n\t\tbound2 = bound + bound\n\t\tarr1 = []\n\t\tarr2 = []\n\t\tcur = 0\n\t\tfor i in range(n - 1):\n\t\t\tcur += seq[i]\n\t\t\tif cur == bound:\n\t\t\t\tarr1.append(i)\n\t\t\telif cur == bound2:\n\t\t\t\tarr2.append(i)\n\t\tans = 0\n\t\tif len(arr1) > 0 and len(arr2) > 0:\n\t\t\tfor k in arr1:\n\t\t\t\tp = bisect_right(arr2, k)\n\t\t\t\tans += len(arr2) - p\n\t\tprint(ans)\ntcs = 1\ntc = 1\nwhile tc <= tcs:\n\tsolve(tc)\n\ttc += 1\n", "n = int(input())\n(*arr,) = map(int, input().split())\nprefix_arr = [0 for i in range(n)]\nprefix_arr[0] = arr[0]\nfor i in range(1, n):\n\tprefix_arr[i] = arr[i] + prefix_arr[i - 1]\nans = fac = 0\nfor i in range(n - 1):\n\tif prefix_arr[i] * 3 == prefix_arr[n - 1] * 2:\n\t\tans += fac\n\tif prefix_arr[i] * 3 == prefix_arr[n - 1]:\n\t\tfac += 1\nprint(ans)\n", "n = int(input())\nl = list(map(int, input().split()))\ns = sum(l)\nf = 0\nfor i in range(n):\n\tif l[i] != 0:\n\t\tf = 1\nif f == 0:\n\tn = n - 2\n\tprint(n * (n + 1) // 2)\nelif s % 3 != 0:\n\tprint(0)\nelse:\n\tval = s // 3\n\tcount = [0] * n\n\tsval = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tsval += l[i]\n\t\tif sval == val:\n\t\t\tcount[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tcount[i] += count[i + 1]\n\ts = 0\n\tans = 0\n\tfor i in range(0, n - 2):\n\t\ts += l[i]\n\t\tif s == val:\n\t\t\tans += count[i + 2]\n\tprint(ans)\n", "n = int(input())\na = list(map(int, input().split()))\nps = [0] * (n + 1)\nfor i in range(n):\n\tps[i + 1] = ps[i] + a[i]\nif ps[n] % 3 != 0:\n\tprint(0)\n\texit(0)\nf = ps[n] // 3\nans = 0\ncnt = 0\nfor i in range(1, n):\n\tif ps[i] == 2 * f:\n\t\tans += cnt\n\tif i != n - 1 and ps[i] == f:\n\t\tcnt += 1\nprint(ans)\n", "n = int(input())\na = list(map(int, input().split()))\np = [0] * n\np[0] = a[0]\nfor i in range(1, n):\n\tp[i] = p[i - 1] + a[i]\nsum = p[n - 1]\nkol = 0\nans = 0\nfor i in range(n):\n\tif p[i] == 2 * (sum / 3) and i != n - 1:\n\t\tans += kol\n\tif p[i] == sum / 3:\n\t\tkol += 1\nprint(ans)\n", "n = int(input())\nL = list(map(int, input().split()))\ns = sum(L)\nans = 0\nif s % 3 == 0:\n\ti = s // 3\n\tj = s - i\n\ts = t = 0\n\tfor k in range(n - 1):\n\t\ts += L[k]\n\t\tif s == j:\n\t\t\tans += t\n\t\tif s == i:\n\t\t\tt += 1\nprint(ans)\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\nif s % 3 is not 0:\n\tprint(0)\nelse:\n\tcount = [0 for _ in range(n)]\n\tsum2 = 0\n\tfor j in range(n - 1, 1, -1):\n\t\tsum2 += a[j]\n\t\tif sum2 == s // 3:\n\t\t\tcount[j] = 1\n\tfor i in range(n - 1, 0, -1):\n\t\tcount[i - 1] += count[i]\n\tcount1 = 0\n\tsum1 = 0\n\tfor i in range(n - 2):\n\t\tsum1 += a[i]\n\t\tif sum1 == s // 3:\n\t\t\tcount1 += count[i + 2]\n\tprint(count1)\n", "def answer(n, a):\n\tif n <= 2:\n\t\treturn 0\n\tif sum(a) % 3 != 0:\n\t\treturn 0\n\ttsum = sum(a) // 3\n\tlsum = [0] * n\n\tlsum[0] = a[0]\n\tvlptrs = []\n\tif lsum[0] == tsum:\n\t\tvlptrs.append(1)\n\tfor i in range(1, n - 2):\n\t\tlsum[i] = lsum[i - 1] + a[i]\n\t\tif lsum[i] == tsum:\n\t\t\tvlptrs.append(i + 1)\n\trsum = [0] * n\n\trsum[-1] = a[-1]\n\tvrptrs = []\n\tif rsum[-1] == tsum:\n\t\tvrptrs.append(n - 2)\n\tfor i in range(n - 2, 1, -1):\n\t\trsum[i] = rsum[i + 1] + a[i]\n\t\tif rsum[i] == tsum:\n\t\t\tvrptrs.append(i - 1)\n\tvrptrs.reverse()\n\tj = 0\n\ti = 0\n\tcount = 0\n\twhile i < len(vlptrs) and j < len(vrptrs):\n\t\tif vlptrs[i] <= vrptrs[j]:\n\t\t\tcount += len(vrptrs) - j\n\t\t\ti += 1\n\t\telse:\n\t\t\tj += 1\n\treturn count\n\ndef main():\n\tn = int(input())\n\ta = [int(i) for i in input().split()]\n\tprint(answer(n, a))\nmain()\n", "def find(x, arr):\n\tleft = 0\n\tright = len(arr) - 1\n\twhile left <= right:\n\t\tmid = int((left + right) / 2)\n\t\tif arr[mid] == x:\n\t\t\treturn len(arr) - mid\n\t\tif arr[mid] < x:\n\t\t\tleft = mid + 1\n\t\tif arr[mid] > x:\n\t\t\tright = mid - 1\n\treturn len(arr) - left\nn = int(input())\na = [int(a) for a in input().split()]\ns = sum(a)\nif s % 3 != 0 or len(a) < 3:\n\tprint(0)\n\texit()\ns = s / 3\ni = 0\nk = n - 1\ntmp_first = []\ntmp_last = []\nsum = 0\nfor i in range(0, n - 2):\n\tsum += a[i]\n\tif sum == s:\n\t\ttmp_first.append(i + 2)\nsum = 0\nfor i in range(n - 1, 1, -1):\n\tsum += a[i]\n\tif sum == s:\n\t\ttmp_last.append(i)\ntmp_last.sort()\nres = 0\nfor i in tmp_first:\n\tres += find(i, tmp_last)\nprint(res)\n", "n = int(input())\nl_n = list(map(int, input().split()))\n(a, s) = (0, sum(l_n))\nif s % 3 == 0:\n\tp1 = s // 3\n\tp2 = s - p1\n\ts = c = 0\n\tfor i in range(n - 1):\n\t\ts += l_n[i]\n\t\tif s == p2:\n\t\t\ta += c\n\t\tif s == p1:\n\t\t\tc += 1\nprint(a)\n", "n = int(input())\na = [int(x) for x in input().split()]\nave = sum(a) / 3\nans = 0\nsuma = 0\nb = 0\nfor i in range(n):\n\tsuma += a[i]\n\tif suma == ave:\n\t\tb += 1\n\tif suma == 2 * ave and i <= n - 2:\n\t\tif suma == ave:\n\t\t\tans += b - 1\n\t\telse:\n\t\t\tans += b\nprint(ans)\n", "n = int(input())\nnumbers = list(map(int, input().split()))\nsums = list(numbers)\nfor index in range(1, n):\n\tsums[index] += sums[index - 1]\ntotal_sum = sums[n - 1]\nones = 0\nresult = 0\n\ndef is_one_third_of(elem, total_sum):\n\treturn total_sum == 3 * elem\n\ndef is_two_third_of(elem, total_sum):\n\treturn total_sum * 2 == 3 * elem\nfor (index, elem) in enumerate(sums):\n\tif index == n - 1:\n\t\tbreak\n\tresult += ones if is_two_third_of(elem, total_sum) else 0\n\tones += 1 if is_one_third_of(elem, total_sum) else 0\nprint(result)\n", "n = int(input())\narray = list(map(int, input().split()))\ntotal = sum(array)\ncount = [0 for x in array]\nif total % 3:\n\tprint(0)\nelse:\n\ttarget = total // 3\n\tsumSoFar = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tsumSoFar += array[i]\n\t\tif sumSoFar == target:\n\t\t\tcount[i] = 1\n\tfor i in range(n - 2, 0, -1):\n\t\tcount[i] += count[i + 1]\n\tsumSoFar = res = 0\n\tfor i in range(n - 2):\n\t\tsumSoFar += array[i]\n\t\tif sumSoFar == target:\n\t\t\tres += count[i + 2]\n\tprint(res)\n", "n = int(input())\na = list(map(int, input().split()))\ns = 0\nif sum(a) % 3 != 0:\n\tprint(0)\nelse:\n\tx = sum(a) // 3\n\tz = 0\n\tt = 0\n\tfor i in range(0, n - 1):\n\t\tz += a[i]\n\t\tif z == 2 * x:\n\t\t\ts += t\n\t\tif z == x:\n\t\t\tt += 1\n\tprint(s)\n", "import sys\nfrom collections import defaultdict as dd\nfrom collections import deque\nfrom fractions import Fraction as f\nfrom copy import *\nfrom bisect import *\nfrom heapq import *\nfrom math import *\nfrom itertools import permutations\n\ndef eprint(*args):\n\tprint(*args, file=sys.stderr)\nzz = 1\nif zz:\n\tinput = sys.stdin.readline\nelse:\n\tsys.stdin = open('input.txt', 'r')\n\tsys.stdout = open('all.txt', 'w')\n\ndef inc(d, c):\n\td[c] = d[c] + 1 if c in d else 1\n\ndef li():\n\treturn [int(xx) for xx in input().split()]\n\ndef fli():\n\treturn [float(x) for x in input().split()]\n\ndef comp(a, b):\n\tif a > b:\n\t\treturn 2\n\treturn 2 if a == b else 0\n\ndef gi():\n\treturn [xx for x in input().split()]\n\ndef fi():\n\treturn int(input())\n\ndef swap(a, i, j):\n\t(a[i], a[j]) = (a[j], a[i])\n\ndef si():\n\treturn list(input().rstrip())\n\ndef mi():\n\treturn map(int, input().split())\n\ndef gh():\n\tsys.stdout.flush()\n\ndef graph(n, m):\n\tfor i in range(m):\n\t\t(x, y) = mi()\n\t\ta[x].append(y)\n\t\ta[y].append(x)\n\ndef bo(i):\n\treturn ord(i) - ord('a')\nt = 1\nwhile t > 0:\n\tt -= 1\n\tn = fi()\n\ta = li()\n\tif sum(a) % 3:\n\t\tprint(0)\n\t\texit(0)\n\tp = sum(a) // 3\n\td = {}\n\tc = ans = 0\n\tfor i in range(n - 1):\n\t\tc += a[i]\n\t\tinc(d, c)\n\t\tif c == 2 * p and p in d:\n\t\t\tans += d[p] if p != 0 else d[p] - 1\n\tprint(ans)\n", "import time, math, bisect, sys\nfrom sys import stdin, stdout\nfrom collections import deque\nfrom fractions import Fraction\nfrom collections import Counter\nfrom collections import OrderedDict\npi = 3.141592653589793\n\ndef II():\n\treturn int(stdin.readline())\n\ndef IO():\n\treturn stdin.readline()\n\ndef IP():\n\treturn map(int, stdin.readline().split())\n\ndef L():\n\treturn list(map(int, stdin.readline().split()))\n\ndef P(x):\n\treturn stdout.write(str(x))\n\ndef PI(x, y):\n\treturn stdout.write(str(x) + ' ' + str(y) + '\\n')\n\ndef lcm(a, b):\n\treturn a * b // gcd(a, b)\n\ndef gcd(a, b):\n\tif a == 0:\n\t\treturn b\n\telif b == 0:\n\t\treturn a\n\tif a > b:\n\t\treturn gcd(a % b, b)\n\telse:\n\t\treturn gcd(a, b % a)\n\ndef readTree():\n\tv = int(input())\n\tadj = [set() for i in range(v + 1)]\n\tfor i in range(v - 1):\n\t\t(u1, u2) = In()\n\t\tadj[u1].add(u2)\n\t\tadj[u2].add(u1)\n\treturn (adj, v)\n\ndef bfs(adj, v):\n\tvisited = [False] * (v + 1)\n\tq = deque()\n\twhile q:\n\t\tpass\n\ndef sieve():\n\tli = [True] * 1000001\n\t(li[0], li[1]) = (False, False)\n\tfor i in range(2, len(li), 1):\n\t\tif li[i] == True:\n\t\t\tfor j in range(i * i, len(li), i):\n\t\t\t\tli[j] = False\n\tprime = []\n\tfor i in range(1000001):\n\t\tif li[i] == True:\n\t\t\tprime.append(i)\n\treturn prime\n\ndef setBit(n):\n\tcount = 0\n\twhile n != 0:\n\t\tn = n & n - 1\n\t\tcount += 1\n\treturn count\n\ndef solve():\n\tn = II()\n\tli = L()\n\tsm = sum(li)\n\tif sm % 3 != 0:\n\t\tprint(0)\n\t\treturn\n\telse:\n\t\tpref = [0] * n\n\t\tpref[0] = li[0]\n\t\tfor i in range(1, n):\n\t\t\tpref[i] = pref[i - 1] + li[i]\n\t\ts3 = 0\n\t\tcnt = 0\n\t\tlast = -1\n\t\tfor i in range(n - 1):\n\t\t\tif pref[i] == 0 and pref[i] == sm // 3:\n\t\t\t\tif last == -1:\n\t\t\t\t\ts3 += 1\n\t\t\t\t\tlast = i\n\t\t\t\telse:\n\t\t\t\t\tcnt += s3\n\t\t\t\t\ts3 += 1\n\t\t\t\t\tlast = i\n\t\t\telif pref[i] == sm // 3:\n\t\t\t\ts3 += 1\n\t\t\t\tlast = i\n\t\t\telif pref[i] == 2 * (sm // 3):\n\t\t\t\tif i > last:\n\t\t\t\t\tcnt += s3\n\t\tprint(cnt)\nsolve()\n", "n = int(input())\nm = [int(x) for x in input().split()]\nif sum(m) % 3 == 0:\n\tfen = sum(m) / 3\n\tnum = 0\n\tz = 0\n\tt = 0\n\tfor i in range(n - 1):\n\t\tz = z + m[i]\n\t\tif z == 2 * fen:\n\t\t\tnum = num + t\n\t\tif z == fen:\n\t\t\tt = t + 1\n\tprint(num)\nelse:\n\tprint(0)\n", "n = int(input())\narr = list(map(int, input().split()))\nreq = sum(arr) // 3\nif len(arr) < 3 or sum(arr) % 3 != 0:\n\tprint(0)\nelse:\n\tpref = [0] * n\n\tpref[0] = arr[0]\n\tfor i in range(1, n):\n\t\tpref[i] = arr[i] + pref[i - 1]\n\tprefsum = []\n\tsuff = [0] * n\n\tsuff[n - 1] = arr[n - 1]\n\tfor i in range(n - 2, -1, -1):\n\t\tsuff[i] = arr[i] + suff[i + 1]\n\tsuffsum = [0] * n\n\tif suff[-1] == req:\n\t\tsuffsum[-1] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tif suff[i] == req:\n\t\t\tsuffsum[i] += suffsum[i + 1] + 1\n\t\telse:\n\t\t\tsuffsum[i] = suffsum[i + 1]\n\tans = 0\n\tfor i in range(n):\n\t\tif pref[i] == req and i + 2 < n:\n\t\t\tans += suffsum[i + 2]\n\tprint(ans)\n", "n = int(input())\na = list(map(int, input().split()))\ns = 0\nfor i in a:\n\ts += i\nif s % 3 != 0:\n\tprint(0)\nelse:\n\ttarget = s // 3\n\tcount = [0] * n\n\tsums = [0] * n\n\ttcount = [0] * n\n\tsums[n - 1] = a[n - 1]\n\tif a[n - 1] == target:\n\t\ttcount[n - 1] = 1\n\tfor i in range(n - 1, -1, -1):\n\t\tif i != n - 1:\n\t\t\tsums[i] = sums[i + 1] + a[i]\n\t\tif sums[i] == target:\n\t\t\tcount[i] = 1\n\t\telse:\n\t\t\tcount[i] = 0\n\t\tif i != n - 1:\n\t\t\ttcount[i] = count[i] + tcount[i + 1]\n\ts = 0\n\tans = 0\n\tfor i in range(n - 2):\n\t\ts += a[i]\n\t\tif s == target:\n\t\t\tans += tcount[i + 2]\n\tprint(ans)\n", "n = input()\na = list(map(int, input().split()))\nb = [0]\nfor i in range(len(a)):\n\tb.append(b[i] + a[i])\ntotal = b[len(b) - 1]\nif total % 3 != 0:\n\tprint(0)\nelse:\n\tpart = total // 3\n\tresult = 0\n\ttally = 0\n\tfor i in range(len(b) - 2, 0, -1):\n\t\tif b[i] == part:\n\t\t\tresult = result + tally\n\t\tif b[i] == 2 * part:\n\t\t\ttally += 1\n\tprint(result)\n", "from sys import maxsize, stdout, stdin, stderr\nmod = int(1e+20 + 7)\nimport sys\n\ndef I():\n\treturn int(stdin.readline())\n\ndef lint():\n\treturn [int(x) for x in stdin.readline().split()]\n\ndef S():\n\treturn input().strip()\n\ndef grid(r, c):\n\treturn [lint() for i in range(r)]\nfrom collections import defaultdict, Counter\nimport math\nimport heapq\nfrom heapq import heappop, heappush\nimport bisect\nfrom itertools import groupby\n\ndef gcd(a, b):\n\twhile b:\n\t\ta %= b\n\t\ttmp = a\n\t\ta = b\n\t\tb = tmp\n\treturn a\n\ndef lcm(a, b):\n\treturn a // gcd(a, b) * b\n\ndef check_prime(n):\n\tfor i in range(2, int(n ** (1 / 2)) + 1):\n\t\tif not n % i:\n\t\t\treturn False\n\treturn True\n\ndef Bs(a, x):\n\ti = 0\n\tj = 0\n\tleft = 1\n\tright = x\n\tflag = False\n\twhile left < right:\n\t\tmi = (left + right) // 2\n\t\tif a[mi] <= x:\n\t\t\tleft = mi + 1\n\t\t\ti += 1\n\t\telse:\n\t\t\tright = mi\n\t\t\tj += 1\n\tif left > 0 and a[left - 1] == x:\n\t\treturn (i - 1, j)\n\telse:\n\t\treturn (-1, -1)\n\ndef nCr(n, r):\n\treturn fact(n) // (fact(r) * fact(n - r))\n\ndef fact(n):\n\tres = 1\n\tfor i in range(2, n + 1):\n\t\tres = res * i\n\treturn res\n\ndef primefactors(n):\n\tnum = 0\n\twhile n % 2 == 0:\n\t\tnum += 1\n\t\tn = n / 2\n\tfor i in range(3, int(math.sqrt(n)) + 1, 2):\n\t\twhile n % i == 0:\n\t\t\tnum += 1\n\t\t\tn = n // i\n\tif n > 2:\n\t\tnum += 1\n\treturn num\n\ndef ask(a):\n\tprint('? {}'.format(a), flush=True)\n\tn = I()\n\treturn n\n\ndef dfs(i, p):\n\t(a, tmp) = (0, 0)\n\tfor j in d[i]:\n\t\tif j != p:\n\t\t\ta += 1\n\t\t\ttmp += dfs(j, i)\n\tif a == 0:\n\t\treturn 0\n\treturn tmp / a + 1\n\ndef primeFactors(n):\n\tl = []\n\twhile n % 2 == 0:\n\t\tl.append(2)\n\t\tn = n // 2\n\tif n > 2:\n\t\tl.append(n)\n\treturn l\nn = I()\ns = lint()\nfor i in range(1, n):\n\ts[i] += s[i - 1]\ntmp = s[-1] // 3\nif s[-1] % 3 != 0:\n\tprint(0)\nelse:\n\to = 0\n\tans = 0\n\tfor i in range(1, n - 1):\n\t\tif s[i - 1] == tmp:\n\t\t\to += 1\n\t\tif s[-1] - s[i] == tmp:\n\t\t\tans += o\n\tprint(ans)\n", "n = int(input())\na = [int(i) for i in input().split()]\nx = sum(a) / 3\ny = z = h = 0\nif int(x) != x:\n\tprint(0)\nelse:\n\ty = a[0]\n\tfor i in range(1, n):\n\t\tif y == 2 * x:\n\t\t\tz += h\n\t\tif y == x:\n\t\t\th += 1\n\t\ty += a[i]\n\tprint(z)\n", "n = int(input())\na = list(map(int, input().split()))\n(res, sum_a) = (0, sum(a))\nif sum_a % 3 == 0:\n\t(sum_a_3, sum_a_2_3, s, cnt) = (sum_a // 3, 2 * sum_a // 3, 0, 0)\n\tfor i in range(0, n - 1):\n\t\ts += a[i]\n\t\tif s == sum_a_2_3:\n\t\t\tres += cnt\n\t\tcnt += s == sum_a_3\nprint(res)\n", "n = int(input())\na = [int(i) for i in input().split()]\nm = [0] * n\ns = sum(a)\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tds = s / 3\n\tts = 0\n\tfor i in range(n):\n\t\tts += a[n - i - 1]\n\t\tif ts == ds:\n\t\t\tm[n - i - 1] = 1\n\tfor i in range(n - 2):\n\t\tm[n - i - 2] += m[n - i - 1]\n\tts = 0\n\tf = 0\n\tfor i in range(n - 2):\n\t\tts += a[i]\n\t\tif ts == ds:\n\t\t\tf += m[i + 2]\n\tprint(f)\n", "from collections import defaultdict\nn = int(input())\na = [int(i) for i in input().split()]\ns = sum(a)\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tk = s // 3\n\tk1 = 2 * k\n\tpref = [0] * n\n\tpref[0] = a[0]\n\tc = 0\n\tc1 = 0\n\ts = 0\n\tif k == 0:\n\t\tfor i in range(n - 1):\n\t\t\ts += a[i]\n\t\t\tif s == 0:\n\t\t\t\tc += 1\n\t\tprint(c * (c - 1) // 2)\n\telse:\n\t\tfor i in range(n - 1):\n\t\t\ts += a[i]\n\t\t\tif s == k:\n\t\t\t\tc += 1\n\t\t\telif s == k1:\n\t\t\t\tc1 += c\n\t\tprint(c1)\n", "n = int(input())\na = [int(i) for i in input().split()]\nif sum(a) % 3 != 0 or n < 3:\n\tprint(0)\n\texit(0)\ncs = [0] * (len(a) + 1)\n(ans, cnt) = (0, 0)\ni = 0\nwhile i < len(a):\n\tcs[i + 1] = cs[i] + a[i]\n\ti += 1\nsum1 = cs[-1] // 3\nsum2 = sum1 * 2\ni = 1\nwhile i < len(cs) - 1:\n\tif cs[i] == sum2:\n\t\tans += cnt\n\tif cs[i] == sum1:\n\t\tcnt += 1\n\ti += 1\nprint(ans)\n", "import os\nimport sys\nfrom io import BytesIO, IOBase\nimport math\n\ndef main():\n\tpass\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\nimport os\nimport sys\nfrom io import BytesIO, IOBase\n\ndef main():\n\tpass\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nn = int(input())\nl = list(map(int, input().split()))\ns = sum(l)\nif s % 3 == 0:\n\tpre = [l[0]]\n\tfor i in range(1, n):\n\t\tpre.append(pre[i - 1] + l[i])\n\ta = 0\n\tb = 0\n\tcs = 0\n\tfor i in range(0, n - 1):\n\t\tif pre[i] == 2 * s // 3:\n\t\t\tb += a\n\t\tif pre[i] == s // 3:\n\t\t\ta += 1\n\tprint(b)\nelse:\n\tprint(0)\n", "n = int(input().strip())\nvals = [int(x) for x in input().strip().split(' ')]\nj = n - 2\ni = 1\nfull_sum = sum(vals)\ncnt = 0\nif full_sum % 3 != 0:\n\tprint(0)\n\texit()\npart_sum = full_sum // 3\nj_s = []\nsum_3 = 0\nfor j in range(n - 1, 0, -1):\n\tsum_3 += vals[j]\n\tif sum_3 == part_sum:\n\t\tj_s.append(j)\nj_s = j_s[::-1]\nif not j_s:\n\tprint(0)\n\texit()\nif full_sum == 0 and len(j_s) == n - 1:\n\tprint((n - 1) * (n - 2) // 2)\n\texit()\nsum_1 = 0\nfor i in range(0, n - 2):\n\tsum_1 += vals[i]\n\tif sum_1 != part_sum:\n\t\tcontinue\n\tfor j in range(len(j_s)):\n\t\tif j_s[j] - 1 > i:\n\t\t\tbreak\n\telse:\n\t\tj = len(j_s)\n\tcnt += len(j_s) - j\nprint(cnt)\n", "from sys import stdin\nfrom functools import reduce\n\ndef solve(n, arr):\n\tif n < 3:\n\t\treturn 0\n\tv = sum(arr)\n\tif v % 3 != 0:\n\t\treturn 0\n\tv /= 3\n\tans = [0] * n\n\ttmp = 0\n\tfor i in range(n - 1, -1, -1):\n\t\ttmp += arr[i]\n\t\tif tmp == v:\n\t\t\tans[i] += 1\n\tfor i in range(n - 2, -1, -1):\n\t\tans[i] += ans[i + 1]\n\ttmp = 0\n\tval = 0\n\tfor i in range(n - 2):\n\t\ttmp += arr[i]\n\t\tif tmp == v:\n\t\t\tval += ans[i + 2]\n\treturn val\nn = int(stdin.readline())\narr = list(map(int, stdin.readline().split(' ')))\nprint(solve(n, arr))\n", "n = int(input())\nlist1 = list(map(int, input().split()))\nlst = []\nlst.append(list1[0])\nx = 0\nsum1 = 0\nfor i in range(1, n):\n\tlst.append(lst[i - 1] + list1[i])\nfor i in range(n - 1):\n\tif lst[i] * 3 == 2 * lst[n - 1]:\n\t\tsum1 += x\n\tif lst[i] * 3 == lst[n - 1]:\n\t\tx += 1\nprint(sum1)\n", "import sys\nimport os\nfrom io import BytesIO, IOBase\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nmod = 10 ** 9 + 7\n\ndef lcm(x, y):\n\treturn x * y / gcd(x, y)\n\ndef fact(x, mod=mod):\n\tans = 1\n\tfor i in range(1, x + 1):\n\t\tans = ans * i % mod\n\treturn ans\n\ndef arr2D(n, m, default=0):\n\tlst = []\n\tfor i in range(n):\n\t\ttemp = [default] * m\n\t\tlst.append(temp)\n\treturn lst\n\ndef sortDictV(x):\n\treturn {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}\n\ndef solve(n, lst):\n\tsumm = sum(lst)\n\tif summ % 3:\n\t\treturn 0\n\t(pref, suff) = ([0] * n, [0] * n)\n\tprefSum = 0\n\tfor i in range(n - 1):\n\t\tprefSum += lst[i]\n\t\tif prefSum == summ // 3:\n\t\t\tpref[i] = 1\n\t\tif summ - prefSum == summ // 3:\n\t\t\tsuff[i + 1] = 1\n\tsuffN = 0\n\tcntSuff = sum(suff)\n\tcnt = 0\n\tif suff[0] == 1:\n\t\tsuffN += 1\n\tfor i in range(n - 1):\n\t\tif suff[i + 1] == 1:\n\t\t\tsuffN += 1\n\t\tif pref[i] == 1:\n\t\t\tcnt += cntSuff - suffN\n\treturn cnt\nn = int(input())\nlst = list(map(int, input().split()))\nprint(solve(n, lst))\n", "def main():\n\tn = int(input())\n\tval = list(map(int, input().split()))\n\ts = sum(val)\n\tif s % 3:\n\t\tprint(0)\n\t\treturn\n\telse:\n\t\ts //= 3\n\t\tcnt = 0\n\t\tans = 0\n\t\ttemp = 0\n\t\tfor i in range(n - 1):\n\t\t\ttemp += val[i]\n\t\t\tif temp == 2 * s:\n\t\t\t\tans += cnt\n\t\t\tif temp == s:\n\t\t\t\tcnt += 1\n\t\tprint(ans)\nmain()\n", "n = int(input())\na = list(map(int, input().split()))\nb = [0 for i in range(n)]\nsum = 0\nfor i in range(len(a)):\n\tsum += a[i]\nif sum % 3 != 0:\n\tprint(0)\nelse:\n\tfans = 0\n\trs = sum // 3\n\tk = n - 1\n\ts = 0\n\tcnt = 0\n\twhile k >= 2:\n\t\ts += a[k]\n\t\tif s == rs:\n\t\t\tcnt += 1\n\t\tb[k] = cnt\n\t\tk -= 1\n\tfs = 0\n\tfor i in range(len(a) - 2):\n\t\tfs += a[i]\n\t\tif fs == rs:\n\t\t\tfans += b[i + 2]\n\tprint(fans)\n", "n = int(input())\na = [int(x) for x in input().split()]\nq1 = [0] * (n + 10)\np1 = [0] * (n + 10)\nq2 = [0] * (n + 10)\np2 = [0] * (n + 10)\nq1[0] = a[0]\np1[n - 1] = a[n - 1]\ntot = 0\nfor i in range(n):\n\ttot += a[i]\nif tot % 3 != 0 and n <= 2:\n\tprint(0)\nelse:\n\tif a[0] == tot / 3:\n\t\tq2[0] = 1\n\tif a[n - 1] == tot / 3:\n\t\tp2[n - 1] = 1\n\tfor i in range(1, n - 2):\n\t\tq1[i] = q1[i - 1] + a[i]\n\t\tp1[n - i - 1] = p1[n - i] + a[n - i - 1]\n\t\tp2[n - i - 1] = p2[n - i]\n\t\tif q1[i] == tot / 3:\n\t\t\tq2[i] = 1\n\t\tif p1[n - i - 1] == tot / 3:\n\t\t\tp2[n - i - 1] += 1\n\tans = 0\n\tfor i in range(0, n - 2):\n\t\tif q2[i] == 1:\n\t\t\tans += q2[i] * p2[i + 2]\n\tprint(ans)\n", "import math\nfrom collections import *\nfrom heapq import *\ninf = 10 ** 18\nmod = 10 ** 9 + 7\nn = int(input())\na = list(map(int, input().split()))\nif sum(a) % 3:\n\tprint(0)\n\texit()\nsumm = 0\nreq = sum(a) // 3\nc = 0\nans = 0\nfor i in range(n - 1):\n\tsumm += a[i]\n\tif summ == 2 * req:\n\t\tans += c\n\tif summ == req:\n\t\tc += 1\nprint(ans)\n", "import bisect\nn = int(input())\narr = [int(x) for x in input().split()]\ntotal = sum(arr)\ndp = [0]\nocc = {}\nfor (ind, item) in enumerate(arr):\n\ttemp = item + dp[-1]\n\tdp.append(temp)\n\tif temp in occ:\n\t\tocc[temp].append(ind)\n\telse:\n\t\tocc[temp] = [ind]\nif n <= 2 or total % 3:\n\tprint(0)\nelse:\n\tif total:\n\t\tans = 0\n\t\ta = total // 3\n\t\tb = 2 * a\n\t\tif a in occ and b in occ:\n\t\t\tfor ind in occ[a]:\n\t\t\t\tans += len(occ[b]) - bisect.bisect_left(occ[b], ind + 1)\n\t\telse:\n\t\t\tans = 0\n\telse:\n\t\tzeros = len(occ[0]) - 2\n\t\tans = zeros * (zeros + 1) // 2\n\tprint(ans)\n", "n = int(input())\nl = list(map(int, input().split()))\ntot = sum(l)\nif tot % 3:\n\tprint(0)\nelif tot:\n\tb1 = tot // 3\n\tb2 = 2 * b1\n\tout = 0\n\tfirst = 0\n\ts = 0\n\tfor v in l:\n\t\ts += v\n\t\tif s == b1:\n\t\t\tfirst += 1\n\t\telif s == b2:\n\t\t\tout += first\n\tprint(out)\nelse:\n\ts = 0\n\tcount = -1\n\tfor v in l:\n\t\ts += v\n\t\tif s == 0:\n\t\t\tcount += 1\n\tprint(count * (count - 1) // 2)\n", "import bisect\nn = int(input())\nA = list(map(int, input().split()))\nprefixes = 0\npre = []\nfor i in range(n):\n\tprefixes += A[i]\n\tpre.append(prefixes)\nU = pre[-1]\nif U % 3 != 0:\n\tprint(0)\nelse:\n\tk = U // 3\n\tcount = 0\n\tone = []\n\tdouble = []\n\tfor i in range(n):\n\t\tif pre[i] == k and i < n - 2:\n\t\t\tone.append(i)\n\t\tif pre[i] == 2 * k and i < n - 1:\n\t\t\tdouble.append(i)\n\tcount = 0\n\tfor k in one:\n\t\tind = bisect.bisect_left(double, k + 1)\n\t\tcount += len(double) - ind\n\tprint(count)\n", "n = int(input())\na = list(map(int, input().strip().split()))\nsumi = sum(a)\nif sumi % 3 != 0:\n\tprint(0)\nelse:\n\ttar = sumi // 3\n\ti = 0\n\tpre = 0\n\tnum = 0\n\tfor i in range(n):\n\t\tpre += a[i]\n\t\tif pre == tar:\n\t\t\tbreak\n\tj = n - 1\n\tpost = 0\n\tstops = [0 for i in range(n)]\n\twhile j > i + 1:\n\t\tpost += a[j]\n\t\tif post == tar:\n\t\t\tnum += 1\n\t\t\tstops[j] = num\n\t\telse:\n\t\t\tstops[j] = num\n\t\tj -= 1\n\tj = 0\n\tans = 0\n\tif sum(stops):\n\t\tans = stops[i + 2]\n\tfor k in range(i + 1, n - 2):\n\t\tpre += a[k]\n\t\tif pre == tar:\n\t\t\tans += stops[k + 2]\n\tprint(ans)\n", "n = int(input())\nc = list(map(int, input().split()))\npre = [0 for _ in range(n)]\npre[0] = c[0]\nfor i in range(1, n):\n\tpre[i] = c[i]\n\tpre[i] += pre[i - 1]\nif pre[-1] % 3 != 0:\n\tprint(0)\n\texit(0)\n(u, v) = (0, 0)\na = pre[-1] // 3\nfor i in range(n):\n\tif pre[i] == 2 * a and i != n - 1:\n\t\tv += u\n\tif pre[i] == a:\n\t\tu += 1\nprint(v)\n", "(n, a) = (int(input()), list(map(int, input().split())))\ns = [0]\ns += (s[i] + a[i] for i in range(n))\nif s[n] % 3 or 2 * s[n] % 3:\n\tprint(0)\nelse:\n\t(d, ans) = (0, 0)\n\tfor i in range(1, n):\n\t\tif 3 * s[i] == 2 * s[n]:\n\t\t\tans += d\n\t\tif 3 * s[i] == s[n]:\n\t\t\td += 1\n\tprint(ans)\n", "import sys\nimport math\nfrom collections import defaultdict\n\ndef read_line():\n\treturn sys.stdin.readline()[:-1]\n\ndef read_int():\n\treturn int(sys.stdin.readline())\n\ndef read_int_line():\n\treturn [int(v) for v in sys.stdin.readline().split()]\nn = read_int()\na = read_int_line()\ns = [0] * (n + 1)\nfor i in range(n):\n\ts[i + 1] = s[i] + a[i]\nif s[n] % 3 != 0:\n\tprint(0)\nelse:\n\tcnt = [0] * n\n\tfor i in range(n):\n\t\tif s[n] - s[i] == s[n] // 3:\n\t\t\tcnt[i] = 1\n\tans = [0] * n\n\tans[n - 1] = cnt[n - 1]\n\tfor i in range(1, n):\n\t\tans[n - 1 - i] = ans[n - i] + cnt[n - 1 - i]\n\tw = 0\n\tfor i in range(1, n - 1):\n\t\tif s[i] == s[n] // 3:\n\t\t\tw += ans[i + 1]\n\tprint(w)\n", "def count_arr_div_3arrs(arr, n):\n\tarr = [0] + arr\n\ttotal_sum = sum(arr)\n\tif total_sum % 3:\n\t\treturn 0\n\telse:\n\t\tcount = 0\n\t\tresult = 0\n\t\tfor i in range(1, n + 1):\n\t\t\tarr[i] += arr[i - 1]\n\t\t\tif i > 1 and i < n and (3 * arr[i] == 2 * total_sum):\n\t\t\t\tresult += count\n\t\t\tif arr[i] == total_sum / 3:\n\t\t\t\tcount += 1\n\t\treturn result\n\ndef run(arr):\n\tres = count_arr_div_3arrs(arr, len(arr))\n\treturn res\nn = input()\narray = list(map(int, input().split()))\nprint(run(array))\n", "def main():\n\tn = int(input())\n\tnums = list(map(int, input().split()))\n\ts = sum(nums)\n\tif s % 3:\n\t\tprint(0)\n\t\treturn\n\trunning = 0\n\tcount = [0 for i in range(n)]\n\tfor (i, e) in enumerate(nums):\n\t\tif running == s / 3 * 2:\n\t\t\tcount[i] = 1\n\t\trunning += e\n\tj = n - 2\n\twhile j >= 0:\n\t\tcount[j] += count[j + 1]\n\t\tj -= 1\n\tprefix = 0\n\tresult = 0\n\tfor (i, e) in enumerate(nums):\n\t\tprefix += e\n\t\tif prefix == s / 3:\n\t\t\tif i + 2 < n:\n\t\t\t\tresult += count[i + 2]\n\tprint(result)\nmain()\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\nr = 0\nif s % 3 == 0:\n\tls = s // 3\n\trs = 2 * ls\n\tc = 0\n\ts = 0\n\tfor i in range(n - 1):\n\t\ts += a[i]\n\t\tif s == rs:\n\t\t\tr += c\n\t\tif s == ls:\n\t\t\tc += 1\nprint(r)\n", "n = int(input())\na = list(map(int, input().split()))\nsums = [a[0]]\nfor i in range(1, len(a)):\n\tsums.append(a[i] + sums[-1])\nif sums[-1] % 3 == 0:\n\tfind = sums[-1] // 3\n\tl = []\n\ts = 0\n\tfor i in range(len(sums) - 1):\n\t\tif sums[i] == find:\n\t\t\tl.append([i, 0])\n\t\tif sums[i] == 2 * find:\n\t\t\tl.append([i, 1])\n\t\t\ts += 1\n\tl.sort(key=lambda x: x[0] or x[1])\n\tcount = 0\n\tif find == 0:\n\t\ts -= 1\n\tfor i in range(len(l)):\n\t\tif l[i][1] == 0:\n\t\t\tcount += s\n\t\tif l[i][1] == 1:\n\t\t\ts -= 1\n\tprint(count)\nelse:\n\tprint(0)\n", "n = int(input())\na = list(map(lambda x: int(x), input().split(' ')))\nfor i in range(1, n):\n\ta[i] += a[i - 1]\nif a[-1] % 3 != 0:\n\tprint(0)\n\texit()\ns = a[-1] // 3\ni = 0\nans = 0\nj = 0\ncounter = [int(a[0] == s)]\nfor i in range(1, n - 1):\n\tif a[i] == s:\n\t\tcounter.append(1 + counter[i - 1])\n\telse:\n\t\tcounter.append(counter[i - 1])\n\tif a[i] == s * 2:\n\t\tans += counter[i - 1]\nprint(ans)\n", "def main():\n\tn = int(input())\n\tl = list(map(int, input().split()))\n\ts = 0\n\tfor x in l:\n\t\ts += x\n\tif s % 3 != 0:\n\t\tprint(0)\n\telse:\n\t\ts //= 3\n\t\tsu = 0\n\t\tk = 0\n\t\tans = 0\n\t\tl.pop()\n\t\tfor x in l:\n\t\t\tsu += x\n\t\t\tif su == 2 * s:\n\t\t\t\tans += k\n\t\t\tif su == s:\n\t\t\t\tk += 1\n\t\tprint(ans)\nmain()\n", "n = int(input())\narr = [int(var) for var in input().split()]\nback = [0 for _ in range(n)]\ntotal = sum(arr)\nif total % 3 == 0:\n\ttotal = total // 3\n\tback[-1] = 1 if arr[-1] == total else 0\n\tcurrSum = arr[-1]\n\tfor i in reversed(range(n - 1)):\n\t\tcurrSum += arr[i]\n\t\tback[i] = back[i + 1] + 1 if currSum == total else back[i + 1]\n\t(currSum, ways) = (0, 0)\n\tfor i in range(n):\n\t\tcurrSum += arr[i]\n\t\tif currSum == total:\n\t\t\tways += back[i + 2] if i + 2 < n else 0\n\tprint(ways)\nelse:\n\tprint(0)\n", "n = int(input())\nways = list(map(int, input().split()))\ntotal = sum(ways)\noutput = 0\nif total % 3 == 0:\n\tsequence = total // 3\n\tcarry = total - sequence\n\tcurrent = 0\n\tw = 0\n\tfor i in range(n - 1):\n\t\tcurrent += ways[i]\n\t\tif current == carry:\n\t\t\toutput += w\n\t\tif current == sequence:\n\t\t\tw += 1\nprint(output)\n", "import sys\nn = int(input())\ns = list(map(int, input().split()))\ndp = [s[i] for i in range(n)]\nfor i in range(1, n):\n\tdp[i] += dp[i - 1]\nif dp[-1] / 3 != dp[-1] // 3 or n == 2:\n\tprint(0)\n\tsys.exit()\nx = dp[-1] // 3 * 2\ny = dp[-1] // 3\ndp1 = [0 for i in range(n)]\naux = dp[::-1]\nfor i in range(1, n):\n\tdp1[i] = dp1[i - 1]\n\tif aux[i] == x:\n\t\tdp1[i] += 1\nans = 0\ndp1 = dp1[::-1]\nfor i in range(n - 1):\n\tif dp[i] == y:\n\t\tans += dp1[i + 1]\nprint(ans)\n", "n = int(input())\narray = list(map(int, input().split(' ')))\nsum_each_part = sum(array) / 3\ncumulative_sum = [array[0]]\nans = 0\nfor i in range(1, n):\n\tcumulative_sum.append(cumulative_sum[i - 1] + array[i])\ncnt = [0] * (n + 1)\nsum_array = 0\nfor i in range(n):\n\tsum_array = sum_array + array[i]\n\tprev_cnt = cnt[i - 1] if i > 0 else 0\n\tcnt[i] = prev_cnt + (1 if sum_array == sum_each_part else 0)\nfor i in range(n - 1):\n\tif cumulative_sum[i] == 2 * sum_each_part:\n\t\tans = ans + cnt[i - 1] if i > 0 else 0\nprint(ans)\n", "n = int(input())\narr = [int(x) for x in input().split()]\ns = sum(arr)\nif s % 3 != 0:\n\tprint(0)\n\texit()\nif n < 3:\n\tprint(0)\n\texit()\nt = int(s / 3)\nprefix = [x for x in arr]\nfor i in range(1, n):\n\tprefix[i] = prefix[i - 1] + arr[i]\nsuffix = [x for x in arr]\nfor i in range(n - 2, -1, -1):\n\tsuffix[i] = suffix[i + 1] + arr[i]\nnew = [1 if x == t else 0 for x in suffix]\nsuffix_new = [x for x in new]\nfor i in range(n - 2, -1, -1):\n\tsuffix_new[i] = suffix_new[i + 1] + new[i]\nanswer = 0\nfor i in range(n - 2):\n\tif prefix[i] == t:\n\t\tanswer += suffix_new[i + 2]\nprint(answer)\n", "import math\nfrom bisect import bisect_right\nn = int(input())\na = list(map(int, input().split()))\nval = sum(a)\nif val % 3 != 0:\n\tprint(0)\nelse:\n\tval //= 3\n\tp1 = []\n\tp2 = []\n\ttsum = 0\n\tfor i in range(n):\n\t\ttsum += a[i]\n\t\tif tsum == val:\n\t\t\tp1.append(i)\n\t\tif tsum == 2 * val:\n\t\t\tp2.append(i)\n\tif val != 0:\n\t\tans = 0\n\t\tfor i in p1:\n\t\t\tans += len(p2) - bisect_right(p2, i)\n\t\tprint(ans)\n\telse:\n\t\tp1 = len(p1) - 1\n\t\tprint(p1 * (p1 - 1) // 2)\n", "N = int(input())\nA = list(map(int, input().split()))\nB = [0] * N\nS = sum(A)\nif S % 3:\n\tprint(0)\nelse:\n\tS //= 3\n\tT = 0\n\tfor i in range(N):\n\t\tT += A[i]\n\t\tif T == S:\n\t\t\tB[i] = 1\n\tfor i in range(1, N):\n\t\tB[i] += B[i - 1]\n\t(T, C) = (0, 0)\n\tfor i in range(N - 1, 1, -1):\n\t\tT += A[i]\n\t\tif T == S:\n\t\t\tC += B[i - 2]\n\tprint(C)\n", "import sys\ninput = sys.stdin.buffer.readline\nimport bisect\n\ndef I():\n\treturn list(map(int, input().split()))\n\ndef sieve(n):\n\ta = [1] * n\n\tfor i in range(2, n):\n\t\tif a[i]:\n\t\t\tfor j in range(i * i, n, i):\n\t\t\t\ta[j] = 0\n\treturn a\nn = int(input())\narr = I()\nc = 0\nprefarr = [0] * (n + 1)\nmp = {}\ntotal = sum(arr)\nfor i in range(n - 1):\n\tprefarr[i] = arr[i] + prefarr[i - 1]\n\tif 3 * prefarr[i] == 2 * total:\n\t\tcurr = mp.get(prefarr[i], [])\n\t\tcurr.append(i)\n\t\tmp[prefarr[i]] = curr\nj = 1\nfor i in range(n - 1):\n\tcurrsum = prefarr[i]\n\ttofind = 2 * prefarr[i]\n\tif tofind in mp:\n\t\tcurr = mp[tofind]\n\t\tc += len(curr) - bisect.bisect(curr, i)\nprint(c)\n", "n = int(input())\na = [int(i) for i in input().split()]\ns = sum(a)\nb = [0 for i in range(n)]\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tr = s // 3\n\tt = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tt += a[i]\n\t\tif t == r:\n\t\t\tb[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tb[i] += b[i + 1]\n\tt = 0\n\tans = 0\n\tfor i in range(n - 2):\n\t\tt += a[i]\n\t\tif t == r:\n\t\t\tans += b[i + 2]\n\tprint(ans)\n", "n = int(input())\na = [int(x) for x in input().split()]\ns = sum(a)\nif s % 3:\n\tprint(0)\n\texit()\ncur = 0\ncnt = 0\ns //= 3\nans = 0\nfor x in a:\n\tcur += x\n\tif s == 0:\n\t\tif cur == 0:\n\t\t\tcnt += 1\n\t\tcontinue\n\tif cur == s:\n\t\tcnt += 1\n\tif cur == s * 2:\n\t\tans += cnt\nif s == 0:\n\tprint((cnt - 1) * (cnt - 2) // 2)\nelse:\n\tprint(ans)\n", "import sys\nimport collections\nimport math\nimport functools\nimport itertools\nimport bisect\nimport operator\nimport heapq\nimport random\ntrue = True\nfalse = False\nnull = None\ntcid = 0\ntcmax = 99999999\n\ndef compute(val, func):\n\treturn func(val)\n\ndef seq(lo, hi, step=1):\n\treturn range(lo, hi + 1, step)\n\ndef sround(val, nd):\n\treturn f'{val:.{nd}f}'\n\ndef ceil(a, b):\n\tans = a // b\n\tif a % b != 0:\n\t\tans += 1\n\treturn ans\n\ndef perr(*args, **kwargs):\n\tprint(*args, file=sys.stderr, **kwargs)\n\ndef line():\n\tln = sys.stdin.readline().strip()\n\tif ln == '':\n\t\tsys.exit()\n\treturn ln\n\ndef lines(n):\n\treturn [line() for i in range(n)]\n\ndef split(ln=None):\n\treturn (ln or line()).split()\n\ndef num(str=None):\n\tstr = str or line()\n\treturn float(str) if '.' in str else int(str)\n\ndef nums(o=None):\n\tif o is not None:\n\t\tif isinstance(o, int):\n\t\t\to = lines(o)\n\t\telif isinstance(o, str):\n\t\t\to = split(o)\n\treturn list(map(num, o or split()))\n\ndef mainloop(tcid=1):\n\n\tdef solve():\n\t\tn = num()\n\t\ta = nums()\n\t\tjml = sum(a)\n\t\tif jml % 3 != 0:\n\t\t\treturn 0\n\t\tptri = jml // 3\n\t\tmult = 0\n\t\tans = 0\n\t\tif a[0] == ptri:\n\t\t\tmult = 1\n\t\tfor ii in range(1, n - 1):\n\t\t\ta[ii] += a[ii - 1]\n\t\t\tif a[ii] == 2 * ptri:\n\t\t\t\tans += mult\n\t\t\tif a[ii] == ptri:\n\t\t\t\tmult += 1\n\t\treturn ans\n\tprint(solve())\nwhile tcid < tcmax:\n\ttcid += 1\n\tmainloop(tcid)\n", "n = int(input())\nl = list(map(int, input().split()))\ns = sum(l)\n(m1, m2) = (s // 3, 2 * s // 3)\n(curr, prev, ans) = (0, 0, 0)\nif s % 3:\n\tprint(0)\n\texit()\nfor i in range(n - 1):\n\tcurr += l[i]\n\tif curr == m2:\n\t\tans += prev\n\tif curr == m1:\n\t\tprev += 1\nprint(ans)\n", "n = int(input())\na = list(map(int, input().split(' ')))\ntotal = int(sum(a) / 3)\ncnt = list()\ncnt1 = list()\ncount = 0\nfor i in range(0, n - 2):\n\tcount = count + a[i]\n\tif count == total:\n\t\tcnt.append(i)\ncount = 0\ni = n - 1\nwhile i > 1:\n\tcount = count + a[i]\n\tif count == total:\n\t\tcnt1.append(i)\n\ti = i - 1\ncnt.sort()\ncnt1.sort()\nk = len(cnt1)\nt = len(cnt)\ni = 0\nj = 0\nnumber = 0\nwhile j < k and i < t:\n\tif cnt1[j] > cnt[i] + 1:\n\t\tnumber = number + (k - j)\n\t\ti = i + 1\n\telse:\n\t\tj = j + 1\nif sum(a) / 3 == int(sum(a) / 3):\n\tprint(number)\nelse:\n\tprint(0)\n", "n = int(float(input()))\na = [int(i) for i in input().strip('\\n').split(' ')]\nsum = 0\nfor i in a:\n\tsum += i\nif sum % 3 != 0:\n\tprint('0')\nelse:\n\tone = sum // 3\n\ttwo = 2 * one\n\tnum_j = 0\n\tgaps_i = []\n\ttemp = 0\n\tfor i in range(n - 1):\n\t\ttemp += a[i]\n\t\tif temp == two:\n\t\t\tnum_j += 1\n\t\tif temp == one:\n\t\t\tgaps_i.append(num_j)\n\tans = num_j * len(gaps_i)\n\tfor i in gaps_i:\n\t\tans -= i\n\tprint(ans)\n", "n = int(input())\nli = list(map(int, input().split()))\nif sum(li) % 3 != 0:\n\tprint(0)\n\tquit()\nt = sum(li) // 3\nfor i in range(1, n):\n\tli[i] += li[i - 1]\ncnt = [0] * n\nfor i in range(n - 2, -1, -1):\n\tcnt[i] = cnt[i + 1]\n\tif li[i] == 2 * t:\n\t\tcnt[i] += 1\nans = 0\nfor i in range(0, n - 1):\n\tif li[i] != t:\n\t\tcontinue\n\tans += cnt[i + 1]\nprint(ans)\n", "def chk():\n\tprint('HI')\nMOD = 10 ** 9 + 7\nI = lambda : list(map(int, input().split()))\n(n,) = I()\nl = I()\n(s, count) = (sum(l), 0)\nif not s % 3:\n\tss = 0\n\tt = s // 3\n\tz = 0\n\tfor i in range(n - 1):\n\t\tss += l[i]\n\t\tif ss == 2 * t:\n\t\t\tcount += z\n\t\tif ss == t:\n\t\t\tz += 1\nprint(count)\n", "n = int(input())\na = list(map(int, input().split()))\nsum = 0\nfor i in a:\n\tsum += i\nif sum % 3 != 0:\n\tprint(0)\nelse:\n\tneed = sum // 3\n\tdp1 = [0 for i in range(n)]\n\tdp2 = [0 for i in range(n)]\n\tsum = 0\n\tfor i in range(n - 1):\n\t\tsum += a[i]\n\t\tif sum == need:\n\t\t\tdp1[i + 1] = dp1[i] + 1\n\t\telse:\n\t\t\tdp1[i + 1] = dp1[i]\n\t\tif sum == 2 * need:\n\t\t\tdp2[i + 1] = dp2[i] + dp1[i]\n\t\telse:\n\t\t\tdp2[i + 1] = dp2[i]\n\tprint(dp2[n - 1])\n", "from bisect import bisect\nwhile True:\n\ttry:\n\t\tn = int(input())\n\texcept EOFError:\n\t\tbreak\n\ta = list(map(int, input().split()))\n\ts = [0]\n\tr = [0]\n\td = {}\n\tfor v in a:\n\t\ts.append(s[-1] + v)\n\ta.reverse()\n\tfor (i, v) in enumerate(a):\n\t\tr.append(r[-1] + v)\n\t\td.setdefault(r[-1], list()).append(n - 1 - i)\n\tfor v in d.values():\n\t\tif v[-1] == 0:\n\t\t\tv.pop()\n\t\tv.reverse()\n\tresult = 0\n\tfor (i, v) in enumerate(s):\n\t\tif i and v * 3 == s[-1]:\n\t\t\tresult += len(d[v]) - bisect(d[v], i)\n\tprint(result)\n", "from pprint import pprint\nn = int(input().strip())\nif n < 3:\n\tprint(0)\n\texit(0)\nl = [int(i) for i in input().strip().split()]\n\ndef init_clos():\n\tpartSum = 0\n\n\tdef summer(x):\n\t\tnonlocal partSum\n\t\tpartSum += x\n\t\treturn partSum\n\treturn summer\nfunc = init_clos()\nsums = [func(x) for x in l]\nif sums[-1] % 3 != 0:\n\tprint(0)\n\texit(0)\nborders = {1: [], 2: []}\nfor (index, s) in enumerate(sums):\n\tif s == sums[-1] // 3:\n\t\tborders[1].append(index)\n\tif s == sums[-1] * 2 // 3:\n\t\tborders[2].append(index)\nr = 0\n(li, ri) = (0, 0)\nlefts = borders[1]\nrights = borders[2]\nif sums[-1] == 0:\n\trights = rights[:-1]\nwhile li < len(lefts) and ri < len(rights):\n\tif lefts[li] < rights[ri]:\n\t\tr += len(rights) - ri\n\t\tli += 1\n\telse:\n\t\tri += 1\nprint(r)\n", "n = int(input())\nlis = input().split()\nfor i in range(n):\n\tlis[i] = int(lis[i])\nlis = [0] + lis\nlis1 = [0]\nfor i in range(1, n + 1):\n\tlis1.append(lis1[i - 1] + lis[i])\nif lis1[n] == 0:\n\tlis2 = []\n\tfor i in range(1, n):\n\t\tif lis1[i] == 0:\n\t\t\tlis2.append(i)\n\tm = len(lis2)\n\tif m >= 2:\n\t\tprint(int(m * (m - 1) / 2))\n\telse:\n\t\tprint(0)\nelse:\n\tlis2 = []\n\tlis3 = []\n\tfor i in range(n):\n\t\tif lis1[i] == lis1[n] / 3:\n\t\t\tlis2.append(i)\n\t\tif lis1[i] == lis1[n] * 2 / 3:\n\t\t\tlis3.append(i)\n\tm1 = len(lis2)\n\tm2 = len(lis3)\n\tif m1 >= 1 and m2 >= 1:\n\t\tlis4 = [0 for k in range(m1)]\n\t\tfor i in range(m1):\n\t\t\tj = 0\n\t\t\twhile j <= m2 - 1 and lis3[j] <= lis2[i]:\n\t\t\t\tj += 1\n\t\t\tlis4[i] = m2 - j\n\t\tprint(sum(lis4))\n\telse:\n\t\tprint(0)\n", "n = int(input())\nv = list(map(int, input().split()))\ns = sum(v)\nif s % 3 == 0:\n\tcont = 0\n\ts = s / 3\n\tr = 0\n\tsoma = 0\n\tfor i in range(n - 1):\n\t\tsoma += v[i]\n\t\tif soma == 2 * s:\n\t\t\tr += cont\n\t\tif soma == s:\n\t\t\tcont += 1\n\tprint(r)\nelse:\n\tprint(0)\n", "n = int(input())\narr = [*map(int, input().split())]\nans = 0\nfrom itertools import accumulate\nfsum = [*accumulate(arr)]\nidx = []\nfor i in range(n - 1):\n\tif (fsum[-1] - fsum[i]) * 3 == fsum[-1]:\n\t\tidx.append(i + 1)\nfrom bisect import bisect\nfor i in range(n):\n\tif fsum[i] * 3 == fsum[-1]:\n\t\tans += len(idx) - bisect(idx, i + 1)\nprint(ans)\n", "from collections import Counter\nn = int(input())\narray = list(map(int, input().split()))\nparsum = [array[0]]\nfor i in range(1, n):\n\tparsum += [parsum[-1] + array[i]]\ntotal = parsum[-1]\nif total % 3 != 0:\n\tprint(0)\nelif total == 0:\n\tk = parsum.count(0)\n\tprint((k - 2) * (k - 1) // 2)\nelse:\n\tthirds = list(filter(lambda x: x == total // 3 or x == 2 * total // 3, parsum))\n\tcount = Counter(thirds)\n\tif len(count) < 2:\n\t\tprint(0)\n\telse:\n\t\tk = count[total // 3]\n\t\tl = count[2 * total // 3]\n\t\toutput = 0\n\t\ti = 0\n\t\twhile k != 0:\n\t\t\tif thirds[i] == total // 3:\n\t\t\t\toutput += l\n\t\t\t\tk -= 1\n\t\t\telse:\n\t\t\t\tl -= 1\n\t\t\ti += 1\n\t\tprint(output)\n", "arg = int(input())\nlist1 = list(map(int, input().strip().split()))\ns = sum(list1)\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tct = 0\n\tt = 0\n\tres = 0\n\tfor element in range(0, len(list1) - 1):\n\t\tt += list1[element]\n\t\tif t == s * 2 // 3:\n\t\t\tres += ct\n\t\tif t == s // 3:\n\t\t\tct += 1\n\tprint(res)\n", "n = int(input())\na = list(map(float, input().split()))\ntotal = sum(a)\none_third = total / 3\ntwo_third = 2 * total / 3\none_third_cummulative_array = [0] * n\ntwo_third_array = [0] * n\none_third_count = 0\nrunning_total = 0\nfor i in range(n):\n\trunning_total += a[i]\n\tif running_total == one_third:\n\t\tone_third_count += 1\n\tif running_total == two_third:\n\t\ttwo_third_array[i] = 1\n\tone_third_cummulative_array[i] = one_third_count\ngrand_total = 0\nfor i in range(n - 2, 0, -1):\n\tif two_third_array[i] == 1:\n\t\tgrand_total += one_third_cummulative_array[i - 1]\nprint(grand_total)\n", "n = int(input())\narr = [int(x) for x in input().split()]\ns = sum(arr)\nif s % 3 != 0:\n\tprint(0)\n\texit()\nif n < 3:\n\tprint(0)\n\texit()\ncnt = [0] * n\ns /= 3\ncurr = 0\nfor i in range(n - 1, -1, -1):\n\tcurr += arr[i]\n\tif curr == s:\n\t\tcnt[i] = 1\nfor i in range(n - 2, -1, -1):\n\tcnt[i] = cnt[i + 1] + cnt[i]\nanswer = 0\ncurr = 0\nfor i in range(0, n - 2):\n\tcurr += arr[i]\n\tif curr == s:\n\t\tanswer += cnt[i + 2]\nprint(answer)\n", "import sys\nn = int(input())\nlst = list(map(int, input().split()))\nif n < 3:\n\tprint(0)\n\tsys.exit()\nsums = [lst[0]]\nfor i in range(1, n):\n\tsums.append(sums[-1] + lst[i])\nif sums[-1] == 0:\n\tzeroes = sums.count(0)\n\tprint((zeroes - 1) * (zeroes - 2) // 2)\n\tsys.exit()\nif sums[-1] % 3 != 0:\n\tprint(0)\n\tsys.exit()\nx = sums[-1] // 3\ny = x * 2\nf_y = [0 + (y == sums[0])]\nind = []\nif x == sums[0]:\n\tind.append(0)\nfor i in range(1, n - 1):\n\tif sums[i] == y:\n\t\tf_y.append(f_y[-1] + 1)\n\telse:\n\t\tif sums[i] == x:\n\t\t\tind.append(i)\n\t\tf_y.append(f_y[-1])\nif 0 in (len(ind), len(f_y)):\n\tprint(0)\n\tsys.exit()\nif ind[-1] == n - 2:\n\tind.pop()\ncount = 0\nfor index in ind:\n\tcount += f_y[-1] - f_y[index]\nprint(count)\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\nans = 0\nd = []\nh = [0] * n\nif s % 3:\n\tprint(ans)\nelse:\n\tby_3_sum = s // 3\n\ttemp_sum = 0\n\tfor i in range(n - 2):\n\t\ttemp_sum += a[i]\n\t\tif temp_sum == by_3_sum:\n\t\t\td.append(i)\n\ttemp_sum = 0\n\tcnt = 0\n\tfor i in range(n - 1, 1, -1):\n\t\ttemp_sum += a[i]\n\t\tif temp_sum == by_3_sum:\n\t\t\tcnt += 1\n\t\th[i] = cnt\n\tfor i in d:\n\t\tans += h[i + 2]\n\tprint(ans)\n", "def countWays(arr, n):\n\tcount = [0] * n\n\ts = sum(arr)\n\tif s % 3 != 0:\n\t\treturn 0\n\telse:\n\t\ts = s // 3\n\tss = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tss += arr[i]\n\t\tif ss == s:\n\t\t\tcount[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tcount[i] += count[i + 1]\n\tans = 0\n\tss = 0\n\tfor i in range(0, n - 2):\n\t\tss += arr[i]\n\t\tif ss == s:\n\t\t\tans += count[i + 2]\n\treturn ans\nn = int(input())\narr = list(map(int, input().split()))\nprint(countWays(arr, n))\n", "def solve():\n\tfrom math import factorial\n\tn = int(input())\n\tl = list(map(int, input().strip().split()))\n\ts = sum(l)\n\tif s % 3 != 0 or n == 1 or n == 2:\n\t\tprint(0)\n\telse:\n\t\tsp = s // 3\n\t\tc = 0\n\t\t(sp1, sp2) = (0, 0)\n\t\tfor i in range(n - 1):\n\t\t\tc += l[i]\n\t\t\tif c == sp * 2:\n\t\t\t\tsp2 += sp1\n\t\t\tif c == sp * 1:\n\t\t\t\tsp1 += 1\n\t\tprint(sp2)\nsolve()\n", "from bisect import bisect_left\nfrom collections import defaultdict\nn = int(input())\na = [int(x) for x in input().split()]\nm = sum(a)\nif m % 3 != 0:\n\tprint(0)\nelse:\n\tan = 0\n\tfor j in range(1, n):\n\t\ta[j] += a[j - 1]\n\tif m == 0:\n\t\tz = a.count(0) - 1\n\t\tprint(z * (z - 1) // 2)\n\telse:\n\t\tx = defaultdict(lambda : 0)\n\t\tfor j in range(n):\n\t\t\tif a[j] == m // 3:\n\t\t\t\tx[0] += 1\n\t\t\telif a[j] == 2 * (m // 3):\n\t\t\t\tan += x[0]\n\t\tprint(an)\n", "n = int(input())\na = list(map(int, input().split()))\nl = [a[0]]\nans = 0\nfor i in range(1, n):\n\tl.append(a[i] + l[i - 1])\nx = l[-1]\nif x % 3 == 0:\n\tx = x // 3\n\tif x == 0:\n\t\tc = l.count(0)\n\t\tc -= 2\n\t\tprint(c * (c + 1) // 2)\n\telse:\n\t\tc = 0\n\t\tfor i in range(n):\n\t\t\tif l[i] == x:\n\t\t\t\tc += 1\n\t\t\tif l[i] == 2 * x:\n\t\t\t\tans += c\n\t\tprint(ans)\nelse:\n\tprint(0)\n", "n = int(input())\na = list(map(int, input().split()))\n(Sum, cnt, s) = (n * [0], n * [0], a[0])\nSum[0] = a[0]\nfor i in range(1, n):\n\tSum[i] = Sum[i - 1] + a[i]\n\ts += a[i]\nss = 0\nfor i in range(n - 1, -1, -1):\n\tss += a[i]\n\tif ss == s / 3:\n\t\tcnt[i] = 1\nfor i in range(n - 2, -1, -1):\n\tcnt[i] += cnt[i + 1]\nans = 0\nfor i in range(n - 2):\n\tif Sum[i] == s / 3:\n\t\tans += cnt[i + 2]\nif s % 3 != 0:\n\tans = 0\nprint(ans)\n", "n = int(input())\na = [int(x) for x in input().split()]\nthird_portion = [0] * (n + 1)\ns = sum(a)\nno_of_ways = 0\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tone_portion = s // 3\n\ts = 0\n\ti = n - 1\n\twhile i >= 0:\n\t\ts += a[i]\n\t\tif s == one_portion:\n\t\t\tthird_portion[i] = 1\n\t\ti -= 1\n\ti = n - 2\n\twhile i >= 0:\n\t\tthird_portion[i] += third_portion[i + 1]\n\t\ti -= 1\n\ts = 0\n\tfor i in range(n - 2):\n\t\ts += a[i]\n\t\tif s == one_portion:\n\t\t\tno_of_ways += third_portion[i + 2]\n\tprint(no_of_ways)\n", "n = int(input(''))\nnums = list(map(int, input('').split(' ')))\ns = sum(nums)\nresult = 0\nif s % 3 == 0:\n\ts = s / 3\n\tt = s * 2\n\tfirst = 0\n\tprefix = 0\n\tfor i in nums[:-1]:\n\t\tprefix += i\n\t\tif prefix == t:\n\t\t\tresult += first\n\t\tif prefix == s:\n\t\t\tfirst += 1\n\tprint(result)\nelse:\n\tprint(0)\n", "n = int(input())\na = list(input().split(' '))\nfor i in range(0, n):\n\ta[i] = int(a[i])\nx = sum(a)\nc = []\ny = x / 3\nres = 0\nif y != x // 3:\n\tprint(0)\nelse:\n\ts1 = []\n\ts2 = []\n\ts3 = []\n\tans = 0\n\tfor i in range(0, n):\n\t\tans += a[i]\n\t\ts1.append(ans)\n\tans = 0\n\tfor j in range(0, n):\n\t\tans += a[n - j - 1]\n\t\ts2.append(ans)\n\tans = 0\n\tfor i in range(0, n):\n\t\tif s2[i] == y:\n\t\t\tans += 1\n\t\ts3.append(ans)\n\tfor i in range(0, n):\n\t\tif s1[i] == y:\n\t\t\tif n - i - 3 >= 0:\n\t\t\t\tres += s3[n - i - 3]\n\tprint(res)\n", "n = int(input())\nnums = [int(x) for x in input().split()]\ns = sum(nums)\nif s % 3 != 0:\n\tprint(0)\nelse:\n\ts = int(s / 3)\n\tres = 0\n\tpos = []\n\ttmp = 0\n\tfor i in range(0, n):\n\t\tpos.append(0)\n\tfor i in range(n - 1, -1, -1):\n\t\ttmp += nums[i]\n\t\tif tmp == s:\n\t\t\tpos[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tpos[i] += pos[i + 1]\n\ttmp = 0\n\tfor i in range(0, n - 2):\n\t\ttmp += nums[i]\n\t\tif tmp == s:\n\t\t\tres += pos[i + 2]\n\tprint(res)\n", "import sys\ninput = sys.stdin.readline\nimport math as mt\nimport collections as cc\n\ndef ncr(n, r):\n\timport math as mt\n\treturn mt.factorial(n) // (mt.factorial(n - r) * mt.factorial(r))\nI = lambda : list(map(int, input().split()))\n(n,) = I()\nl = I()\nsu = sum(l)\nif su % 3:\n\tprint(0)\nelif n < 3:\n\tprint(0)\nelse:\n\ta = su // 3\n\tb = a * 2\n\tans = 0\n\tcount = 0\n\tfor i in range(1, n):\n\t\tl[i] += l[i - 1]\n\tfor i in range(n - 1):\n\t\tif l[i] * 3 == su * 2:\n\t\t\tans += count\n\t\tif l[i] * 3 == su:\n\t\t\tcount += 1\n\tprint(ans)\n", "def solv():\n\tn = int(input())\n\ta = [int(x) for x in input().split(' ')]\n\ts = sum(a)\n\tif s % 3 == 0:\n\t\t(cnt, ans) = (0, 0)\n\t\tcurr_sum = 0\n\t\tfor i in range(len(a) - 1):\n\t\t\tcurr_sum += a[i]\n\t\t\tif curr_sum == 2 * s // 3:\n\t\t\t\tans += cnt\n\t\t\tif curr_sum == s // 3:\n\t\t\t\tcnt += 1\n\t\tprint(ans)\n\telse:\n\t\tprint(0)\nsolv()\n", "n = int(input())\na = list(map(int, input().split()))\ns = [a[0]] * n\nfor i in range(1, n):\n\ts[i] = s[i - 1] + a[i]\nif s[n - 1] % 3 != 0:\n\tprint(0)\nelse:\n\ts1 = s[n - 1] // 3\n\ts2 = s1 * 2\n\tans = t = 0\n\tfor i in s[:-1]:\n\t\tif i == s2:\n\t\t\tans += t\n\t\tif i == s1:\n\t\t\tt += 1\n\tprint(ans)\n", "import sys\nfrom functools import lru_cache, cmp_to_key\nfrom heapq import merge, heapify, heappop, heappush, nlargest, nsmallest, _heapify_max, _heapreplace_max\nfrom math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log\nfrom collections import defaultdict as dd, deque, Counter as c\nfrom itertools import combinations as comb, permutations as perm\nfrom bisect import bisect_left as bl, bisect_right as br, bisect\nfrom fractions import Fraction\nmod = pow(10, 9) + 7\nmod2 = 998244353\n\ndef data():\n\treturn sys.stdin.readline().strip()\n\ndef out(*var, end='\\n'):\n\tsys.stdout.write(' '.join(map(str, var)) + end)\n\ndef l():\n\treturn list(sp())\n\ndef sl():\n\treturn list(ssp())\n\ndef sp():\n\treturn map(int, data().split())\n\ndef ssp():\n\treturn map(str, data().split())\n\ndef l1d(n, val=0):\n\treturn [val for i in range(n)]\n\ndef l2d(n, m, val=0):\n\treturn [l1d(n, val) for j in range(m)]\nn = int(data())\narr = l()\nif sum(arr) % 3 != 0:\n\tout(0)\n\texit()\ns = sum(arr) // 3\nsuf = [0] * n\ntemp = 0\nfor i in range(n - 1, -1, -1):\n\ttemp += arr[i]\n\tif temp == s:\n\t\tsuf[i] = 1\nfor i in range(n - 2, -1, -1):\n\tsuf[i] += suf[i + 1]\n(answer, temp) = (0, 0)\nfor i in range(n - 2):\n\ttemp += arr[i]\n\tif temp == s:\n\t\tanswer += suf[i + 2]\nout(answer)\n", "import bisect\nn = int(input())\na = input().split()\ns = 0\nanswer = 0\nfor i in range(n):\n\ta[i] = int(a[i])\n\ts += a[i]\nif s % 3 != 0:\n\tprint(0)\n\texit()\nfirst_pack = []\nm = 0\nfor i in range(n - 2):\n\tm += a[i]\n\tif m == s // 3:\n\t\tfirst_pack.append(i)\nif len(first_pack) == 0:\n\tprint(0)\n\texit()\nk = len(first_pack)\nm = 0\nfor i in range(first_pack[0] + 1, n - 1):\n\tm += a[i]\n\tif m == s // 3:\n\t\tanswer += bisect.bisect_left(first_pack, i)\nprint(answer)\n", "n = int(input())\nl = list(map(int, input().split()))\np = sum(l)\nk = 0\nif p % 3 == 0:\n\ty = p // 3\n\tr = p - y\n\tz = 0\n\tt = 0\n\tfor i in range(n - 1):\n\t\tz = z + l[i]\n\t\tif z == r:\n\t\t\tk = k + t\n\t\tif z == y:\n\t\t\tt = t + 1\n\tprint(k)\nelse:\n\tprint(0)\n", "n = int(input())\nnums = list(map(int, input().split()))\ntotal = sum(nums)\nif total % 3 != 0:\n\tprint('0')\n\texit()\nportion = total // 3\npart_sum = 0\nways = 0\ntotal_ways = 0\nfor i in range(n - 1):\n\tpart_sum += nums[i]\n\tif part_sum == 2 * portion:\n\t\ttotal_ways += ways\n\tif part_sum == portion:\n\t\tways += 1\nprint(total_ways)\n", "n = int(input(''))\narr = list(map(int, input().split()))\ns = sum(arr)\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tdp = [0] * n\n\tdp[0] = arr[0]\n\tfor i in range(1, n):\n\t\tdp[i] = dp[i - 1] + arr[i]\n\tif arr[n - 1]:\n\t\tsol = 0\n\t\tcnt = 0\n\t\ts = dp[n - 1]\n\t\tfor i in range(n):\n\t\t\tif dp[i] == 2 * s // 3:\n\t\t\t\tsol += cnt\n\t\t\tif dp[i] == s // 3:\n\t\t\t\tcnt += 1\n\t\tprint(sol)\n\telse:\n\t\tcnt = dp.count(0)\n\t\tsol = (cnt - 1) * (cnt - 2) // 2\n\t\tprint(sol)\n", "def getInts():\n\treturn [int(s) for s in input().split()]\n\ndef getInt():\n\treturn int(input())\n\ndef getStrs():\n\treturn [s for s in input().split()]\n\ndef getStr():\n\treturn input()\n\ndef listStr():\n\treturn list(input())\n\ndef solve():\n\tN = getInt()\n\tA = getInts()\n\ttarg = sum(A)\n\tif targ % 3 > 0:\n\t\treturn 0\n\tcurr_sum = 0\n\ti = 0\n\ti_options = []\n\tj_options = []\n\twhile i < N:\n\t\tcurr_sum += A[i]\n\t\tif curr_sum == targ // 3 and i < N - 2:\n\t\t\ti_options.append(i)\n\t\tif curr_sum == 2 * targ // 3 and i < N - 1:\n\t\t\tj_options.append(i)\n\t\ti += 1\n\tj_greater = []\n\tcurr_num = 0\n\ti = N - 1\n\twhile i >= 0:\n\t\tif j_options and j_options[-1] == i:\n\t\t\tcurr_num += 1\n\t\t\tj_options.pop()\n\t\tj_greater.append(curr_num)\n\t\ti -= 1\n\tj_greater.reverse()\n\tans = 0\n\tfor ii in i_options:\n\t\tif ii < N - 1:\n\t\t\tans += j_greater[ii + 1]\n\treturn ans\nans = solve()\nprint(ans)\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tr = [1 * int(a[-1] == s // 3)]\n\te = a[-1]\n\tfor i in range(n - 2, -1, -1):\n\t\te += a[i]\n\t\tif e == s // 3:\n\t\t\tr.append(r[-1] + 1)\n\t\telse:\n\t\t\tr.append(r[-1])\n\tanswer = 0\n\te = 0\n\tr = r[::-1]\n\tfor i in range(n - 2):\n\t\te += a[i]\n\t\tif e == s // 3:\n\t\t\tanswer += r[i + 2]\n\tprint(answer)\n", "n = int(input())\nl = list(map(int, input().rstrip().split()))\nfor i in range(1, n):\n\tl[i] = l[i] + l[i - 1]\ncount = 0\na = 0\nfor i in range(n - 1):\n\tif l[i] * 3 == l[n - 1] * 2:\n\t\ta += count\n\tif l[i] * 3 == l[n - 1]:\n\t\tcount += 1\nprint(a)\n", "from sys import stdin, stdout\nfrom collections import Counter\nfrom math import ceil\nfrom bisect import bisect_left\nfrom bisect import bisect_right\n\ndef ai():\n\treturn list(map(int, stdin.readline().split()))\n\ndef ei():\n\treturn map(int, stdin.readline().split())\n\ndef ip():\n\treturn int(stdin.readline().strip())\n\ndef op(ans):\n\treturn stdout.write(str(ans) + '\\n')\nn = ip()\nli = ai()\ns = sum(li)\nif s % 3:\n\texit(print(0))\nans = coutn = 0\nfor i in range(1, n - 1):\n\tli[i] += li[i - 1]\nf = s // 3\ns = s - f\nfor i in li[:n - 1]:\n\tif i == s:\n\t\tans += coutn\n\tif i == f:\n\t\tcoutn += 1\nprint(ans)\n", "import sys\nfrom collections import defaultdict as dd\nfrom collections import Counter as cc\nfrom queue import Queue\nimport math\nimport itertools\ntry:\n\tsys.stdin = open('input.txt', 'r')\n\tsys.stdout = open('output.txt', 'w')\nexcept:\n\tpass\ninput = lambda : sys.stdin.buffer.readline().rstrip()\nq = int(input())\nw = list(map(int, input().split()))\ne = sum(w)\nif e % 3 != 0 or q < 3:\n\tprint(0)\n\texit()\na = 0\nb = 0\nc = 0\nr = e // 3\nif r != 0:\n\tfor i in range(q):\n\t\tc += w[i]\n\t\tif c == r:\n\t\t\ta += 1\n\t\tif c == 2 * r:\n\t\t\tb += a\n\tprint(b)\nelse:\n\tfor i in range(q):\n\t\tc += w[i]\n\t\tif c == r:\n\t\t\ta += 1\n\tprint((a - 1) * (a - 2) // 2)\n", "def solve(ns):\n\tn = len(ns)\n\ts = sum(ns)\n\tif s % 3 != 0:\n\t\treturn 0\n\ts /= 3\n\tps = ns[:]\n\tfor i in range(1, n):\n\t\tps[i] += ps[i - 1]\n\n\tdef segsum(a, b):\n\t\treturn ps[b] - (0 if a == 0 else ps[a - 1])\n\tls = [0 for i in range(n)]\n\trs = [0 for i in range(n)]\n\tfor i in range(n):\n\t\tif segsum(0, i) == s:\n\t\t\tls[i] += 1\n\t\tif i > 0:\n\t\t\tls[i] += ls[i - 1]\n\tfor i in range(n - 1, -1, -1):\n\t\tif segsum(i, n - 1) == s:\n\t\t\trs[i] += 1\n\t\tif i < n - 1:\n\t\t\trs[i] += rs[i + 1]\n\tres = 0\n\tfor i in range(n - 2):\n\t\tif segsum(0, i) == s:\n\t\t\tres += rs[i + 2]\n\treturn res\nassert solve([1, 2, 3, 0, 3]) == 2\nassert solve([0, 1, -1, 0]) == 1\nassert solve([4, 1]) == 0\nassert solve([1, 1]) == 0\nn = int(input())\nns = list(map(int, input().split()))\nprint(solve(ns))\n", "k = int(input())\narr = list(map(int, input().strip().split()))[:k]\nif sum(arr) % 3 == 0:\n\tsums = sum(arr) // 3\nelse:\n\tprint(0)\n\texit(0)\nsum2 = sums * 2\ncount = 0\nans = 0\nc1 = 0\nfor i in range(k - 1):\n\tcount += arr[i]\n\tif count == sum2:\n\t\tans += c1\n\tif count == sums:\n\t\tc1 += 1\nprint(ans)\n", "import math\nimport collections\nimport bisect\nimport heapq\nimport time\nimport random\nimport itertools\nimport sys\nN = int(input())\nA = [int(x) for x in input().split()]\npresum = [0] * (N + 1)\nfor i in range(1, N + 1):\n\tpresum[i] = presum[i - 1] + A[i - 1]\nif presum[-1] % 3 != 0:\n\tprint(0)\n\texit(0)\na = presum[-1] // 3\nb = presum[-1] * 2 // 3\nc = presum[-1]\nidx = collections.defaultdict(list)\nfor i in range(1, N + 1):\n\tif presum[i] == a:\n\t\tidx[a].append(i)\n\telif presum[i] == b:\n\t\tidx[b].append(i)\n\telif presum[i] == c:\n\t\tidx[c].append(i)\nans = 0\nfor i in idx[a]:\n\tl = bisect.bisect_right(idx[b], i)\n\tr = bisect.bisect_left(idx[b], N)\n\tans += max(0, r - l)\nprint(ans)\n", "from collections import defaultdict\nn = int(input())\ninp = input().split()\ninp = list(map(lambda x: int(x), inp))\ntotal = sum(inp)\ncnt = defaultdict(int)\nres = count = right_sum = left_sum = 0\nif total % 3 == 0:\n\tfor i in range(len(inp) - 1, -1, -1):\n\t\tright_sum += inp[i]\n\t\tif right_sum == total / 3:\n\t\t\tcount += 1\n\t\tcnt[i] = count\n\tfor i in range(len(inp) - 2):\n\t\tleft_sum += inp[i]\n\t\tif left_sum == total / 3:\n\t\t\tres += cnt[i + 2]\nprint(res)\n", "n = int(input())\nli = list(map(int, input().split(' ', n)[:n]))\nsu = 0\nfor i in range(n):\n\tsu += li[i]\nif su % 3 != 0:\n\tprint(0)\nelse:\n\tsu //= 3\n\tcnt = [0] * n\n\tss = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tss += li[i]\n\t\tif ss == su:\n\t\t\tcnt[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tcnt[i] += cnt[i + 1]\n\tans = 0\n\tss = 0\n\tfor i in range(0, n - 2):\n\t\tss += li[i]\n\t\tif ss == su:\n\t\t\tans += cnt[i + 2]\n\tprint(ans)\n", "n = int(input())\na = [int(i) for i in input().split(' ')]\n\ndef gao():\n\ta_sum = sum(a)\n\tif a_sum % 3 != 0:\n\t\tprint('0')\n\t\treturn\n\tpiece = a_sum // 3\n\tright = set()\n\tright_sum = 0\n\tfor i in range(n - 1, 0, -1):\n\t\tright_sum += a[i]\n\t\tif right_sum == piece:\n\t\t\tright.add(i)\n\tleft_sum = 0\n\tans = 0\n\tfor i in range(n):\n\t\tleft_sum += a[i]\n\t\tif i + 1 in right:\n\t\t\tright.remove(i + 1)\n\t\tif left_sum == piece:\n\t\t\tans += len(right)\n\tprint(ans)\ngao()\n", "n = input()\nn = int(n.strip())\nsez = input()\nsez = list(map(int, sez.strip().split(' ')))\nvsota = sum(sez)\ntretjina = vsota / 3\nlevi_indeksi = []\nvsota_leva = sez[0]\nfor i in range(1, len(sez)):\n\tif vsota_leva == tretjina:\n\t\tlevi_indeksi.append(i)\n\tvsota_leva += sez[i]\ndesni_indeksi = []\nvsota_desna = sez[-1]\nfor i in range(len(sez) - 2, -1, -1):\n\tif vsota_desna == tretjina:\n\t\tdesni_indeksi.append(i)\n\tvsota_desna += sez[i]\ndesni_indeksi.sort()\nlevi_indeksi.sort()\nd = len(desni_indeksi)\nvseh = 0\nj = 0\nfor i in levi_indeksi:\n\twhile j < len(desni_indeksi) and desni_indeksi[j] < i:\n\t\tj = j + 1\n\t\td -= 1\n\tif j == len(desni_indeksi):\n\t\tbreak\n\tvseh += d\nprint(vseh)\n", "input()\nnums = list(map(int, input().split()))\nsum_n = sum(nums)\nans = current_sum = one_third = 0\nfor x in nums[:-1]:\n\tcurrent_sum += x\n\tif current_sum == 2 / 3 * sum_n:\n\t\tans += one_third\n\tif current_sum == 1 / 3 * sum_n:\n\t\tone_third += 1\nprint(ans)\n", "n = int(input())\nl = list(map(int, input().split()))\ns = sum(l)\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tt = 0\n\tcnt = 0\n\tres = 0\n\tfor i in range(n - 1):\n\t\tt += l[i]\n\t\tif t == 2 * s // 3:\n\t\t\tres += cnt\n\t\tif t == s // 3:\n\t\t\tcnt += 1\n\tprint(res)\n", "from sys import stdin, stdout, setrecursionlimit\nfrom functools import lru_cache, cmp_to_key\nfrom heapq import merge, heapify, heappop, heappush\nfrom math import *\nfrom collections import defaultdict as dd, deque, Counter as C\nfrom itertools import combinations as comb, permutations as perm, accumulate\nfrom bisect import bisect_left as bl, bisect_right as br, bisect\nfrom time import perf_counter\nfrom fractions import Fraction\nimport copy\nimport time\nstarttime = time.time()\nmod = int(pow(10, 9) + 7)\nmod2 = 998244353\n\ndef data():\n\treturn input()\n\ndef num():\n\treturn int(input())\n\ndef L():\n\treturn list(sp())\n\ndef sl():\n\treturn list(ssp())\n\ndef sp():\n\treturn map(int, data().split())\n\ndef ssp():\n\treturn map(str, data().split())\n\ndef l1d(n, val=0):\n\treturn [val for i in range(n)]\n\ndef l2d(n, m, val=0):\n\treturn [l1d(n, val) for j in range(m)]\n\ndef pmat(A):\n\tfor ele in A:\n\t\tprint(*ele, end='\\n')\n\ndef seive():\n\tprime = [1 for i in range(10 ** 6 + 1)]\n\tprime[0] = 0\n\tprime[1] = 0\n\tfor i in range(10 ** 6 + 1):\n\t\tif prime[i]:\n\t\t\tfor j in range(2 * i, 10 ** 6 + 1, i):\n\t\t\t\tprime[j] = 0\n\treturn prime\nn = int(data())\na = L()\ntotal = sum(a)\nans = 0\nif total % 3 != 0:\n\tprint(0)\nelse:\n\tot = total // 3\n\tst = ot * 2\n\t(c, s) = (0, 0)\n\tfor i in range(n - 1):\n\t\ts += a[i]\n\t\tif s == st:\n\t\t\tans += c\n\t\tif s == ot:\n\t\t\tc += 1\n\tprint(ans)\nendtime = time.time()\n", "n = int(input())\na = [int(x) for x in input().split()]\ns = sum(a)\ns1 = [0 for i in range(n)]\ns2 = [0 for i in range(n + 1)]\nsum1 = 0\nsum2 = 0\nm = 0\nif s // 3 * 3 != s:\n\tprint(0)\nelse:\n\tfor i in range(0, n - 2):\n\t\tsum1 += a[i]\n\t\tif sum1 == s // 3:\n\t\t\ts1[i] = 1\n\tfor j in range(n - 1, 1, -1):\n\t\tsum2 += a[j]\n\t\tif sum2 == s // 3:\n\t\t\ts2[j] = s2[j + 1] + 1\n\t\telse:\n\t\t\ts2[j] = s2[j + 1]\n\tfor k in range(0, n - 2):\n\t\tm += s1[k] * s2[k + 2]\n\tprint(m)\n", "n = int(input())\nk = list(map(int, input().split()))\nfor i in range(1, n):\n\tk[i] += k[i - 1]\nc = 0\nans = 0\nif k[-1] % 3 != 0:\n\tprint(0)\nelse:\n\tfor i in range(n - 1):\n\t\tif 3 * k[i] == 2 * k[-1]:\n\t\t\tans += c\n\t\tif 3 * k[i] == k[-1]:\n\t\t\tc += 1\n\tprint(ans)\n", "n = int(input())\nls = list(map(int, input().split()))\ns = sum(ls)\nans = 0\not = int(s / 3)\notc = 0\ntt = ot * 2\np = 0\nif s % 3 == 0:\n\tfor i in range(n - 1):\n\t\tp = p + ls[i]\n\t\tif p == tt:\n\t\t\tans = ans + otc\n\t\tif p == ot:\n\t\t\totc = otc + 1\n\tprint(ans)\nelse:\n\tprint(0)\n", "import sys\nimport math\nfrom collections import defaultdict\nimport json\nfrom bisect import bisect\n\ndef bin_search(e, a):\n\n\tdef _search(e, a, lo, hi):\n\t\tp = (lo + hi) // 2\n\t\tif lo == hi:\n\t\t\tif a[p] <= e:\n\t\t\t\treturn p\n\t\t\telse:\n\t\t\t\treturn p + 1\n\t\tif e < a[p]:\n\t\t\treturn _search(e, a, p + 1, hi)\n\t\telse:\n\t\t\treturn _search(e, a, lo, p)\n\tif not a:\n\t\treturn 0\n\treturn _search(e, a, 0, len(a) - 1)\n\ndef main():\n\tn = int(sys.stdin.readline().strip())\n\ta = list(map(int, sys.stdin.readline().strip().split(' ')))\n\ttrail = defaultdict(lambda : [])\n\ts = 0\n\tfor (i, e) in enumerate(reversed(a)):\n\t\ts += e\n\t\ttrail[s].append(n - i - 1)\n\ttotal = s\n\ts = 0\n\tcnt = 0\n\tprev_pos = len(a) - 1\n\tfor (i, e) in enumerate(a):\n\t\ts += e\n\t\tif e == 0:\n\t\t\tif prev_pos == 0:\n\t\t\t\tcontinue\n\t\t\telif len(a) - prev_pos > i + 1:\n\t\t\t\tcnt += prev_pos\n\t\t\t\tcontinue\n\t\tprev_pos = len(a) - 1\n\t\tcands = trail[s]\n\t\tif not cands:\n\t\t\tcontinue\n\t\tif not total == 3 * s:\n\t\t\tcontinue\n\t\tprev_pos = bin_search(i + 1, cands)\n\t\tcnt += prev_pos\n\tsys.stdout.write(str(cnt) + '\\n')\nmain()\n", "(n, a) = (int(input()), list(map(int, input().split())))\n(sums, s) = ([[0, 0]] + [0] * n, sum(a))\nfor i in range(n):\n\tsums[i + 1] = [sums[i][0] + a[i], sums[i][1] + int(i != n - 1 and (sums[i][0] + a[i]) * 3 == s * 2)]\nprint(0 if s % 3 else sum((sums[-1][1] - sums[i][1] for i in range(1, n) if sums[i][0] * 3 == s)))\n", "def solve():\n\tn = int(input())\n\tarr = list(map(int, input().split()))\n\ts = sum(arr)\n\tif s % 3:\n\t\treturn 0\n\ttrg = s // 3\n\tnp = 0\n\tps = []\n\ttmp = 0\n\tfor i in range(n - 2):\n\t\ttmp += arr[i]\n\t\tif tmp == trg:\n\t\t\tnp += 1\n\t\t\tps.append(i)\n\tans = 0\n\ttmp = 0\n\tfor i in range(n - 1, 1, -1):\n\t\ttmp += arr[i]\n\t\tif tmp == trg:\n\t\t\tans += np\n\t\tif np and ps[-1] == i - 2:\n\t\t\tps.pop()\n\t\t\tnp -= 1\n\treturn ans\nprint(solve())\n", "n = int(input())\na = list(map(int, input().split()))\no = 0\ns = 0\nans = 0\nf = 0\np = 0\nk = sum(a) // 3\nif sum(a) % 3 != 0:\n\tprint('0')\n\tf = 1\nelse:\n\tfor i in a[:-1]:\n\t\ts += i\n\t\tif s == k * 2:\n\t\t\tans += o\n\t\tif s == k:\n\t\t\to += 1\nif f != 1:\n\tprint(ans)\n", "n = int(input())\narr = [int(i) for i in input().split()]\ns = sum(arr)\nif s % 3 == 0:\n\tcnt = [0] * n\n\ttarget = s // 3\n\ttemp = 0\n\tfor i in range(n - 1, -1, -1):\n\t\ttemp += arr[i]\n\t\tif temp == target:\n\t\t\tcnt[i] = 1\n\tsum_arr = [0] * n\n\tfor i in range(n - 1, -1, -1):\n\t\tif i == n - 1:\n\t\t\tsum_arr[i] = cnt[i]\n\t\telse:\n\t\t\tsum_arr[i] = sum_arr[i + 1] + cnt[i]\n\ttemp = 0\n\tans = 0\n\tfor i in range(n - 2):\n\t\ttemp += arr[i]\n\t\tif temp == target:\n\t\t\tans += sum_arr[i + 2]\n\tprint(ans)\nelse:\n\tprint(0)\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\nif s % 3 != 0:\n\tprint('0')\nelse:\n\ts //= 3\n\t(sums, k, ans) = (0, 0, 0)\n\ta.pop()\n\tfor x in a:\n\t\tsums += x\n\t\tif sums == 2 * s:\n\t\t\tans += k\n\t\tif sums == s:\n\t\t\tk += 1\n\tprint(ans)\n", "import itertools\nn = int(input())\nb = list(itertools.accumulate([int(i) for i in input().split()]))\nans = 0\nif not b[n - 1] % 3:\n\tx = b[n - 1] // 3\n\ty = 2 * x\n\tif not x:\n\t\tt = b.count(0)\n\t\tans = (t - 2) * (t - 1) // 2\n\telse:\n\t\ttemp = 0\n\t\tfor i in range(n - 1):\n\t\t\tif b[i] == x:\n\t\t\t\ttemp += 1\n\t\t\telif b[i] == y:\n\t\t\t\tans += temp\nprint(ans)\n", "n = int(input())\narr = [int(i) for i in input().split()]\npre = []\nsuff = []\ns = sum(arr)\nif s % 3 != 0:\n\tprint(0)\nelse:\n\ts = s // 3\n\tl = 0\n\tc = 0\n\tans = 0\n\tfor i in range(n):\n\t\tl += arr[i]\n\t\tif l == s:\n\t\t\tpre.append(1)\n\t\telse:\n\t\t\tpre.append(0)\n\tl = 0\n\tc = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tl += arr[i]\n\t\tif l == s:\n\t\t\tc += 1\n\t\tsuff.append(c)\n\tsuff.reverse()\n\tfor i in range(n - 2):\n\t\tif pre[i] > 0:\n\t\t\tans += pre[i] * suff[i + 2]\n\tprint(ans)\n", "n = int(input())\n(res, summ, s) = (0, 0, 0)\na = [int(x) for x in input().split()]\nsumm = sum(a)\nif summ % 3 != 0:\n\tprint(0)\n\texit(0)\narr = [0] * (n + 1)\nfor i in range(n - 1, -1, -1):\n\ts += a[i]\n\tif s == summ // 3:\n\t\tarr[i] = arr[i + 1] + 1\n\telse:\n\t\tarr[i] = arr[i + 1]\ns = 0\nfor i in range(n - 2):\n\ts += a[i]\n\tif s == summ // 3:\n\t\tres += arr[i + 2]\nprint(res)\n", "N = int(input())\nnums = list(map(int, input().split()))\ncount = 0\nt = sum(nums)\nif t % 3 != 0:\n\tprint(0)\nelse:\n\ttarget = t // 3\n\tcount = 0\n\tmark1 = list()\n\ttemp = 0\n\tfor (index, num) in enumerate(nums):\n\t\ttemp += num\n\t\tif temp == target:\n\t\t\tmark1.append(index)\n\tmark2 = list()\n\ttemp = 0\n\tfor index in range(N - 1, -1, -1):\n\t\ttemp += nums[index]\n\t\tif temp == target and len(mark1) > 0:\n\t\t\tif index > 1 + mark1[-1]:\n\t\t\t\tcount += len(mark1)\n\t\t\telse:\n\t\t\t\twhile len(mark1) > 0 and index <= mark1[-1] + 1:\n\t\t\t\t\tmark1.pop()\n\t\t\t\tif temp == target and len(mark1) > 0:\n\t\t\t\t\tif index > 1 + mark1[-1]:\n\t\t\t\t\t\tcount += len(mark1)\n\tprint(count)\n", "n = int(input())\na = [int(x) for x in input().split()]\ns = sum(a)\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tb = [a[0]]\n\ts = s // 3\n\tfor i in range(1, n):\n\t\tb.append(b[i - 1] + a[i])\n\tif s == 0:\n\t\tm = b.count(0)\n\t\tprint((m - 1) * (m - 2) // 2 if m > 2 else 0)\n\telse:\n\t\tt = 0\n\t\tc = [0]\n\t\tfor i in range(n - 1, -1, -1):\n\t\t\tif b[i] == s * 2:\n\t\t\t\tt += 1\n\t\t\telif b[i] == s:\n\t\t\t\tc.append(t + c[-1])\n\t\t\t\tt = 0\n\t\tprint(sum(c))\n", "r = input\nn = int(r())\nN = -10 ** 9 - 7\narr = [int(x) for x in r().split()]\nSum = [0]\ncnt = 0\nfor i in arr:\n\tSum.append(Sum[-1] + i)\ncnt = [0] * (n + 1)\nsuf = [0] * (n + 1)\nfor i in range(n, 0, -1):\n\tif (Sum[-1] - Sum[i - 1]) * 3 == Sum[-1]:\n\t\tcnt[i] = 1\n\tif i == n:\n\t\tsuf[i] = cnt[i]\n\telse:\n\t\tsuf[i] = cnt[i] + suf[i + 1]\nans = 0\nfor i in range(1, n - 1, 1):\n\tif Sum[i] * 3 == Sum[-1]:\n\t\tans += suf[i + 2]\nprint(ans)\n", "n = int(input())\nlist1 = list(map(int, input().split()))\ns = sum(list1)\nif s % 3 == 0:\n\tw = 0\n\ts = s / 3\n\tt = s * 2\n\t(u, g) = (0, 0)\n\tfor x in list1[:-1]:\n\t\tu += x\n\t\tif u == t:\n\t\t\tw += g\n\t\tif u == s:\n\t\t\tg += 1\n\tprint(w)\nelse:\n\tprint(0)\n", "n = int(input())\nl = list(map(int, input().split()))\nlu = l[:]\nfor i in range(1, n):\n\tl[i] = l[i] + l[i - 1]\nans = 0\ns = 0\nco = 0\nif l[-1] % 3 == 0:\n\tfor i in lu[:-1]:\n\t\ts += i\n\t\tif s == l[-1] // 3 * 2:\n\t\t\tans += co\n\t\tif s == l[-1] // 3:\n\t\t\tco += 1\n\tprint(ans)\nelse:\n\tprint(0)\n", "from math import *\nsu = 0\nls = []\nn = int(input())\nls = [int(x) for x in input().split()]\npre = [0 for i in range(n + 1)]\nfor i in range(n):\n\tpre[i + 1] = pre[i] + ls[i]\nx = sum(ls)\nres = x // 3\nres2 = 2 * res\nans = 0\ncnt = 0\nfor i in range(1, n):\n\tif pre[i] == res2:\n\t\tans += cnt\n\tif pre[i] == res:\n\t\tcnt += 1\nif x % 3 != 0:\n\tprint(0)\nelse:\n\tprint(ans)\n", "n = int(input())\nentrada = input().split()\nentrada = [int(i) for i in entrada]\nsoma = 0\nfor i in range(0, n):\n\tsoma += entrada[i]\nif soma % 3 != 0:\n\tprint(0)\nelse:\n\tbuffer = 0\n\tparte = soma // 3\n\tposicao = [0] * 500100\n\ti = n - 1\n\twhile i >= 0:\n\t\tbuffer += entrada[i]\n\t\tif buffer == parte:\n\t\t\tposicao[i] += 1\n\t\ti -= 1\n\ti = n - 2\n\twhile i >= 0:\n\t\tposicao[i] += posicao[i + 1]\n\t\ti -= 1\n\tbuffer = 0\n\tresult = 0\n\ti = 0\n\twhile i + 2 < n:\n\t\tbuffer += entrada[i]\n\t\tif buffer == parte:\n\t\t\tresult += posicao[i + 2]\n\t\ti += 1\n\tprint(result)\n", "n = int(input())\nA = [int(x) for x in input().split()]\nB = [0] * n\nS = 0\nSA = sum(A)\nCnt = 0\nTot = 0\nfor i in range(n - 1):\n\tS += A[i]\n\tif 3 * S == 2 * SA:\n\t\tTot += Cnt\n\tif 3 * S == SA:\n\t\tCnt += 1\nprint(Tot)\n", "import bisect\ninput()\nls = list(map(int, input().split()))\ncum = [0] * len(ls)\ncum[0] = ls[0]\nfor i in range(1, len(cum)):\n\tcum[i] = ls[i] + cum[i - 1]\n\ndef f(arr, a, b):\n\tif a == 0:\n\t\treturn arr[b]\n\telse:\n\t\treturn arr[b] - arr[a - 1]\nc = 0\ns = sum(ls)\nif not s % 3:\n\tbtm = []\n\ttop = []\n\ttg = s // 3\n\tfor i in range(len(cum) - 1):\n\t\tif cum[i] == tg:\n\t\t\tbtm.append(i)\n\t\tif cum[i] == 2 * tg:\n\t\t\ttop.append(i)\n\tfor ind in btm:\n\t\tnum = bisect.bisect_right(top, ind)\n\t\tc += len(top) - num\nprint(c)\n", "from bisect import bisect_right\nn = int(input())\na = [int(i) for i in input().split()]\nprefix = [0] * (n + 1)\nprefix[1] = a[0]\nfor i in range(2, n + 1):\n\tprefix[i] = prefix[i - 1] + a[i - 1]\nneed = prefix[-1] // 3\nans = 0\nif need * 3 == prefix[-1]:\n\tgood_pref1 = []\n\tgood_pref2 = []\n\tfor i in range(1, n):\n\t\tif prefix[i] == need:\n\t\t\tgood_pref1.append(i)\n\t\tif prefix[i] == 2 * need:\n\t\t\tgood_pref2.append(i)\n\tl = len(good_pref2)\n\tfor i in good_pref1:\n\t\tans += l - bisect_right(good_pref2, i)\nprint(ans)\n", "n = int(input())\narr = list(map(int, input().split()))\nprefix_sum = [0] * n\nprefix_sum[0] = arr[0]\nfor i in range(1, n):\n\tprefix_sum[i] = prefix_sum[i - 1] + arr[i]\nif prefix_sum[-1] % 3 != 0:\n\tprint(0)\nelse:\n\tanswer = 0\n\tpart = prefix_sum[-1] // 3\n\ts2_right = [0] * n\n\tfor i in range(n - 2, 0, -1):\n\t\ts2_right[i] = s2_right[i + 1]\n\t\tif prefix_sum[i] == part * 2:\n\t\t\ts2_right[i] += 1\n\tfor i in range(n - 1):\n\t\tif prefix_sum[i] == part:\n\t\t\tanswer += s2_right[i + 1]\n\tprint(answer)\n", "n = int(input())\na = input().split()\nfor i in range(n):\n\ta[i] = int(a[i])\n\tif i > 0:\n\t\ta[i] += a[i - 1]\n(x, ans) = (0, 0)\nfor i in range(n - 1):\n\tif a[i] * 3 == a[n - 1] * 2:\n\t\tans += x\n\tif a[i] * 3 == a[n - 1]:\n\t\tx += 1\nprint(ans)\n", "import sys\nn = int(input())\na = [int(i) for i in input().split()]\nsun = (n + 2) * [0]\nsun[0] = 0\nfor i in range(n):\n\tsun[i + 1] = sun[i] + a[i]\nif sun[n] % 3 != 0:\n\tprint(0)\nelse:\n\ts = sun[n] / 3\n\ts2 = 2 * s\n\tcnt = 0\n\tans = 0\n\tfor i in range(1, n):\n\t\tif sun[i] == s2:\n\t\t\tans += cnt\n\t\tif sun[i] == s:\n\t\t\tcnt += 1\n\tprint(ans)\n", "from itertools import accumulate\n(n, a) = (int(input()), (*map(int, input().split()),))\np = (*accumulate(a),)\nq = (*accumulate((x == 2 * p[-1] // 3 for x in p)),)\nprint(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)\n", "ints = int(input())\nlistx = list(map(int, input().split(' ')))\nsumx = sum(listx)\nif sumx % 3 != 0:\n\tprint(0)\nelse:\n\tnum1 = 0\n\tnum2 = 0\n\tctotal = 0\n\tlistx.reverse()\n\tlistx.remove(listx[0])\n\tlistx.reverse()\n\tfor i in listx:\n\t\tctotal += i\n\t\tif ctotal == int(sumx * 2 / 3):\n\t\t\tnum2 += num1\n\t\tif ctotal == int(sumx * 1 / 3):\n\t\t\tnum1 += 1\n\tprint(num2)\n", "n = int(input())\na = list(map(int, input().split()))\ndp = [0] * n\ndp[0] = a[0]\nfor i in range(1, n):\n\tdp[i] = dp[i - 1] + a[i]\nif dp[-1] % 3 != 0:\n\tprint(0)\nelif dp[-1] != 0:\n\to = dp[-1] // 3\n\tt = 2 * o\n\tc = 0\n\tco = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tif dp[i] == t:\n\t\t\tc += 1\n\t\telif dp[i] == o:\n\t\t\tco += c\n\tprint(co)\nelse:\n\tc = dp.count(0)\n\tc -= 1\n\tprint(c * (c - 1) // 2)\n", "n = int(input())\ns = list(map(int, input().split()))\ncount = 0\nsv = sum(s)\nif not sv % 3:\n\tsv = sv / 3\n\tt = 2 * sv\n\tu = g = 0\n\tfor i in s[:-1]:\n\t\tu += i\n\t\tif u == t:\n\t\t\tcount += g\n\t\tif u == sv:\n\t\t\tg += 1\nprint(count)\n", "n = int(input())\nA = [int(x) for x in input().split()]\nB = [0] * n\nB[0] = A[0]\nfor i in range(1, n):\n\tB[i] = B[i - 1] + A[i]\nmem = dict()\nans = 0\nfor i in range(n - 3, -1, -1):\n\tif B[i + 1] not in mem:\n\t\tmem[B[i + 1]] = 1\n\telse:\n\t\tmem[B[i + 1]] += 1\n\tif (B[-1] - B[i]) % 2 != 0:\n\t\tcontinue\n\telif (B[-1] - B[i]) // 2 != B[i]:\n\t\tcontinue\n\telif B[-1] - B[i] not in mem:\n\t\tcontinue\n\telse:\n\t\tans += mem[B[-1] - B[i]]\nprint(ans)\n", "from sys import *\n\ndef solve(a, n):\n\t(sm, r) = ([], 0)\n\tfor i in range(n):\n\t\tr += a[i]\n\t\tsm.append(r)\n\tt = sm[-1]\n\tif t % 3 != 0 or n == 1 or n == 2:\n\t\treturn 0\n\tcount = [0] * (n - 1)\n\trc = 0\n\tfor i in range(n - 2, 0, -1):\n\t\tif sm[i] == 2 * t // 3:\n\t\t\trc += 1\n\t\tcount[i - 1] = rc\n\tans = 0\n\tfor i in range(n - 2):\n\t\tif sm[i] == t // 3:\n\t\t\tans += count[i]\n\treturn ans\ntest = 1\nfor t in range(0, test):\n\tn = int(input())\n\tarr = [int(x) for x in input().split()]\n\tans = solve(arr, n)\n\tprint(ans)\n", "n = int(input())\nt = list(map(int, input().split()))\n(cnt, cnt2, s) = (0, 0, sum(t))\nif s % 3 == 0:\n\ta = s // 3\n\tb = s - a\n\tpartial = 0\n\tfor i in range(n - 1):\n\t\tpartial += t[i]\n\t\tif partial == b:\n\t\t\tcnt += cnt2\n\t\tif partial == a:\n\t\t\tcnt2 += 1\nprint(cnt)\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\nif s % 3 != 0:\n\tprint(0)\n\texit()\ns //= 3\nisSum = [0] * n\ncur = 0\nfor i in range(n - 1, -1, -1):\n\tcur += a[i]\n\tif cur == s:\n\t\tisSum[i] = 1\npsums = [0] * (n - 1) + [isSum[-1]]\nfor i in range(n - 2, -1, -1):\n\tpsums[i] += psums[i + 1] + isSum[i]\nret = 0\ncur = 0\nfor i in range(n - 2):\n\tcur += a[i]\n\tif cur == s:\n\t\tret += psums[i + 2]\nprint(ret)\n", "tamanho = int(input())\nvetor_entrada = input()\nvetor_str = list(vetor_entrada.split(' '))\nvetor = list(map(int, vetor_str))\nsoma = sum(vetor)\nif soma % 3 == 0:\n\tsoma = soma / 3\n\tsoma_s = 0\n\tresposta = 0\n\tct = 0\n\tfor cont in range(0, tamanho - 1):\n\t\tsoma_s = soma_s + vetor[cont]\n\t\tif soma_s == 2 * soma:\n\t\t\tresposta = resposta + ct\n\t\tif soma_s == soma:\n\t\t\tct = ct + 1\n\tprint(int(resposta))\nelse:\n\tprint(0)\n", "n = int(input())\nl = [int(j) for j in input().split()]\nsumm = sum(l)\ns = summ // 3\nt = summ - s\nc = 0\na = 0\nb = 0\nif summ % 3 != 0:\n\tprint(0)\nelse:\n\tfor i in range(n - 1):\n\t\ta += l[i]\n\t\tif a == t:\n\t\t\tc += b\n\t\tif a == s:\n\t\t\tb += 1\n\tprint(c)\n", "n = int(input())\na = [int(x) for x in input().split()]\nnb_ways = 0\nsum_a = sum(a)\nif sum_a % 3 != 0 or n < 3:\n\tprint(0)\nelse:\n\tsum_group = int(sum_a / 3)\n\tlist_i = []\n\tlist_j = []\n\tsum_i = 0\n\tfor i in range(1, n - 1):\n\t\tsum_i += a[i - 1]\n\t\tif sum_i == sum_group:\n\t\t\tlist_i.append(i)\n\tsum_j = 0\n\tfor j in reversed(range(2, n)):\n\t\tsum_j += a[j]\n\t\tif sum_j == sum_group:\n\t\t\tlist_j.append(j)\n\tlist_j.sort()\n\tpointeur_i = 0\n\tpointeur_j = 0\n\tlongueur_i = len(list_i)\n\tlongueur_j = len(list_j)\n\twhile pointeur_i < longueur_i and pointeur_j < longueur_j:\n\t\tif list_i[pointeur_i] >= list_j[pointeur_j]:\n\t\t\tpointeur_j += 1\n\t\t\tcontinue\n\t\telse:\n\t\t\tnb_ways += longueur_j - pointeur_j\n\t\t\tpointeur_i += 1\n\tprint(nb_ways)\n", "def countways(a, n):\n\tcnt = [0 for i in range(n)]\n\ts = 0\n\ts = sum(a)\n\tif s % 3 != 0:\n\t\treturn 0\n\ts //= 3\n\tss = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tss += a[i]\n\t\tif ss == s:\n\t\t\tcnt[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tcnt[i] += cnt[i + 1]\n\tans = 0\n\tss = 0\n\tfor i in range(0, n - 2):\n\t\tss += a[i]\n\t\tif ss == s:\n\t\t\tans += cnt[i + 2]\n\treturn ans\nn = int(input())\na = list(map(int, input().split()))\nprint(countways(a, n))\n", "from sys import stdin, stdout\nnmbr = lambda : int(stdin.readline())\nlst = lambda : list(map(int, input().split()))\nn = nmbr()\na = lst()\nsm = sum(a)\nif sm % 3 != 0:\n\tprint(0)\n\texit()\ndp = [0] * n\nsuf = pre = ans = 0\nfor i in range(n - 1, -1, -1):\n\tsuf += a[i]\n\tif i == n - 1:\n\t\tdp[i] = 0\n\telse:\n\t\tdp[i] = dp[i + 1]\n\tif suf == sm // 3:\n\t\tdp[i] += 1\nfor i in range(n):\n\tpre += a[i]\n\tif pre == sm // 3:\n\t\tans += dp[i + 2] if i + 2 < n else 0\nprint(ans)\n", "import bisect\nn = int(input())\na = [int(i) for i in input().split()]\ns = sum(a)\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tans = 0\n\tpref = [0] * (n + 1)\n\tfor i in range(1, n + 1):\n\t\tpref[i] = pref[i - 1] + a[i - 1]\n\tgood1 = []\n\tfor i in range(1, n - 1):\n\t\tif pref[i] == s / 3:\n\t\t\tgood1.append(i - 1)\n\tsuff = [0] * (n + 1)\n\tfor i in range(1, n + 1):\n\t\tsuff[i] = suff[i - 1] + a[n - i]\n\tfor i in range(1, n - 1):\n\t\tif suff[i] == s / 3:\n\t\t\tindex = n - i - 2\n\t\t\tans += bisect.bisect_right(good1, index)\n\tprint(ans)\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\npre = [0] * n\nprecoun = [0] * n\nsuf = [0] * n\nsufcoun = [0] * n\nif s % 3 == 0:\n\tfor i in range(n):\n\t\ttemp = n - i - 1\n\t\tpre[i] = pre[i - 1] + a[i]\n\t\tif pre[i] == s // 3:\n\t\t\tprecoun[i] = precoun[i - 1] + 1\n\t\telse:\n\t\t\tprecoun[i] = precoun[i - 1]\n\t\tsuf[temp] = suf[(temp + 1) % n] + a[temp]\n\t\tif suf[temp] == s // 3:\n\t\t\tsufcoun[temp] = sufcoun[(temp + 1) % n] + 1\n\t\telse:\n\t\t\tsufcoun[temp] = sufcoun[(temp + 1) % n]\n\tans = 0\n\tfor i in range(n - 2):\n\t\tif pre[i] == s // 3:\n\t\t\tans += sufcoun[i + 2]\n\tprint(ans)\nelse:\n\tprint(0)\n", "from __future__ import division, print_function\nimport itertools, os, sys, threading\nfrom collections import deque, Counter, OrderedDict, defaultdict\nimport heapq\nfrom math import ceil, floor, log, sqrt, factorial, pow, pi\n\ndef ii():\n\treturn int(input())\n\ndef si():\n\treturn str(input())\n\ndef mi():\n\treturn map(int, input().split())\n\ndef li():\n\treturn list(mi())\nabc = 'abcdefghijklmnopqrstuvwxyz'\nabd = {'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}\nmod = 1000000007\n(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])\n\ndef getKey(item):\n\treturn item[0]\n\ndef sort2(l):\n\treturn sorted(l, key=getKey)\n\ndef d2(n, m, num):\n\treturn [[num for x in range(m)] for y in range(n)]\n\ndef isPowerOfTwo(x):\n\treturn x and (not x & x - 1)\n\ndef decimalToBinary(n):\n\treturn bin(n).replace('0b', '')\n\ndef ntl(n):\n\treturn [int(i) for i in str(n)]\n\ndef powerMod(x, y, p):\n\tres = 1\n\tx %= p\n\twhile y > 0:\n\t\tif y & 1:\n\t\t\tres = res * x % p\n\t\ty = y >> 1\n\t\tx = x * x % p\n\treturn res\n\ndef gcd(x, y):\n\twhile y:\n\t\t(x, y) = (y, x % y)\n\treturn x\ngraph = defaultdict(list)\nvisited = [0] * 1000000\ncol = [-1] * 1000000\n\ndef dfs(v, c):\n\tif visited[v]:\n\t\tif col[v] != c:\n\t\t\tprint('-1')\n\t\t\texit()\n\t\treturn\n\tcol[v] = c\n\tvisited[v] = 1\n\tfor i in graph[v]:\n\t\tdfs(i, c ^ 1)\n\ndef bfs(d, v):\n\tq = []\n\tq.append(v)\n\tvisited[v] = 1\n\twhile len(q) != 0:\n\t\tx = q[0]\n\t\tq.pop(0)\n\t\tfor i in d[x]:\n\t\t\tif visited[i] != 1:\n\t\t\t\tvisited[i] = 1\n\t\t\t\tq.append(i)\n\t\tprint(x)\n\tprint(l)\n\ndef make_graph(e):\n\td = {}\n\tfor i in range(e):\n\t\t(x, y) = mi()\n\t\tif x not in d.keys():\n\t\t\td[x] = [y]\n\t\telse:\n\t\t\td[x].append(y)\n\t\tif y not in d.keys():\n\t\t\td[y] = [x]\n\t\telse:\n\t\t\td[y].append(x)\n\treturn d\n\ndef gr2(n):\n\td = {}\n\tfor i in range(n):\n\t\t(x, y) = mi()\n\t\tif x not in d.keys():\n\t\t\td[x] = [y]\n\t\telse:\n\t\t\td[x].append(y)\n\treturn d\n\ndef connected_components(graph):\n\tseen = set()\n\n\tdef dfs(v):\n\t\tvs = set([v])\n\t\tcomponent = []\n\t\twhile vs:\n\t\t\tv = vs.pop()\n\t\t\tseen.add(v)\n\t\t\tvs |= set(graph[v]) - seen\n\t\t\tcomponent.append(v)\n\t\treturn component\n\tans = []\n\tfor v in graph:\n\t\tif v not in seen:\n\t\t\td = dfs(v)\n\t\t\tans.append(d)\n\treturn ans\nn = ii()\nl = li()\nif sum(l) % 3 != 0:\n\tprint(0)\nelse:\n\t(k, b, s, c) = (0, 0, 0, 0)\n\tk = sum(l) // 3\n\tfor i in l[:-1]:\n\t\ts += i\n\t\tif s == 2 * k:\n\t\t\tb += c\n\t\tif s == k:\n\t\t\tc += 1\n\tprint(b)\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\nif s % 3 != 0:\n\tprint(0)\n\texit()\nst = 0\nx = s / 3\n(ans, count) = (0, 0)\nfor i in range(n - 1):\n\tst += a[i]\n\tif st == 2 * x:\n\t\tans += count\n\tif st == x:\n\t\tcount += 1\nprint(ans)\n", "n = int(input())\na = [int(x) for x in input().split()]\ntotal = sum(a)\nx = 0\ny = 0\nz = 0\nif total % 3 != 0:\n\tprint(0)\nelse:\n\ts = int(total / 3)\n\tfor i in range(n - 1):\n\t\tx += a[i]\n\t\tif x == 2 * s and i > 0:\n\t\t\tz += y\n\t\tif x == s and i < n - 2:\n\t\t\ty += 1\n\tprint(z)\n", "log = lambda *x: print(*x)\n\ndef cin(*fn, def_fn=str):\n\t(i, r) = (0, [])\n\tfor c in input().split(' '):\n\t\tr += [(fn[i] if len(fn) > i else fn[0])(c)]\n\t\ti += 1\n\treturn r\n(n,) = cin(int)\na = cin(int)\n\ndef RSQ(i, j):\n\treturn pre_sum[j] - pre_sum[i - 1]\ncount = 0\nif sum(a) % 3 == 0:\n\tpre_sum = [0] * n\n\tpre_sum[0] = a[0]\n\tfor i in range(1, n):\n\t\tpre_sum[i] = pre_sum[i - 1] + a[i]\n\tindex = {pre_sum[0]: 0}\n\tidx = lambda i: index.setdefault(i, -1)\n\tseen = {pre_sum[0]: 1}\n\tfor i in range(1, n - 1):\n\t\tv = pre_sum[i]\n\t\thf = v // 2\n\t\tif 2 * hf == v:\n\t\t\tif idx(hf) != -1 and RSQ(i + 1, n - 1) == hf:\n\t\t\t\tcount += seen[hf]\n\t\tindex.setdefault(i, -1)\n\t\tindex[v] = i\n\t\tseen.setdefault(v, 0)\n\t\tseen[v] += 1\nlog(count)\n", "def threeway(a, n):\n\tcnt = [0 for i in range(n)]\n\ts = 0\n\ts = sum(a)\n\tif s % 3 != 0:\n\t\treturn 0\n\ts //= 3\n\tss = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tss += a[i]\n\t\tif ss == s:\n\t\t\tcnt[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tcnt[i] += cnt[i + 1]\n\tans = 0\n\tss = 0\n\tfor i in range(0, n - 2):\n\t\tss += a[i]\n\t\tif ss == s:\n\t\t\tans += cnt[i + 2]\n\treturn ans\nn = eval(input())\na = [int(a) for a in input().split()]\nprint(threeway(a, n))\n", "from sys import stdin\nfrom itertools import accumulate\nn = int(input())\na = (*map(int, input().split()),)\np = (*accumulate(a),)\ns = p[-1]\nif s % 3 or n < 3:\n\tprint(0)\n\texit()\nq = (*accumulate((x == 2 * s // 3 for x in p)),)\nprint(sum((q[-2] - q[i] for i in range(n - 2) if p[i] == s // 3)))\n", "n = int(input())\nnums = [int(i) for i in input().split()]\ns = sum(nums)\nans = 0\nif s % 3 != 0:\n\tans = 0\nelse:\n\ts /= 3\n\tcnt = [0] * n\n\tsum = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tsum += nums[i]\n\t\tif sum == s:\n\t\t\tcnt[i] = 1\n\tfor j in range(n - 2, -1, -1):\n\t\tcnt[j] += cnt[j + 1]\n\tsum = 0\n\tk = 0\n\twhile k + 2 < n:\n\t\tsum += nums[k]\n\t\tif sum == s:\n\t\t\tans += cnt[k + 2]\n\t\tk += 1\nprint(ans)\n", "n = int(input())\na = list(map(int, input().split()))\ndp = list(range(0, n + 1))\ndp[0] = a[0]\nfor i in range(1, n):\n\tdp[i] = dp[i - 1] + a[i]\nif dp[n - 1] % 3 != 0:\n\tprint(0)\n\texit()\ndiv = dp[n - 1] / 3\ntime = 0\ntimg = 0\nfor i in range(0, n - 1):\n\tif dp[i] == div * 2:\n\t\ttimg += time\n\tif dp[i] == div:\n\t\ttime += 1\nprint(timg)\n", "n = int(input())\nlist = list(map(int, input().split()))\n(count, add) = (0, sum(list))\nif add % 3 == 0:\n\tthird = add // 3\n\trem = add - third\n\t(add, d) = (0, 0)\n\tfor i in range(n - 1):\n\t\tadd += list[i]\n\t\tif add == rem:\n\t\t\tcount += d\n\t\tif add == third:\n\t\t\td += 1\nprint(count)\n", "import sys\nimport math\nfrom collections import defaultdict\n\ndef solve():\n\tn = int(input())\n\ta = [int(x) for x in input().split()]\n\tb = [a[0]] * n\n\tfor i in range(1, n):\n\t\tb[i] = b[i - 1] + a[i]\n\tif b[-1] % 3 != 0:\n\t\tprint(0)\n\t\treturn\n\tans = 0\n\tcount = 0\n\trec = []\n\tfor i in range(1, n - 1):\n\t\tif b[i] == 2 * b[-1] // 3:\n\t\t\tcount += 1\n\t\t\trec.append(i)\n\tj = 0\n\tif count == 0:\n\t\tprint(ans)\n\t\treturn\n\tfor i in range(n):\n\t\tif b[i] == 2 * b[-1] // 3 and i == rec[j]:\n\t\t\tcount -= 1\n\t\t\tj += 1\n\t\t\tif count == 0:\n\t\t\t\tbreak\n\t\tif b[i] == b[-1] // 3 and i != rec[j]:\n\t\t\tans += count\n\tprint(ans)\ntest_cases = 1\nfor _ in range(test_cases):\n\tsolve()\n", "n = int(input())\nl = list(map(int, input().split()))\nprif = []\nsum_ = 0\nfor i in l:\n\tsum_ = sum_ + i\n\tprif.append(sum_)\ntotal = prif[n - 1]\nif total % 3 != 0:\n\tprint(0)\nelse:\n\tarr = [0] * n\n\treq = total * 2 // 3\n\treq1 = total // 3\n\tcou = prif[:n - 1].count(req)\n\tj = 0\n\tfor i in range(n - 1):\n\t\tif i != 0:\n\t\t\tarr[i] = cou - j\n\t\tif prif[i] == req:\n\t\t\tj += 1\n\tans = 0\n\tfor i in range(n - 2):\n\t\tif prif[i] == req1:\n\t\t\tans = ans + arr[i + 1]\n\tprint(ans)\n", "import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools\nsys.setrecursionlimit(10 ** 7)\ninf = 10 ** 20\neps = 1.0 / 10 ** 10\nmod = 10 ** 9 + 7\ndd = [(-1, 0), (0, 1), (1, 0), (0, -1)]\nddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]\n\ndef LI():\n\treturn [int(x) for x in sys.stdin.readline().split()]\n\ndef LI_():\n\treturn [int(x) - 1 for x in sys.stdin.readline().split()]\n\ndef LF():\n\treturn [float(x) for x in sys.stdin.readline().split()]\n\ndef LS():\n\treturn sys.stdin.readline().split()\n\ndef I():\n\treturn int(sys.stdin.readline())\n\ndef F():\n\treturn float(sys.stdin.readline())\n\ndef S():\n\treturn input()\n\ndef pf(s):\n\treturn print(s, flush=True)\n\ndef main():\n\tn = I()\n\ta = LI()\n\ts = sum(a)\n\tif s % 3 != 0:\n\t\treturn 0\n\ts3 = s // 3\n\twa = [0]\n\tc = 0\n\tfor k in a:\n\t\tc += k\n\t\tif c == s3:\n\t\t\twa.append(wa[-1] + 1)\n\t\telse:\n\t\t\twa.append(wa[-1])\n\tr = 0\n\tc = 0\n\tfor i in range(n - 1, 0, -1):\n\t\tc += a[i]\n\t\tif c == s3:\n\t\t\tr += wa[i - 1]\n\treturn r\nprint(main())\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\naccumulate = 0\none_third = [0] * (n + 1)\nfor i in range(n - 2):\n\taccumulate += a[i]\n\tif accumulate * 3 == s:\n\t\tone_third[i] = 1\naccumulate = 0\nans = 0\nv = sum(one_third)\nfor i in range(n - 1, 1, -1):\n\tv -= one_third[i - 1]\n\taccumulate += a[i]\n\tif accumulate * 3 == s:\n\t\tans += v\nprint(ans)\n", "n = int(input())\na = list(map(int, input().split()))\nb = sum(a)\nif b % 3 == 0:\n\te = b / 3\n\ti = 0\n\tc = [0] * n\n\td = 0\n\twhile i < n:\n\t\td += a[i]\n\t\tc[i] += c[i - 1]\n\t\tif d == e and i < n - 2:\n\t\t\tc[i] += 1\n\t\ti += 1\n\ti = n\n\td = 0\n\tf = 0\n\twhile i > 0:\n\t\ti -= 1\n\t\td += a[i]\n\t\tif d == e and i > 1:\n\t\t\tf += c[i - 2]\n\tprint(f)\nelse:\n\tprint(0)\n", "def main():\n\tfrom sys import stdin\n\tfrom itertools import accumulate, tee, islice\n\ttokens = map(int, stdin.read().split())\n\tN = next(tokens)\n\t(A, A2) = tee(tokens)\n\tS = sum(A2)\n\tif S % 3 != 0:\n\t\tprint(0)\n\t\treturn\n\tS1 = int(S / 3)\n\tS2 = int(2 * S / 3)\n\tans = count = 0\n\tfor s in islice(accumulate(A), N - 1):\n\t\tif s == S2:\n\t\t\tans += count\n\t\tif s == S1:\n\t\t\tcount += 1\n\tprint(ans)\nmain()\n", "import bisect\ninput()\nls = list(map(int, input().split()))\ncum = [0] * len(ls)\ncum[0] = ls[0]\nfor i in range(1, len(cum)):\n\tcum[i] = ls[i] + cum[i - 1]\n\ndef f(arr, a, b):\n\tif a == 0:\n\t\treturn arr[b]\n\telse:\n\t\treturn arr[b] - arr[a - 1]\nc = 0\ns = sum(ls)\nif not s % 3:\n\ttg = s // 3\n\tnl = nh = 0\n\tacm = 0\n\tfor x in ls[:-1]:\n\t\tacm += x\n\t\tif acm == 2 * tg:\n\t\t\tc += nl\n\t\tif acm == tg:\n\t\t\tnl += 1\nprint(c)\n", "n = int(input())\na = map(int, input().split())\nv = 0\ns = []\nfor e in a:\n\tv += e\n\ts.append(v)\ntotal_sum = s[-1]\nif total_sum % 3 != 0:\n\tprint(0)\nelse:\n\tr_idx = [0] * n\n\tfor i in range(n - 1, 0, -1):\n\t\tif i < n - 1:\n\t\t\tr_idx[i] = r_idx[i + 1]\n\t\tif s[i - 1] == total_sum // 3 * 2:\n\t\t\tr_idx[i] += 1\n\tr = 0\n\tfor i in range(n - 2):\n\t\tif s[i] == total_sum // 3:\n\t\t\tr += r_idx[i + 2]\n\tprint(r)\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\nif n < 3:\n\tprint(0)\nelif s % 3 == 0:\n\tes = s // 3\n\tc = 0\n\tc1 = 0\n\tfirst = []\n\tsuml = []\n\tfor i in range(n):\n\t\tc += a[i]\n\t\tfirst.append(int(c == es))\n\t\tc1 += a[n - 1 - i]\n\t\tv = int(c1 == es)\n\t\tif i == 0:\n\t\t\tsuml.append(v)\n\t\telse:\n\t\t\tsuml.append(suml[-1] + v)\n\tsuml = suml[::-1]\n\tans = 0\n\tfor i in range(n - 2):\n\t\tif first[i] == 1:\n\t\t\tans += suml[i + 2]\n\tprint(ans)\nelse:\n\tprint(0)\n", "n = int(input())\nk = list(map(int, input().split()))\nans = 0\ns = sum(k)\nif s % 3 == 0:\n\tone_thirds = s // 3\n\ttwo_thirds = s - one_thirds\n\ts = t = 0\n\tfor i in range(n - 1):\n\t\ts += k[i]\n\t\tif s == two_thirds:\n\t\t\tans += t\n\t\tif s == one_thirds:\n\t\t\tt += 1\nprint(ans)\n", "import sys\nn = int(input())\na = map(int, input().split())\na = list(a)\ns = sum(a)\nif n <= 2 or s % 3:\n\tprint(0)\n\tsys.exit(0)\nzz = int(s / 3)\nc = [0] * n\nx = 0\nfor i in range(n - 1, -1, -1):\n\tx += a[i]\n\tif x == zz:\n\t\tc[i] = 1\nfor i in range(n - 2, -1, -1):\n\tc[i] += c[i + 1]\nans = 0\nx = 0\nfor i in range(n - 2):\n\tx += a[i]\n\tif x == zz:\n\t\tans += c[i + 2]\nprint(ans)\n", "n = int(input())\na = [int(i) for i in input().split()]\ns = sum(a)\nx = 0\nif s % 3 == 0:\n\td1 = s / 3\n\td2 = 2 * d1\n\tz = t = 0\n\tfor i in range(n - 1):\n\t\tz += a[i]\n\t\tif z == d2:\n\t\t\tx += t\n\t\tif z == d1:\n\t\t\tt += 1\nprint(x)\n", "from sys import stdin, stdout\ninput = stdin.readline\nmp = lambda : map(int, input().split())\nit = lambda : int(input())\na = it()\nb = list(mp())\n(sm, rem) = divmod(sum(b), 3)\nif rem:\n\tprint(0)\nelse:\n\t(fst, scd) = (0, 0)\n\tpre = 0\n\tfor i in range(a - 1):\n\t\tpre += b[i]\n\t\tif pre == 2 * sm:\n\t\t\tscd += fst\n\t\tif pre == sm:\n\t\t\tfst += 1\n\tprint(scd)\n", "n = int(input())\nA = [int(a) for a in input().split()]\ns = sum(A)\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tpr = [0] * (n + 1)\n\tfor i in range(n):\n\t\tpr[i + 1] = pr[i] + A[i]\n\tsf = [0] * (n + 1)\n\tsf[n] = A[n - 1]\n\tfor i in range(n - 1, 0, -1):\n\t\tsf[i] = sf[i + 1] + A[i - 1]\n\tcnt = 0\n\tC = [0] * (n + 1)\n\tfor j in range(1, n + 1):\n\t\tC[j] = 1 if sf[j] == s // 3 else 0\n\tS = [0] * (n + 1)\n\tS[n] = C[n]\n\tfor i in range(n - 1, 0, -1):\n\t\tS[i] = S[i + 1] + C[i]\n\tfor i in range(1, n - 1):\n\t\tif pr[i] == s // 3:\n\t\t\tcnt += S[i + 2]\n\tprint(cnt)\n", "n = int(input())\narr = list(map(int, input().split()))\ntot = sum(arr)\nif tot % 3 != 0:\n\tprint(0)\nelse:\n\treq = tot // 3\n\tpresum = []\n\tsuma = 0\n\tfor i in range(n):\n\t\tsuma += arr[i]\n\t\tif suma == req:\n\t\t\tpresum.append(i)\n\tpostsum = []\n\tsuma = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tsuma += arr[i]\n\t\tif suma == req:\n\t\t\tpostsum.append(i)\n\tpostsum.sort()\n\tl1 = len(presum)\n\tl2 = len(postsum)\n\tans = 0\n\tfor i in range(l1):\n\t\tcurr = presum[i]\n\t\tl = 0\n\t\tr = l2 - 1\n\t\twhile l <= r:\n\t\t\tmid = (l + r) // 2\n\t\t\tif postsum[mid] > curr + 1:\n\t\t\t\tr = mid - 1\n\t\t\telse:\n\t\t\t\tl = mid + 1\n\t\tif 0 <= l < l2 and postsum[l] > curr + 1:\n\t\t\tans += l2 - l\n\tprint(ans)\n", "import heapq as hp\nimport collections\nimport bisect\nimport math\n\ndef unpack(func=int):\n\treturn map(func, input().split())\n\ndef l_unpack(func=int):\n\treturn list(map(func, input().split()))\n\ndef s_unpack(func=int):\n\treturn sorted(map(func, input().split()))\n\ndef range_n():\n\treturn range(int(input()))\n\ndef counter(a):\n\td = {}\n\tfor x in a:\n\t\tif x in d:\n\t\t\td[x] += 1\n\t\telse:\n\t\t\td[x] = 1\n\treturn d\n\ndef getint():\n\treturn int(input())\n\ndef main():\n\tn = getint()\n\ta = l_unpack()\n\tcs2 = cs = 0\n\ts = sum(a)\n\tif s % 3:\n\t\tprint(0)\n\telse:\n\t\tt = 0\n\t\ts //= 3\n\t\tfor i in range(n - 1):\n\t\t\tt += a[i]\n\t\t\tif t == 2 * s:\n\t\t\t\tcs2 += cs\n\t\t\tif t == s:\n\t\t\t\tcs += 1\n\t\tprint(cs2)\nmain()\n", "n = int(input())\nlst = list(map(int, input().split()))\nsm = [0] * n\nsm[0] = lst[0]\nfor i in range(1, len(lst)):\n\tsm[i] = sm[i - 1] + lst[i]\nif sm[-1] % 3 != 0:\n\tprint(0)\nelse:\n\tcount = 0\n\tpart = sm[-1] // 3\n\tpos3 = [0] * n\n\tpos6 = [0] * n\n\tcount = 0\n\tfor i in range(len(sm) - 1)[::-1]:\n\t\tif sm[i] == part and i != n - 2:\n\t\t\tpos3[i] = count\n\t\tif sm[i] == part * 2 and i != 0:\n\t\t\tpos6[i] = 1\n\t\t\tcount += 1\n\tprint(sum(pos3))\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\nif n <= 2 or s % 3 != 0:\n\tprint(0)\nelse:\n\tref = s // 3\n\tl = 0\n\tr = s\n\tposl = []\n\tposr = []\n\tfor i in range(len(a) - 1):\n\t\tl += a[i]\n\t\tr -= a[i]\n\t\tif l == ref:\n\t\t\tposl.append(i)\n\t\tif r == ref:\n\t\t\tposr.append(i + 1)\n\tcount = 0\n\ti = len(posl) - 1\n\tj = len(posr) - 1\n\twhile i >= 0 and j >= 0:\n\t\tif posl[i] + 2 <= posr[j]:\n\t\t\tcount += i + 1\n\t\t\tj -= 1\n\t\telse:\n\t\t\ti -= 1\n\tprint(count)\n", "n = int(input())\nm = b = 0\ns = [int(i) for i in input().split()]\nfor i in range(n - 1):\n\tif s[i + 1] < 0:\n\t\tb = 1\n\ts[i + 1] = s[i] + s[i + 1]\nif int(s[n - 1] / 3) == s[n - 1] / 3:\n\ta = s[n - 1] // 3\n\tif a == 0:\n\t\tt = s.count(0)\n\t\tm = (t - 1) * (t - 2) // 2\n\telif b == 0:\n\t\tm = s.count(a) * s.count(2 * a)\n\telse:\n\t\tfor i in range(n):\n\t\t\tif s[i] == a:\n\t\t\t\tm += s[i:].count(2 * a)\nprint(m)\n", "from itertools import accumulate, combinations\nn = int(input())\nseq = list(map(int, input().split()))\norder_sum = list(accumulate(seq, lambda x, y: x + y))\nif order_sum[n - 1] % 3 != 0 or n < 3:\n\tprint(0)\nelse:\n\treverse_sum = []\n\tcur = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tcur += seq[i]\n\t\treverse_sum.append(cur)\n\tborder = order_sum[n - 1] // 3\n\tequal = reverse_sum.count(border)\n\tcounter = 0\n\tif border != 0:\n\t\tmiddle_list = []\n\t\torder_list = []\n\t\tright_list = []\n\t\tt = 0\n\t\tfor i in range(n):\n\t\t\tif reverse_sum[i] == border:\n\t\t\t\tt += 1\n\t\t\tif order_sum[i] == border:\n\t\t\t\tright_list.append(i)\n\t\t\torder_list.append(t)\n\t\tfor i in range(len(right_list)):\n\t\t\tcounter += order_list[n - 2 - right_list[i]]\n\t\tprint(counter)\n\telse:\n\t\tprint((equal ** 2 - 3 * equal + 2) // 2)\n", "n = int(input())\na = list(map(int, input().split()))\ntotal = sum(a)\nif total % 3 != 0:\n\tprint(0)\nelse:\n\tt = total // 3\n\tp = 0\n\ts = 0\n\tprefix = []\n\tsuffix = []\n\tfor (idx, val) in enumerate(a):\n\t\tj = n - idx - 1\n\t\tp += val\n\t\ts += a[j]\n\t\tif p == t:\n\t\t\tprefix.append(idx)\n\t\tif s == t:\n\t\t\tsuffix.append(j)\n\tsuffix = suffix[::-1]\n\tres = 0\n\tj = 0\n\tsuffix_size = len(suffix)\n\tfor (idx, val) in enumerate(prefix):\n\t\twhile j < suffix_size and val >= suffix[j] - 1:\n\t\t\tj += 1\n\t\tres += suffix_size - j\n\tprint(res)\n", "n = int(input())\narr = list(map(int, input().split()))\nsumm = 0\nfor i in arr:\n\tsumm = summ + i\nif summ % 3 != 0:\n\tprint('0')\nelif summ == 0:\n\tcount = 0\n\tcum = 0\n\tfor i in arr:\n\t\tcum = cum + i\n\t\tif cum == 0:\n\t\t\tcount = count + 1\n\tif count >= 3:\n\t\tans = int((count - 1) * (count - 2) / 2)\n\t\tprint(int(ans))\n\telse:\n\t\tprint(0)\nelse:\n\tk1 = summ / 3\n\tk2 = k1 * 2\n\tk3 = summ\n\tck1 = 0\n\tck2 = 0\n\tck3 = 0\n\tcum = 0\n\tfor i in arr:\n\t\tcum = cum + i\n\t\tif cum == k3:\n\t\t\tck3 = ck3 + ck2\n\t\t\tck2 = 0\n\t\tif cum == k2:\n\t\t\tck2 = ck1 + ck2\n\t\tif cum == k1:\n\t\t\tck1 = ck1 + 1\n\tck1 = 0\n\tprint(int(ck3))\n", "class Solve:\n\n\tdef __init__(self, n):\n\t\tself.n = n\n\t\tself.ls = list(map(int, input().split()))\n\n\tdef solution(self):\n\t\ts = sum(self.ls)\n\t\tans = 0\n\t\tif not s % 3:\n\t\t\tp = s // 3\n\t\t\tv = s - p\n\t\t\ts = t = 0\n\t\t\tself.ls.pop()\n\t\t\tfor x in self.ls:\n\t\t\t\ts += x\n\t\t\t\tif s == v:\n\t\t\t\t\tans += t\n\t\t\t\tif s == p:\n\t\t\t\t\tt += 1\n\t\tprint(ans)\nobj = Solve(int(input()))\nobj.solution()\n", "n = int(input())\nans = 0\na = []\nl = 0\nif n < 3:\n\tprint(0)\n\tquit()\nfor (i, e) in enumerate(map(int, input().split())):\n\tif i == 0:\n\t\ta.append(e)\n\t\tcontinue\n\ta.append(e + a[i - 1])\ns = a[-1]\nif s == 0:\n\tm = a.count(0) - 2\n\tans = m * (m + 1) // 2\n\tif ans < 0:\n\t\tans = 0\n\tprint(ans)\n\tquit()\nif s % 3 != 0:\n\tprint(0)\n\tquit()\nfor e in a:\n\tif e == s // 3:\n\t\tl += 1\n\tif e == 2 * s // 3:\n\t\tans += l\nprint(ans)\n", "n = int(input())\na = [int(x) for x in input().split()]\ni = 1\ns = sum(a)\nif s % 3 != 0:\n\tprint(0)\n\texit()\nif n < 3:\n\tprint(0)\n\texit()\nt = int(s / 3)\nie = 0\nsecond = [0 for x in a]\nfor i in range(n - 1):\n\tie += a[i]\n\tif ie == 2 * t:\n\t\tsecond[i] = 1\nfor i in range(n - 2, -1, -1):\n\tsecond[i] += second[i + 1]\nfirst = 0\nanswer = 0\nfor i in range(0, n - 2):\n\tfirst += a[i]\n\tif first == t:\n\t\tanswer += second[i + 1]\nprint(answer)\n", "def sol(a, n):\n\ts = sum(a)\n\tdp = [0 for i in range(n)]\n\tif s % 3 != 0:\n\t\treturn 0\n\ts //= 3\n\tss = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tss += a[i]\n\t\tif ss == s:\n\t\t\tdp[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tdp[i] += dp[i + 1]\n\tans = 0\n\tss = 0\n\tfor i in range(n - 2):\n\t\tss += a[i]\n\t\tif ss == s:\n\t\t\tans += dp[i + 2]\n\treturn ans\nn = int(input())\na = [int(i) for i in input().split()]\nprint(sol(a, n))\n", "n = int(input())\nlst = list(map(int, input().split()))\ns = sum(lst)\n(a, b, c) = (0, 0, 0)\nif s % 3 == 0:\n\tr = s // 3\n\tr1 = s - r\n\tfor i in range(n - 1):\n\t\tc = c + lst[i]\n\t\tif c == r1:\n\t\t\ta += b\n\t\tif c == r:\n\t\t\tb += 1\nprint(a)\n", "def main():\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\ts = sum(a)\n\tif s % 3 != 0:\n\t\tprint(0)\n\t\treturn\n\tp = [a[0]]\n\tfor x in a[1:]:\n\t\tp.append(p[-1] + x)\n\tr = [a[-1]]\n\tfor x in a[-2::-1]:\n\t\tr.append(r[-1] + x)\n\tr = r[::-1]\n\tz = [0] * n\n\tfor (i, x) in enumerate(r):\n\t\tif x * 3 == s:\n\t\t\tz[i] = 1\n\ti = n - 2\n\twhile i >= 0:\n\t\tz[i] += z[i + 1]\n\t\ti -= 1\n\tans = 0\n\tfor (i, x) in enumerate(p):\n\t\tif x * 3 == s and i + 2 < n:\n\t\t\tans += z[i + 2]\n\tprint(ans)\nmain()\n", "from collections import defaultdict\nn = int(input())\na = list(map(int, input().split()))\npref = [0 for i in range(n + 123)]\nwas = defaultdict(int)\nfor i in range(1, n + 1):\n\tpref[i] = pref[i - 1] + a[i - 1]\n\twas[pref[i]] += 1\nans = 0\nfor i in range(1, n - 1):\n\twas[pref[i]] -= 1\n\tif pref[n] == pref[i] * 3:\n\t\tans += was[pref[i] * 2]\n\t\tans -= pref[n] == pref[i] * 2\nprint(ans)\n", "n = int(input())\nlis = list(map(int, input().split()))\npre = [0] * (n + 1)\nfor i in range(1, n + 1):\n\tpre[i] = pre[i - 1] + lis[i - 1]\nif pre[-1] % 3:\n\tprint(0)\nelse:\n\ts = pre[-1] // 3\n\tans = t = 0\n\tfor i in range(1, n):\n\t\tif pre[i] == 2 * s:\n\t\t\tans += t\n\t\tif pre[i] == s:\n\t\t\tt += 1\n\tprint(ans)\n", "def solve(n, li):\n\tsu = sum(li)\n\tfact = su / 3\n\tfirst = 0\n\tcount = 0\n\tcount_num = 0\n\tfor i in range(n - 1):\n\t\tfirst += li[i]\n\t\tif first == 2 * fact:\n\t\t\tcount += count_num\n\t\tif first == fact:\n\t\t\tcount_num += 1\n\treturn count\nn = int(input())\nli = list(map(int, input().split()))\nresult = solve(n, li)\nprint(result)\n", "n = int(input())\nmas = list(map(int, input().split(' ')))\nmas2 = [0] * n\nmas3 = [0] * n\nsumm = sum(mas)\nif summ % 3 != 0:\n\tprint(0)\nelse:\n\tsumm /= 3\n\tsu = 0\n\tschet = 0\n\tfor i in range(n):\n\t\tsu += mas[i]\n\t\tif su == summ:\n\t\t\tmas2[i] = 1\n\tsu = 0\n\tschet = 0\n\tfor i in range(n):\n\t\tsu += mas[n - i - 1]\n\t\tif su == summ:\n\t\t\tschet += 1\n\t\tmas3[n - i - 1] = schet\n\tres = 0\n\tfor i in range(n - 2):\n\t\tif mas2[i]:\n\t\t\tres += mas3[i + 2]\n\tprint(res)\n", "n = int(input())\nk = list(map(int, input().split()))\nans = 0\ns = sum(k)\nif s % 3 == 0:\n\tp = s // 3\n\tv = s - p\n\ts = t = 0\n\tfor i in range(n - 1):\n\t\ts += k[i]\n\t\tif s == v:\n\t\t\tans += t\n\t\tif s == p:\n\t\t\tt += 1\nprint(ans)\n", "n = int(input())\narr = list(map(int, input().split()))\nsum_of_arr = sum(arr)\nprefix_sum = 0\ntotal = 0\ncount = 0\nif sum_of_arr % 3:\n\tprint(0)\nelse:\n\tfor i in range(n - 1):\n\t\tprefix_sum += arr[i]\n\t\tif prefix_sum == 2 * (sum_of_arr // 3):\n\t\t\ttotal += count\n\t\tif prefix_sum == sum_of_arr // 3:\n\t\t\tcount += 1\n\tprint(total)\n", "n = int(input())\na = list(map(int, input().split()))\nsum_a = sum(a)\nsum_a_3 = sum_a // 3\nres = 0\nif sum_a % 3 == 0:\n\tl = []\n\tr = []\n\tcnt = 0\n\tfor i in range(0, n - 1):\n\t\tcnt += a[i]\n\t\ta[i] = cnt\n\t\tif cnt == sum_a_3:\n\t\t\tl.append(i)\n\t\tif sum_a - cnt == sum_a_3:\n\t\t\tr.append(i + 1)\n\tj = 0\n\tfor i in l:\n\t\twhile j < len(r) and i + 2 > r[j]:\n\t\t\tj += 1\n\t\tif j < len(r):\n\t\t\tres += len(r) - j\nprint(res)\n", "from collections import defaultdict, Counter\nn = int(input())\narr = [int(i) for i in input().split()]\nprefix = []\nfor (i, num) in enumerate(arr[:-1]):\n\tif prefix == []:\n\t\tprefix.append(num)\n\telse:\n\t\tprefix.append(num + prefix[i - 1])\nc = defaultdict(int)\na = sum(arr) // 3\ns = sum(arr)\nif s % 3 != 0:\n\tprint(0)\n\texit(0)\nb = 2 * sum(arr) // 3\nr = 0\ntot = 0\ncnt = 0\nfor p in arr[:-1]:\n\tr += p\n\tif r == b:\n\t\ttot += cnt\n\tif r == a:\n\t\tcnt += 1\nprint(tot)\n", "n = int(input())\na = list(map(int, input().split()))\ntot = sum(a)\nif tot % 3:\n\tprint(0)\nelse:\n\t(x, y) = (tot // 3, 2 * tot // 3)\n\tb = [0] * (n + 1)\n\tss = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tss += a[i]\n\t\tb[i] = b[i + 1]\n\t\tif ss == x:\n\t\t\tb[i] += 1\n\tss = 0\n\tans = 0\n\tfor i in range(n - 2):\n\t\tss += a[i]\n\t\tif ss == x:\n\t\t\tans += b[i + 2]\n\tprint(ans)\n", "from __future__ import print_function\nimport sys\nfrom collections import *\nfrom heapq import *\nfrom functools import *\nINF = float('inf')\nNINF = float('-inf')\ntry:\n\tinput = raw_input\nexcept:\n\tpass\n\ndef read_string():\n\treturn input()\n\ndef read_string_line():\n\treturn [x for x in input().split(' ')]\n\ndef read_int_line():\n\treturn [int(x) for x in input().split(' ')]\n\ndef read_int():\n\treturn int(input())\n\nclass TreeNode(object):\n\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.left = None\n\t\tself.right = None\n\n\tdef __str__(self):\n\t\tq = deque()\n\t\tans = []\n\t\tq.append(self)\n\t\tans.append(self.val)\n\t\twhile q:\n\t\t\tcur = q.popleft()\n\t\t\tans.append(cur.left.val if cur.left else 'null')\n\t\t\tans.append(cur.right.val if cur.right else 'null')\n\t\t\tif cur.left:\n\t\t\t\tq.append(cur.left)\n\t\t\tif cur.right:\n\t\t\t\tq.append(cur.right)\n\t\twhile ans and ans[-1] == 'null':\n\t\t\tans.pop()\n\t\treturn str(ans).replace(\"'null'\", 'null')\n\ndef toTreeNode(s):\n\tl = eval(s.replace('null', 'None'))\n\tn = len(l)\n\tif not n:\n\t\treturn None\n\troot = TreeNode(l[0])\n\tq = deque()\n\tq.append(root)\n\ti = 1\n\twhile q and i < n:\n\t\tcur = q.popleft()\n\t\tif l[i] != None:\n\t\t\tcur.left = TreeNode(l[i])\n\t\t\tq.append(cur.left)\n\t\ti += 1\n\t\tif i >= n:\n\t\t\tbreak\n\t\tif l[i] != None:\n\t\t\tcur.right = TreeNode(l[i])\n\t\t\tq.append(cur.right)\n\t\ti += 1\n\treturn root\n\nclass Djs:\n\n\tdef __init__(self, n):\n\t\tself.w = [1] * n\n\t\tself.p = [-1] * n\n\n\tdef find(self, x):\n\t\tp = self.p[x]\n\t\tif p == -1:\n\t\t\treturn x\n\t\telse:\n\t\t\tself.p[x] = self.find(p)\n\t\t\treturn self.p[x]\n\n\tdef union(self, x, y):\n\t\t(x, y) = (self.find(x), self.find(y))\n\t\tif x == y:\n\t\t\treturn\n\t\tif self.w[x] > self.w[y]:\n\t\t\t(x, y) = (y, x)\n\t\tself.p[x] = y\n\t\tself.w[y] += self.w[x]\n\n\tdef roots(self):\n\t\treturn [x for (x, y) in enumerate(self.p) if y == -1]\n\ndef mat3d(x, y, z, fill=0):\n\treturn [[[fill] * z for _ in range(y)] for _ in range(x)]\n\ndef mat2d(h, w, fill=0):\n\treturn [[fill] * w for _ in range(h)]\n\ndef matmul(m1, m2, P=0):\n\t(h1, w1) = (len(m1), len(m1[0]))\n\t(h2, w2) = (len(m2), len(m2[0]))\n\tassert w1 == h2\n\tans = mat2d(h1, w2)\n\tif P:\n\t\tfor i in range(h1):\n\t\t\tfor j in range(w2):\n\t\t\t\tfor k in range(w1):\n\t\t\t\t\tans[i][j] = (ans[i][j] + m1[i][k] * m2[k][j]) % P\n\telse:\n\t\tfor i in range(h1):\n\t\t\tfor j in range(w2):\n\t\t\t\tfor k in range(w1):\n\t\t\t\t\tans[i][j] = ans[i][j] + m1[i][k] * m2[k][j]\n\treturn ans\n\ndef mati(n):\n\tans = mat2d(n, n)\n\tfor i in range(n):\n\t\tans[i][i] = 1\n\treturn ans\n\ndef matpow(m, p, P):\n\t(h, w) = (len(m), len(m[0]))\n\tassert h == w\n\tans = mati(h)\n\tbase = m\n\twhile p:\n\t\tif p % 2 == 1:\n\t\t\tans = matmul(ans, base, P)\n\t\tbase = matmul(base, base, P)\n\t\tp //= 2\n\treturn ans\n\ndef doit():\n\tn = read_int()\n\tarr = read_int_line()\n\tw = len(arr)\n\tdp = [0] * (w + 1)\n\tsm = sum(arr)\n\tif sm % 3 != 0:\n\t\tprint('0')\n\t\treturn\n\tavg = sm // 3\n\tacc = 0\n\tfor (i, a) in enumerate(arr):\n\t\tacc += a\n\t\tif acc == avg:\n\t\t\tdp[i + 1] = dp[i] + 1\n\t\telse:\n\t\t\tdp[i + 1] = dp[i]\n\tans = 0\n\tacc = 0\n\tfor i in range(w - 1, -1, -1):\n\t\tacc += arr[i]\n\t\tif acc == avg:\n\t\t\tif avg != 0:\n\t\t\t\tans += dp[i]\n\t\t\telif i > 0:\n\t\t\t\tans += dp[i - 1]\n\tprint('%d' % ans)\ntry:\n\twhile True:\n\t\tdoit()\nexcept EOFError:\n\tpass\n", "import os, io\nfrom sys import stdout\nimport collections\nimport random\ninput = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline\n\ndef binomial_coefficient(n, k):\n\tif 0 <= k <= n:\n\t\tntok = 1\n\t\tktok = 1\n\t\tfor t in range(1, min(k, n - k) + 1):\n\t\t\tntok *= n\n\t\t\tktok *= t\n\t\t\tn -= 1\n\t\treturn ntok // ktok\n\telse:\n\t\treturn 0\n\ndef powerOfK(k, max):\n\tif k == 1:\n\t\treturn [1]\n\tif k == -1:\n\t\treturn [-1, 1]\n\tresult = []\n\tn = 1\n\twhile n <= max:\n\t\tresult.append(n)\n\t\tn *= k\n\treturn result\n\ndef prefixSum(arr):\n\tfor i in range(1, len(arr)):\n\t\tarr[i] = arr[i] + arr[i - 1]\n\treturn arr\nn = int(input())\na = list(map(int, input().split()))\n\ndef bf(n, arr):\n\tcnt = 0\n\tfor i in range(len(a) - 2):\n\t\tfor j in range(i + 1, len(a) - 1):\n\t\t\tif sum(arr[:i + 1]) == sum(arr[i + 1:j + 1]) == sum(arr[j + 1:]):\n\t\t\t\tcnt += 1\n\treturn cnt\n\ndef sol(n, a):\n\tps = prefixSum(a)\n\ttarget = ps[-1]\n\tif target % 3 != 0:\n\t\treturn 0\n\telse:\n\t\tf1 = target // 3\n\t\tf2 = f1 * 2\n\t\tf3 = f1 * 3\n\t\tif f1 == f2 == f3:\n\t\t\treturn binomial_coefficient(ps.count(f1) - 1, 2)\n\t\telse:\n\t\t\tcnt = 0\n\t\t\tp1 = 0\n\t\t\tfor e in ps:\n\t\t\t\tif e == f1:\n\t\t\t\t\tp1 += 1\n\t\t\t\telif e == f2:\n\t\t\t\t\tcnt += p1\n\t\t\treturn cnt\nprint(sol(n, a))\n", "def fun():\n\tn = int(input())\n\ta = input()\n\ta = a.split(' ')\n\tcnt = [0] * n\n\ts = 0\n\tfor i in range(n):\n\t\ts = s + int(a[i])\n\tif s % 3 != 0:\n\t\treturn 0\n\ts = s / 3\n\tt = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tt = t + int(a[i])\n\t\tif t == s:\n\t\t\tcnt[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tcnt[i] += cnt[i + 1]\n\tans = 0\n\tss = 0\n\tfor i in range(0, n - 2, 1):\n\t\tss += int(a[i])\n\t\tif ss == s:\n\t\t\tans = ans + cnt[i + 2]\n\treturn ans\nprint(fun())\n", "input()\nmat = list(map(int, input().split()))\navg = sum(mat)\nif avg % 3:\n\tprint(0)\nelse:\n\tavg //= 3\n\tjumper = counter = answer = 0\n\tfor x in mat:\n\t\tjumper += x\n\t\tif avg == 0:\n\t\t\tif jumper == 0:\n\t\t\t\tcounter += 1\n\t\t\tcontinue\n\t\tif jumper == avg:\n\t\t\tcounter += 1\n\t\tif jumper == avg * 2:\n\t\t\tanswer += counter\n\tif avg == 0:\n\t\tprint((counter - 1) * (counter - 2) // 2)\n\telse:\n\t\tprint(answer)\n", "n = int(input())\na = list(map(int, input().split()))\ntotal_sum = sum(a)\ncurr_sum = 0\nidx_count = 0\nans = 0\nfor i in range(n):\n\tcurr_sum += a[i]\n\tif total_sum != 0:\n\t\tif total_sum == 3 * curr_sum:\n\t\t\tidx_count += 1\n\t\telif total_sum * 2 == 3 * curr_sum:\n\t\t\tans += idx_count\n\telif curr_sum == 0 and i != n - 1:\n\t\tidx_count += 1\nif total_sum == 0:\n\tprint(idx_count * (idx_count - 1) // 2)\nelse:\n\tprint(ans)\n", "n = int(input())\na = list(map(int, input().split()))\nsum = 0\nfor x in a:\n\tsum += x\nif sum % 3 != 0 or n < 3:\n\tprint(0)\nelse:\n\t(ct, ans, s) = (0, 0, 0)\n\tfor i in range(0, n - 1):\n\t\ts += a[i]\n\t\tif s == 2 * (sum // 3):\n\t\t\tans += ct\n\t\tif s == sum // 3:\n\t\t\tct += 1\n\tprint(ans)\n", "n = int(input())\na = [int(x) for x in input().split()]\ntotal = sum(a)\nsum = 0\nb = 0\nc = 0\nfor i in range(0, n - 1):\n\tsum += a[i]\n\tif 3 * sum == 2 * total:\n\t\tc = c + b\n\tif 3 * sum == total:\n\t\tb = b + 1\nprint(c)\n", "import sys\nn = int(input())\na = [int(a) for a in input().split()]\ns = sum(a)\nif s % 3 != 0:\n\tprint(0)\n\tsys.exit()\ncounter = a[0]\nfirst = 0\nfirsts = [0]\nlasts = []\nfor i in range(1, n):\n\tif counter == s // 3:\n\t\tfirst += 1\n\tfirsts.append(first)\n\tif counter == 2 * s // 3:\n\t\tlasts.append(i)\n\tcounter += a[i]\nanswer = 0\nfor i in lasts:\n\tif i >= 2:\n\t\tanswer += firsts[i - 1]\nprint(answer)\n", "n = int(input())\ns = input().split()\nfor i in range(len(s)):\n\ts[i] = int(s[i])\nsm = sum(s)\nif sm % 3:\n\tprint(0)\nelse:\n\tsm /= 3\n\tq = []\n\tfor i in s:\n\t\tq.append(False)\n\tt = 0\n\tfor j in range(len(s)):\n\t\ti = -j - 1\n\t\tt += s[i]\n\t\tif t == sm:\n\t\t\tq[i] = True\n\tr = 0\n\tfor j in range(len(q)):\n\t\ti = -j - 1\n\t\tif q[i]:\n\t\t\tr += 1\n\t\tq[i] = r\n\tres = 0\n\tt = 0\n\tfor i in range(len(s) - 2):\n\t\tt += s[i]\n\t\tif t == sm:\n\t\t\tres += q[i + 2]\n\tprint(res)\n", "n = int(input())\nls = list(map(int, input().split()))\nfor i in range(1, n):\n\tls[i] += ls[i - 1]\n(ans, c) = (0, 0)\nfor i in range(n - 1):\n\tx = ls[i] * 3\n\tif x == ls[n - 1] * 2:\n\t\tans += c\n\tif x == ls[n - 1]:\n\t\tc += 1\nprint(ans)\n", "import math\n\ndef c(n, k):\n\tr = 1\n\tfor i in range(k):\n\t\tr *= n - i\n\treturn r // math.factorial(k)\nn = int(input())\narr = list(map(int, input().split()))\n(sumarr, count, s) = (sum(arr), 0, 0)\nif sumarr % 3:\n\tprint('0')\nelse:\n\tp = sumarr // 3\n\tif p == 0:\n\t\tdp = 0\n\t\tfor i in range(n - 1):\n\t\t\ts += arr[i]\n\t\t\tif s == 0:\n\t\t\t\tdp += 1\n\t\tprint(c(dp, 2))\n\telse:\n\t\t(dp0, dp1) = ([], [])\n\t\tfor i in range(n - 1):\n\t\t\ts += arr[i]\n\t\t\tif s == p:\n\t\t\t\tdp0.append(i)\n\t\t\telif s == 2 * p:\n\t\t\t\tdp1.append(i)\n\t\tf = 0\n\t\tfor i in dp1:\n\t\t\tcount += f\n\t\t\tfor j in dp0[f:]:\n\t\t\t\tif i > j:\n\t\t\t\t\tcount += 1\n\t\t\t\t\tf += 1\n\t\tprint(count)\n", "from bisect import bisect_left\nn = int(input())\na = list(map(int, input().split()))\nasum = sum(a)\nif asum % 3 == 0:\n\tpre = [0]\n\tfor ai in a:\n\t\tpre.append(pre[-1] + ai)\n\tdel pre[0]\n\tpre.append(0)\n\ti_a = []\n\tj_a = []\n\tfor k in range(n - 1):\n\t\tif pre[k] == asum // 3:\n\t\t\ti_a.append(k)\n\t\tif pre[k] == 2 * asum // 3:\n\t\t\tj_a.append(k)\n\tnumways = 0\n\tfor i in i_a:\n\t\tjmin = bisect_left(j_a, i + 1)\n\t\tnumways += len(j_a) - jmin\n\tprint(numways)\nelse:\n\tprint(0)\n", "import bisect\n\ndef main():\n\tn = int(input())\n\tarr = list(map(int, input().split()))\n\tsums = {}\n\ttotal = 0\n\tdp = []\n\tfor i in range(n - 1, -1, -1):\n\t\ttotal += arr[i]\n\t\tif total not in sums.keys():\n\t\t\tsums[total] = []\n\t\tdp.append(total)\n\t\tsums[total].append(i)\n\tfor s in sums.keys():\n\t\tsums[s].sort()\n\tdp.reverse()\n\tways = 0\n\tcurr = 0\n\tfor i in range(n - 1):\n\t\tcurr += arr[i]\n\t\tif dp[i + 1] == 2 * curr:\n\t\t\tif curr in sums.keys():\n\t\t\t\tindex = bisect.bisect(sums[curr], i + 1)\n\t\t\t\tways += len(sums[curr]) - index\n\tprint(ways)\nmain()\n", "import math\nn = int(input())\na = list(map(int, input().split()))\ntotal = sum(a)\nthird = int(total / 3)\n\ndef search(arr, l, r, val):\n\tans = -1\n\twhile l <= r:\n\t\tmid = math.ceil((l + r) / 2)\n\t\tif arr[mid] >= val:\n\t\t\tl = mid + 1\n\t\t\tans = mid\n\t\telse:\n\t\t\tr = mid - 1\n\treturn ans\nif n < 3 or total % 3 != 0:\n\tprint(0)\nelse:\n\tleft = []\n\tso_far = a[0]\n\tfor i in range(1, n - 1):\n\t\tif so_far == third:\n\t\t\tleft.append(i)\n\t\tso_far += a[i]\n\tright = []\n\ta.reverse()\n\tso_far = a[0]\n\tfor j in range(1, n - 1):\n\t\tif so_far == third:\n\t\t\tright.append(n - 1 - j)\n\t\tso_far += a[j]\n\tcounter = 0\n\tlen_right = len(right) - 1\n\tfor i in left:\n\t\tidx = search(right, 0, len_right, i)\n\t\tcounter += idx + 1\n\tprint(counter)\n", "n = int(input())\ninputlist = list(map(int, input().split()))\ns = 0\nfor j in inputlist:\n\ts += j\nm = s / 3\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tans = 0\n\ticount = 0\n\tm = int(s / 3)\n\tpossibleis = []\n\tpossiblejs = []\n\tsum = 0\n\tfor j in range(n - 1):\n\t\tsum += inputlist[j]\n\t\tif s - sum == m and j > 0:\n\t\t\tpossiblejs.append(j)\n\t\t\tans += icount\n\t\tif sum == m:\n\t\t\tpossibleis.append(j)\n\t\t\ticount += 1\n\tprint(ans)\n", "n = int(input())\narr = list(map(int, input().split()))\nc1 = 0\nsumm = sum(arr)\nif summ % 3 != 0:\n\tprint(0)\nelse:\n\tnum1 = summ // 3\n\tnum2 = num1 * 2\n\tsum = 0\n\tc1 = 0\n\tans = 0\n\tfor i in range(len(arr) - 1):\n\t\tsum += arr[i]\n\t\tif sum == num2:\n\t\t\tans += c1\n\t\tif sum == num1:\n\t\t\tc1 += 1\n\tprint(ans)\n", "n = int(input())\nt = list(map(int, input().split()))\ns = sum(t)\nif s % 3 != 0:\n\tprint(0)\nelse:\n\tcnt = [0] * n\n\ts = s // 3\n\tss = 0\n\tfor j in range(n - 1, -1, -1):\n\t\tss += t[j]\n\t\tif ss == s:\n\t\t\tcnt[j] = 1\n\tfor k in range(n - 2, -1, -1):\n\t\tcnt[k] += cnt[k + 1]\n\tss = 0\n\tans = 0\n\tfor i in range(0, n - 2):\n\t\tss += t[i]\n\t\tif ss == s:\n\t\t\tans += cnt[i + 2]\n\tprint(ans)\n", "n = int(input())\nlis = list(map(int, input().split()))\nsu = sum(lis)\nways = 0\nif su % 3 != 0 or n < 3:\n\tprint(0)\nelse:\n\tsu /= 3\n\t(a, b, c, temp) = (0, 0, 0, 0)\n\tfor i in range(len(lis) - 1):\n\t\ta += lis[i]\n\t\tif a == 2 * su:\n\t\t\tways += temp\n\t\tif a == su:\n\t\t\ttemp += 1\n\tprint(ways)\n", "inp = input().split(' ')\nn = inp[0]\nss = 0\na = []\ng = []\ninp1 = input().split(' ')\np = []\nz = 0\nfor i in range(int(n)):\n\ta.append(int(inp1[i]))\nans = 0\ns = 0\nfor i in range(len(a)):\n\ts += a[i]\nif s % 3 != 0:\n\tprint('0')\nelse:\n\ts //= 3\n\tss = 0\n\tk = 0\n\tl = 0\n\tfor n in a:\n\t\tk += n\n\t\tif s == 0:\n\t\t\tif k == 0:\n\t\t\t\tl += 1\n\t\t\t\tcontinue\n\t\tif k == s:\n\t\t\tl += 1\n\t\tif k == s * 2:\n\t\t\tans += l\n\tif s == 0:\n\t\tprint((l - 1) * (l - 2) // 2)\n\telse:\n\t\tprint(ans)\n", "from sys import stdin, stdout\ncin = stdin.readline\ncout = stdout.write\nmp = lambda : list(map(int, cin().split()))\n(n,) = mp()\na = mp()\nsumm = sum(a)\nif summ % 3 or n < 3:\n\tcout('0')\nelse:\n\tx = summ // 3\n\tl = [0] * 3\n\toneThird = (0, 1)[a[0] == x]\n\tsumm = a[0]\n\tans = 0\n\tfor i in range(1, n - 1):\n\t\tsumm += a[i]\n\t\tif summ == x * 2:\n\t\t\tans += oneThird\n\t\tif summ == x:\n\t\t\toneThird += 1\n\tcout(str(ans))\n", "import sys\nimport os\nfrom io import BytesIO, IOBase\n\ndef cel(n, k):\n\treturn n // k + (n % k != 0)\n\ndef ii():\n\treturn int(input())\n\ndef mi():\n\treturn map(int, input().split())\n\ndef li():\n\treturn list(map(int, input().split()))\n\ndef lcm(a, b):\n\treturn abs(a * b) // math.gcd(a, b)\n\ndef prr(a, sep=' '):\n\tprint(sep.join(map(str, a)))\n\ndef dd():\n\treturn defaultdict(int)\n\ndef ddl():\n\treturn defaultdict(list)\n\ndef ddd():\n\treturn defaultdict(defaultdict(int))\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nfrom collections import defaultdict\nn = ii()\na = li()\nsm = sum(a)\nif sm % 3 != 0 or n < 3:\n\tprint(0)\n\tsys.exit()\ntmp = 0\ns = 0\nans = 0\nfor i in range(n - 1):\n\ts += a[i]\n\tif s == 2 * sm // 3:\n\t\tans += tmp\n\tif s == sm // 3:\n\t\ttmp += 1\nprint(ans)\n", "from math import inf as inf\nfrom math import *\nfrom collections import *\nimport sys\ninput = sys.stdin.readline\nt = 1\nwhile t:\n\tt -= 1\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\tpre = 0\n\ts = 0\n\tr = 0\n\tx = sum(a)\n\tif x % 3:\n\t\tprint(0)\n\t\texit(0)\n\tre = x // 3\n\tfor i in range(n - 1):\n\t\tpre += a[i]\n\t\tif pre == 2 * re:\n\t\t\tr += s\n\t\tif pre == re:\n\t\t\ts += 1\n\tprint(r)\n", "from itertools import accumulate, permutations\nR = lambda : map(int, input().split())\nn = int(input())\narr = list(R())\nsm = sum(arr)\nlarr = list(accumulate((3 * x == sm for x in accumulate(arr))))\nrsm = 0\nres = 0\nfor i in range(n - 1, 1, -1):\n\trsm += arr[i]\n\tif rsm * 3 == sm:\n\t\tres += larr[i - 2]\nprint(res)\n", "n = int(input())\nl = list(map(int, input().split()))\na = [l[0]]\nd = {}\nm = 0\nx = 0\nch = 1\nfor i in range(1, n):\n\ta.append(l[i] + a[i - 1])\nj = a[-1]\nif j == 0:\n\tfor i in a:\n\t\tif i == 0:\n\t\t\tx += 1\n\tp = (x - 1) * (x - 2)\n\tp //= 2\n\tm += p\nelif j % 3 == 0:\n\tj //= 3\n\tfor i in a:\n\t\tif i == j:\n\t\t\tx += 1\n\t\tif i == 2 * j:\n\t\t\tm += x\nprint(m)\n", "from collections import deque, Counter, OrderedDict\nfrom heapq import nsmallest, nlargest\nfrom math import ceil, floor, log, log2, sqrt, gcd, factorial, pow, pi\n\ndef binNumber(n, size=4):\n\treturn bin(n)[2:].zfill(size)\n\ndef iar():\n\treturn list(map(int, input().split()))\n\ndef ini():\n\treturn int(input())\n\ndef isp():\n\treturn map(int, input().split())\n\ndef sti():\n\treturn str(input())\n\ndef par(a):\n\tprint(' '.join(list(map(str, a))))\n\ndef tdl(outerListSize, innerListSize, defaultValue=0):\n\treturn [[defaultValue] * innerListSize for i in range(outerListSize)]\n\ndef sts(s):\n\ts = list(s)\n\ts.sort()\n\treturn ''.join(s)\n\nclass pair:\n\n\tdef __init__(self, f, s):\n\t\tself.fi = f\n\t\tself.se = s\n\n\tdef __lt__(self, other):\n\t\treturn (self.fi, self.se) < (other.fi, other.se)\nn = ini()\na = iar()\ns = sum(a)\nif s % 3 != 0:\n\tprint(0)\n\tquit()\ns = s // 3\n(ans, cnt) = (0, 0)\nx = 0\nfor i in range(n - 1):\n\tx += a[i]\n\tif x == 2 * s:\n\t\tans += cnt\n\tif x == s:\n\t\tcnt += 1\nprint(ans)\n", "def solve(n, l):\n\ts = sum(l)\n\tif s % 3 != 0:\n\t\treturn 0\n\telse:\n\t\tt = s // 3\n\t\t(i_fi, i_se) = ([], [])\n\t\tps = 0\n\t\tfor i in range(n - 1):\n\t\t\tps += l[i]\n\t\t\tif ps == t * 1:\n\t\t\t\ti_fi.append(i)\n\t\t\tif ps == t * 2:\n\t\t\t\ti_se.append(i)\n\t\tif t == 0:\n\t\t\treturn len(i_fi) * (len(i_fi) - 1) // 2\n\t\telse:\n\t\t\tout = 0\n\t\t\ti = 0\n\t\t\tfor x in i_fi:\n\t\t\t\twhile i < len(i_se) and i_se[i] <= x:\n\t\t\t\t\ti += 1\n\t\t\t\tout += len(i_se) - i\n\t\t\treturn out\nn = int(input())\nl = list(map(int, input().split()))\nprint(solve(n, l))\n", "n = int(input())\na = list(map(int, input().strip().split(' ')))\nsum_part = [a[0]] * n\nfor i in range(1, n):\n\tsum_part[i] = sum_part[i - 1] + a[i]\nsuma = sum_part[n - 1]\nif n < 3:\n\tprint(0)\nelif suma % 3 != 0:\n\tprint(0)\nelse:\n\t(q, ans) = (0, 0)\n\tfor j in range(n - 1):\n\t\tif 3 * sum_part[j] == 2 * suma:\n\t\t\tans += q\n\t\tif 3 * sum_part[j] == suma:\n\t\t\tq += 1\n\tprint(ans)\n", "n = int(input())\na = list(map(int, input().split()))\ns = []\nsi = 0\nfor i in range(n):\n\tsi += a[i]\n\ts.append(si)\nif s[-1] % 3 != 0 or n <= 2:\n\tprint(0)\nelif s[n - 1] != 0:\n\tl = [0] * n\n\tfor j in range(n):\n\t\tif s[j] == s[n - 1] // 3:\n\t\t\tl[j] = 1\n\t\tif s[j] == s[n - 1] * 2 // 3:\n\t\t\tl[j] = 2\n\tp = 0\n\tq = 0\n\tfor k in range(n):\n\t\tif l[-1 - k] == 2:\n\t\t\tp += 1\n\t\tif l[-1 - k] == 1:\n\t\t\tq += p\n\tprint(q)\nelse:\n\tc = s.count(0) - 1\n\tprint(c * (c - 1) // 2)\n", "n = int(input())\narr = [int(i) for i in input().split()]\ntotal = sum(arr)\nsplits = 0\nk = 0\nl = 0\na = []\nb = []\nif total % 3 != 0:\n\tpass\nelse:\n\tfor i in range(n):\n\t\tk += arr[i]\n\t\tif k == total / 3 and i != n - 1:\n\t\t\ta.append(i)\n\t\tif k == 2 * total / 3 and i != n - 1:\n\t\t\tb.append(i)\n\tfor i in a:\n\t\twhile l < len(b) and i >= b[l]:\n\t\t\tl += 1\n\t\tsplits += len(b) - l\nprint(splits)\n", "n = int(input())\nalist = [int(x) for x in input().split()]\npsum = 0\nl = 0\nlim = sum(alist) // 3\nways = 0\nif sum(alist) % 3 != 0:\n\tprint(0)\n\tquit()\nfor i in range(n - 1):\n\tpsum += alist[i]\n\tif psum == 2 * lim:\n\t\tways += l\n\tif psum == lim:\n\t\tl += 1\nprint(ways)\n", "n = int(input())\na = list(map(int, input().split()))\ns = 0\nfor i in a:\n\ts += i\nif s % 3 == 0:\n\t(ans, num, one) = (0, 0, 0)\n\tfor i in range(0, len(a)):\n\t\tnum += a[i]\n\t\tif num == int(2 * s / 3) and i > 0 and (i < n - 1):\n\t\t\tans += one\n\t\tif num == int(s / 3):\n\t\t\tone += 1\n\tprint(ans)\nelse:\n\tprint(0)\n", "n = int(input())\na = list((int(i) for i in input().split()))\ns = sum(a)\nans = 0\nx = 0\nif s % 3 == 0:\n\tc = s // 3\n\tb = s - c\n\td = 0\n\tfor i in range(n - 1):\n\t\tx = x + a[i]\n\t\tif x == b:\n\t\t\tans += d\n\t\tif x == c:\n\t\t\td += 1\nprint(ans)\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\nflag = [0 for i in range(n)]\nc = [0 for i in range(n)]\nif s % 3 != 0:\n\tprint(0)\nelse:\n\ts = s // 3\n\tt = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tt += a[i]\n\t\tif t == s:\n\t\t\tflag[i] = 1\n\tc[n - 1] = flag[n - 1]\n\tfor i in range(n - 2, -1, -1):\n\t\tc[i] = c[i + 1] + flag[i]\n\tt = 0\n\tans = 0\n\tfor i in range(n - 2):\n\t\tt += a[i]\n\t\tif t == s:\n\t\t\tans += c[i + 2]\n\tprint(ans)\n", "import bisect\nn = int(input())\na = [int(x) for x in input().split()]\n(s, p, k) = (sum(a), a[0], sum(a) / 3)\n(b, c) = ([], [])\nfor i in range(len(a)):\n\tif s == k:\n\t\tc.append(i)\n\tif p == k:\n\t\tb.append(i)\n\ts -= a[i]\n\tif i < n - 1:\n\t\tp += a[i + 1]\nif len(b) != 0 and b[-1] == n - 1:\n\tb = b[:-1]\nif len(c) != 0 and c[0] == 0:\n\tc = c[1:]\nres = 0\ne = len(c)\nfor i in b:\n\tres += e - bisect.bisect_left(c, i + 2)\nprint(res)\n", "n = int(input())\na = list(map(int, input().split()))\ns = sum(a)\nc = [0] * n\nif n <= 2 or s % 3:\n\tprint(0)\nelse:\n\t(cnt1, cnt2) = (0, 0)\n\ts1 = s // 3\n\ts2 = 2 * s1\n\tcs = 0\n\tout = 0\n\tfor i in range(len(a)):\n\t\tcs += a[i]\n\t\tif i < n - 1 and cs == s2:\n\t\t\tout += cnt1\n\t\tif cs == s1:\n\t\t\tcnt1 += 1\n\tprint(out)\n", "from itertools import accumulate\nn = int(input())\na = list(map(int, input().split()))\nr_a = list(reversed(a))\nif sum(a) % 3 != 0 or n < 3:\n\tprint(0)\nelse:\n\ts = sum(a) // 3\n\tans = 0\n\tc = list(reversed(list(accumulate([1 if e == s else 0 for e in accumulate(r_a)]))))\n\tfor (i, e) in enumerate(accumulate(a)):\n\t\tif e == s:\n\t\t\tif i + 2 < n:\n\t\t\t\tans += c[i + 2]\n\tprint(ans)\n", "n = int(input())\ns = list(map(int, input().split()))\nsu = sum(s)\np = a = b = 0\nif su % 3:\n\tprint(0)\nelse:\n\tm = su / 3\n\tmm = m * 2\n\tfor i in range(n - 1):\n\t\tp += s[i]\n\t\tif p == mm:\n\t\t\tb += a\n\t\tif p == m:\n\t\t\ta += 1\n\tprint(b)\n", "import sys\nn = int(sys.stdin.readline())\na = list(map(int, sys.stdin.readline().split(' ')))\n\ndef countways(a, n):\n\tcnt = [0 for i in range(n)]\n\ts = 0\n\ts = sum(a)\n\tif s % 3 != 0:\n\t\treturn 0\n\ts //= 3\n\tss = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tss += a[i]\n\t\tif ss == s:\n\t\t\tcnt[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tcnt[i] += cnt[i + 1]\n\tans = 0\n\tss = 0\n\tfor i in range(0, n - 2):\n\t\tss += a[i]\n\t\tif ss == s:\n\t\t\tans += cnt[i + 2]\n\treturn ans\nprint(countways(a, n))\n", "n = int(input())\nA = list(map(int, input().split()))\nT = [0 for _ in range(n)]\nT[n - 1] = A[n - 1]\nI = []\nG = [0 for _ in range(n)]\nfor i in range(n - 2, -1, -1):\n\tT[i] = T[i + 1] + A[i]\nif T[0] % 3:\n\tprint(0)\nelse:\n\tfor i in range(2, n):\n\t\tif T[i] == T[0] // 3:\n\t\t\tG[i] = 1\n\tT[0] = A[0]\n\tfor i in range(1, n):\n\t\tT[i] = T[i - 1] + A[i]\n\tfor i in range(n - 2):\n\t\tif T[i] == T[n - 1] // 3:\n\t\t\tI += [i]\n\ts = [0 for _ in range(n)]\n\ts[n - 1] = G[n - 1]\n\tfor i in range(n - 2, -1, -1):\n\t\ts[i] = s[i + 1] + G[i]\n\tprint(sum([s[i + 2] for i in I]))\n", "n = int(input())\na = [int(x) for x in input().split(' ')]\ns = sum(a)\nif s % 3 == 0:\n\t(part_1, part_2) = (0, 0)\n\tcurr_sum = 0\n\tfor i in range(len(a) - 1):\n\t\tcurr_sum += a[i]\n\t\tif curr_sum == 2 * s // 3:\n\t\t\tpart_2 += part_1\n\t\tif curr_sum == s // 3:\n\t\t\tpart_1 += 1\n\tprint(part_2)\nelse:\n\tprint(0)\n", "n = int(input())\na = list(map(int, input().split()))\nprefixSum = [0] * n\nsuffixSum = [0] * n\nprefixSum[0] = a[0]\nsuffixSum[n - 1] = a[n - 1]\nsumTotal = 0\ni = 1\nj = n - 2\nwhile i < n:\n\tprefixSum[i] = prefixSum[i - 1] + a[i]\n\tsuffixSum[j] = suffixSum[j + 1] + a[j]\n\tj -= 1\n\ti += 1\nsumPart = prefixSum[n - 1] // 3\nstartIndices = [0] * n\nfor i in range(n):\n\tif i == 0:\n\t\tif a[0] == sumPart:\n\t\t\tstartIndices[0] = 1\n\t\telse:\n\t\t\tstartIndices[0] = 0\n\telse:\n\t\tstartIndices[i] = startIndices[i - 1]\n\t\tif prefixSum[i] == sumPart:\n\t\t\tstartIndices[i] += 1\nanswer = 0\nfor j in range(n - 1, 1, -1):\n\tif suffixSum[j] == sumPart:\n\t\tanswer += startIndices[j - 2]\nif sumPart * 3 == prefixSum[n - 1]:\n\tprint(answer)\nelse:\n\tprint(0)\n", "n = int(input())\narr = list(map(int, input().split()))\nfwd = []\ns = 0\nfor i in arr:\n\ts = s + i\n\tfwd.append(s)\ns = sum(arr)\nif s % 3 != 0 or n <= 2:\n\tprint(0)\nelif s == 0:\n\tcc = fwd.count(0)\n\tif cc == 3:\n\t\tprint(1)\n\telif cc > 3:\n\t\tcc = cc - 2\n\t\tprint(int(cc / 2 * (cc + 1)))\n\telse:\n\t\tprint(0)\nelse:\n\tans = 0\n\tcount = 0\n\tflag = 0\n\tfor i in range(0, n - 1):\n\t\tif fwd[i] == s / 3 and flag == 1:\n\t\t\tcount = 1\n\t\t\tflag = 0\n\t\telif fwd[i] == 2 * s / 3:\n\t\t\tans = ans + count\n\t\t\tflag = 1\n\t\tif fwd[i] == s / 3:\n\t\t\tcount += 1\n\tprint(ans)\n", "n = int(input())\nif n < 3:\n\tprint(0)\n\texit()\nl = [int(x) for x in input().split()]\nsl = sum(l)\ns = int(sl / 3)\nif 3 * s != sl:\n\tprint(0)\n\texit()\n(a, c) = (l[0], l[-1])\nb = 3 * s - a - c\ntotal = 0\nleft = []\nsum = 0\nfor x in range(n - 2):\n\tsum += l[x]\n\tif sum == s:\n\t\tleft += [x]\nif len(left) == 0:\n\tprint(0)\n\texit()\nsum = 0\nfor y in range(n - 1, 1, -1):\n\tsum += l[y]\n\tif sum == s:\n\t\twhile len(left) > 0 and y - left[-1] <= 1:\n\t\t\tleft.pop()\n\t\tif len(left) == 0:\n\t\t\tbreak\n\t\ttotal += len(left)\nprint(total)\n", "from sys import stdin\nn = int(input())\na = list(map(int, stdin.readline().rstrip().split(' ')))\nr = [0] * n\ns = 0\nz = sum(a)\nif z % 3:\n\tprint(0)\nelse:\n\tz = z // 3\n\tfor i in range(n - 1, -1, -1):\n\t\ts += a[i]\n\t\tif s == z:\n\t\t\tr[i] = 1\n\tfor i in range(n - 2, -1, -1):\n\t\tr[i] += r[i + 1]\n\tans = 0\n\tss = 0\n\tfor i in range(n - 2):\n\t\tss += a[i]\n\t\tif ss == z:\n\t\t\tans += r[i + 2]\n\tprint(ans)\n", "n = int(input())\nnums = list(map(int, input().split()))\nt = sum(nums)\nc = 0\no = 0\nif t % 3 != 0:\n\tprint(0)\nelse:\n\ts = 0\n\tfor i in range(n - 1):\n\t\ts += nums[i]\n\t\tif s * 3 == t * 2:\n\t\t\to += c\n\t\tif s * 3 == t and i != n - 2:\n\t\t\tc += 1\n\tprint(o)\n", "def calc(arr, n, total):\n\tif n < 2 or total % 3:\n\t\treturn 0\n\ts = total // 3\n\ts2 = s * 2\n\tret = 0\n\tt = 0\n\tc = 0\n\tfor num in arr[:-1]:\n\t\tc += num\n\t\tif c == s2:\n\t\t\tret += t\n\t\tif c == s:\n\t\t\tt += 1\n\treturn ret\nn = int(input())\narr = list(map(int, input().split()))\ntotal = sum(arr)\nprint(calc(arr, n, total))\n", "n = int(input())\nli = list(map(int, input().split()))\ns = sum(li) / 3\npresum = 0\nans = 0\ncnt = 0\nfor i in range(len(li) - 1):\n\tpresum += li[i]\n\tif presum == 2 * s:\n\t\tans += cnt\n\tif presum == s:\n\t\tcnt += 1\nprint(ans)\n", "n = int(input())\narr = list(map(int, input().split()))\nsums = sum(arr)\ntwo = 0\nif sums % 3 == 0:\n\ts = sums / 3\n\tcur = 0\n\tk = s * 2\n\tone = 0\n\tfor x in arr[:-1]:\n\t\tcur = cur + x\n\t\tif cur == k:\n\t\t\ttwo += one\n\t\tif cur == s:\n\t\t\tone += 1\nprint(two)\n", "l = int(input())\na = list(map(int, input().split()))\n\ndef res():\n\ts = sum(a)\n\tif s % 3 != 0:\n\t\treturn 0\n\telse:\n\t\ts3 = s // 3\n\t\tr = set()\n\t\trsum = 0\n\t\tfor i in range(l - 1, 0, -1):\n\t\t\trsum += a[i]\n\t\t\tif rsum == s3:\n\t\t\t\tr.add(i)\n\t\tlsum = 0\n\t\tans = 0\n\t\tfor i in range(l):\n\t\t\tlsum += a[i]\n\t\t\tif i + 1 in r:\n\t\t\t\tr.remove(i + 1)\n\t\t\tif lsum == s3:\n\t\t\t\tans += len(r)\n\t\treturn ans\nprint(res())\n", "n = int(input())\narr = list(map(int, input().split(' ')))\nsumArr = []\nmSum = 0\nfor (index, elem) in enumerate(arr):\n\tmSum += elem\n\tsumArr.append((index, mSum))\nmSum = sumArr[-1][1]\nif mSum % 3 != 0:\n\tprint(0)\nelse:\n\tmSum = int(mSum / 3)\n\ttheSum = 0\n\tcnt = 0\n\tcntArr = []\n\tmlen = len(arr)\n\tfor i in range(mlen):\n\t\ttheSum += arr[mlen - 1 - i]\n\t\tif theSum == mSum:\n\t\t\tcnt += 1\n\t\tcntArr.append(cnt)\n\tsumArr = filter(lambda x: x[1] == mSum, sumArr)\n\ttheSum = 0\n\tvariants = 0\n\tfor elem in sumArr:\n\t\tif elem[0] < mlen - 2:\n\t\t\tvariants += cntArr[mlen - elem[0] - 3]\n\tprint(variants)\n", "n = int(input())\narr = list(map(int, input().split()))\ns = sum(arr)\nw = 0\nif not s % 3:\n\ts = s / 3\n\tt = s * 2\n\t(u, g) = (0, 0)\n\tfor x in arr[:-1]:\n\t\tu += x\n\t\tif u == t:\n\t\t\tw += g\n\t\tif u == s:\n\t\t\tg += 1\nprint(w)\n", "n = int(input())\na = [int(x) for x in input().split()]\ns = sum(a)\nif s % 3 != 0:\n\tprint(0)\nelse:\n\ts1 = s / 3\n\ts2 = 2 * s1\n\ttick1 = tick2 = num = 0\n\tfor i in range(n - 1):\n\t\ttick2 += a[i]\n\t\tif tick2 == s2:\n\t\t\tnum += tick1\n\t\tif tick2 == s1:\n\t\t\ttick1 += 1\n\tprint(num)\n", "def fun(n):\n\treturn int(n * (n + 1) // 2)\nn = int(input())\narr = [int(x) for x in input().split()]\nif sum(arr) % 3 == 0:\n\tth = int(sum(arr) / 3)\n\tpre = [arr[0]]\n\tsuf = [arr[-1]]\n\tfor i in range(1, n):\n\t\tpre.append(pre[-1] + arr[i])\n\tfor i in range(n - 2, -1, -1):\n\t\tsuf.append(suf[-1] + arr[i])\n\tsuf = suf[::-1]\n\tpreT = [0]\n\tif pre[0] == th:\n\t\tpreT[0] = 1\n\tsufT = [0]\n\tif suf[-1] == th:\n\t\tsufT[0] = 1\n\tfor i in range(1, n):\n\t\tpreT.append(preT[-1])\n\t\tif pre[i] == th:\n\t\t\tpreT[-1] += 1\n\tfor i in range(n - 2, -1, -1):\n\t\tsufT.append(sufT[-1])\n\t\tif suf[i] == th:\n\t\t\tsufT[-1] += 1\n\tsufT = sufT[::-1]\n\tways = 0\n\tfor i in range(n - 2):\n\t\tif pre[i] == th:\n\t\t\tways += sufT[i + 2]\n\tprint(ways)\nelse:\n\tprint(0)\n", "def main():\n\timport sys\n\tn = int(sys.stdin.readline())\n\tt = tuple(map(int, sys.stdin.readline().split()))\n\tacc = sum(t)\n\tif acc % 3:\n\t\tsys.stdout.write('0')\n\telse:\n\t\tcnt = []\n\t\tconst = acc // 3\n\t\tss = 0\n\t\tfor i in t:\n\t\t\tif acc - ss == const:\n\t\t\t\tcnt.append(1)\n\t\t\telse:\n\t\t\t\tcnt.append(0)\n\t\t\tss += i\n\t\tfor i in range(n - 2, -1, -1):\n\t\t\tcnt[i] += cnt[i + 1]\n\t\tans = 0\n\t\tss = 0\n\t\tfor i in range(n - 2):\n\t\t\tss += t[i]\n\t\t\tif ss == const:\n\t\t\t\tans += cnt[i + 2]\n\t\tsys.stdout.write(str(ans) + '\\n')\nmain()\n", "def main():\n\tfrom sys import stdin, stdout\n\tit = iter(map(int, stdin.read().split()))\n\tn = next(it)\n\tcumSum = [0] * (n + 3)\n\tfor i in range(1, n + 1):\n\t\tcumSum[i] = cumSum[i - 1] + next(it)\n\ttotSum = cumSum[n]\n\tif n < 3 or totSum % 3 != 0:\n\t\tans = 0\n\telif totSum == 0:\n\t\tzeroCnt = 0\n\t\tfor x in cumSum[1:n + 1]:\n\t\t\tif x == 0:\n\t\t\t\tzeroCnt += 1\n\t\tzeroCnt -= 2\n\t\tans = int(zeroCnt * (zeroCnt + 1) / 2) if zeroCnt > 0 else 0\n\telse:\n\t\tsegSum = totSum // 3\n\t\tsegSumTwice = segSum * 2\n\t\tcnt = 0\n\t\tans = 0\n\t\tfor i in range(1, n):\n\t\t\tif cumSum[i] == segSum:\n\t\t\t\tcnt += 1\n\t\t\tif cumSum[i] == segSumTwice:\n\t\t\t\tans += cnt\n\tstdout.write(str(ans) + '\\n')\nmain()\n", "n = int(input())\narr = list(map(int, input().split()))\nif sum(arr) % 3 != 0:\n\tprint(0)\nelse:\n\ts = sum(arr) // 3\n\tcnt = 0\n\tanswer = 0\n\tsum1 = 0\n\tfor i in range(0, n - 1):\n\t\tsum1 = sum1 + arr[i]\n\t\tif sum1 == 2 * s:\n\t\t\tanswer = answer + cnt\n\t\tif sum1 == s:\n\t\t\tcnt = cnt + 1\n\tprint(answer)\n", "import sys, math, itertools, random, bisect\nfrom collections import defaultdict\nINF = 10 ** 18\n\ndef get_ints():\n\treturn map(int, sys.stdin.readline().strip().split())\n\ndef get_array():\n\treturn list(map(int, sys.stdin.readline().strip().split()))\n\ndef input():\n\treturn sys.stdin.readline().strip()\nmod = 10 ** 9 + 7\ntest = 1\nfor _ in range(test):\n\t(n,) = get_ints()\n\ta = get_array()\n\ttot = sum(a)\n\tif tot % 3 != 0:\n\t\tprint(0)\n\telse:\n\t\ttot = tot // 3\n\t\tcur = 0\n\t\tcnt = [0] * n\n\t\tfor i in range(n - 1, -1, -1):\n\t\t\tcur += a[i]\n\t\t\tif cur == tot:\n\t\t\t\tcnt[i] = 1\n\t\tfor i in range(n - 2, -1, -1):\n\t\t\tcnt[i] += cnt[i + 1]\n\t\tans = 0\n\t\tcur = 0\n\t\tfor i in range(n - 2):\n\t\t\tcur += a[i]\n\t\t\tif cur == tot:\n\t\t\t\tans += cnt[i + 2]\n\t\tprint(ans)\n", "n = int(input())\na = [0]\na.extend([int(x) for x in input().split()])\nprefix_sum = [0] * (n + 1)\nfor i in range(1, n + 1):\n\tprefix_sum[i] = prefix_sum[i - 1] + a[i]\nif prefix_sum[n] % 3 != 0:\n\tprint(0)\nelse:\n\ts = ans = 0\n\tfor i in range(1, n + 1):\n\t\tif i > 1 and i < n and (prefix_sum[i] == prefix_sum[n] * 2 // 3):\n\t\t\tans += s\n\t\tif prefix_sum[i] == prefix_sum[n] // 3:\n\t\t\ts += 1\n\tprint(ans)\n", "n = int(input())\nc = list(map(int, input().split()))\nk = sum(c)\nif k % 3 == 0 and n >= 3:\n\tk = k // 3\n\ta = [0] * n\n\tb = [0] * n\n\ts = 0\n\tfor i in range(n - 1, 1, -1):\n\t\ts += c[i]\n\t\tif s == k:\n\t\t\ta[i] = 1\n\tb[-1] = a[-1]\n\tfor i in range(n - 2, -1, -1):\n\t\tb[i] = b[i + 1] + a[i]\n\ts = 0\n\tl = 0\n\tfor i in range(n - 2):\n\t\ts += c[i]\n\t\tif s == k:\n\t\t\tl += b[i + 2]\n\tprint(l)\nelse:\n\tprint(0)\n", "n = int(input())\ns = list(map(int, input().split()))\nsum = 0\nfor i in s:\n\tsum += i\nif sum % 3 != 0:\n\tprint(0)\nelse:\n\tx = sum // 3\n\tans = 0\n\tv = 0\n\ty = [0] * n\n\ty[0] = s[0]\n\tfor i in range(1, n):\n\t\ty[i] = y[i - 1] + s[i]\n\tfor i in range(n - 1):\n\t\tif y[i] == sum - x:\n\t\t\tans += v\n\t\tif y[i] == x:\n\t\t\tv += 1\n\tprint(ans)\n", "n = int(input())\nc = list(map(int, input().split()))\nF = []\nF.append(c[0])\nfor i in range(1, n):\n\tF.append(c[i] + F[i - 1])\nsum = F[n - 1]\nif sum % 3 != 0:\n\tprint(0)\nelif sum != 0:\n\ttemp = 0\n\tcount = 0\n\tfor i in range(0, n - 2):\n\t\tif F[i] == sum // 3:\n\t\t\ttemp = temp + 1\n\t\tif F[i] == 2 * sum // 3:\n\t\t\tcount = count + temp\n\tif F[n - 2] == 2 * sum // 3:\n\t\tcount = count + temp\n\tprint(count)\nelse:\n\ttemp = 0\n\tfor i in range(0, n - 1):\n\t\tif F[i] == 0:\n\t\t\ttemp = temp + 1\n\ttemp = temp * (temp - 1) // 2\n\tprint(temp)\n", "n = int(input())\narr = list(map(int, input().split()))\npsum = [0] * n\nt = 0\nfor i in range(n):\n\tt += arr[i]\n\tpsum[i] = t\nif psum[-1] % 3 != 0:\n\tprint(0)\nelif psum[-1] == 0:\n\tcnt = psum.count(0)\n\tcnt -= 1\n\tif cnt < 2:\n\t\tprint(0)\n\telif cnt == 2:\n\t\tprint(1)\n\telse:\n\t\tprint(cnt * (cnt - 1) // 2)\nelse:\n\tans = 0\n\tx = psum[-1] // 3\n\tx2 = x * 2\n\tx3 = x * 3\n\t(xc, x2c, x3c) = (0, 0, 0)\n\tfor i in range(n):\n\t\tif psum[i] == x:\n\t\t\txc += 1\n\t\tif psum[i] == x2:\n\t\t\tans += xc\n\tprint(ans)\n", "pp = int(input())\na = [int(i) for i in input().split()]\nb = [0 for i in range(pp)]\ns = 0\nk = sum(a)\na1 = k // 3\ns = 0\nss = 0\nsss = 0\nif k % 3 == 0:\n\tfor i in range(pp - 1):\n\t\ts = s + a[i]\n\t\tif s == 2 * a1:\n\t\t\tss = sss + ss\n\t\tif s == a1:\n\t\t\tsss = sss + 1\nprint(ss)\n", "n = int(input())\na = []\np = 0\nfor i in map(int, input().split()):\n\ta.append(p + i)\n\tp += i\ncnt = 0\ncnt = 0\nans = 0\nfor i in range(n - 1):\n\tif a[i] * 3 == a[n - 1] * 2:\n\t\tans += cnt\n\tif a[i] * 3 == a[n - 1]:\n\t\tcnt += 1\nprint(ans)\n", "from itertools import accumulate\nn = int(input())\n(a, ans, c) = (list(accumulate(map(int, input().split()))), 0, 0)\nif a[n - 1] % 3 == 0:\n\tfor i in range(n - 1):\n\t\tif a[i] == 2 * a[n - 1] // 3:\n\t\t\tans += c\n\t\tif a[i] == a[n - 1] // 3:\n\t\t\tc += 1\nprint(ans)\n", "n = input()\na = [int(i) for i in input().split()]\nk = 0\nb = 0\nc = 0\ns = 0\nif sum(a) % 3 == 0:\n\tk = sum(a) / 3\n\tfor i in a[:-1]:\n\t\ts += i\n\t\tif s == 2 * k:\n\t\t\tb += c\n\t\tif s == k:\n\t\t\tc += 1\nprint(b)\n", "def main():\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\ts = sum(a)\n\tif s % 3 != 0:\n\t\tprint(0)\n\t\treturn\n\telse:\n\t\tcnt = [0 for _ in range(n)]\n\t\tcnts = 0\n\t\tfor i in range(n - 1, -1, -1):\n\t\t\tcnts += a[i]\n\t\t\tif cnts == s // 3:\n\t\t\t\tcnt[i] = 1\n\t\tfor i in range(n - 2, -1, -1):\n\t\t\tcnt[i] += cnt[i + 1]\n\t\tcnts = 0\n\t\tans = 0\n\t\tfor i in range(n):\n\t\t\tcnts += a[i]\n\t\t\tif i + 2 < n and cnts == s // 3:\n\t\t\t\tans += cnt[i + 2]\n\t\tprint(ans)\n\t\treturn\nmain()\n", "n = int(input())\na = list(map(int, input().split()))\nif sum(a) % 3 != 0:\n\tprint(0)\nelse:\n\treq = sum(a) // 3\n\t(f, s) = ([], [])\n\tpref = [0] * n\n\tfor i in range(n):\n\t\tif i == 0:\n\t\t\tpref[i] = a[i]\n\t\telse:\n\t\t\tpref[i] = pref[i - 1] + a[i]\n\t\tif pref[i] == req:\n\t\t\tf.append(i)\n\t\tif pref[i] == 2 * req and i != n - 1:\n\t\t\ts.append(i)\n\t(i, j, ans) = (0, 0, 0)\n\twhile i < len(f):\n\t\twhile j < len(s) and s[j] <= f[i]:\n\t\t\tj += 1\n\t\tans += len(s) - j\n\t\ti += 1\n\tprint(ans)\n", "import sys\ninf = float('inf')\n(mod, MOD) = (1000000007, 998244353)\n\ndef get_array():\n\treturn list(map(int, sys.stdin.readline().strip().split()))\n\ndef get_ints():\n\treturn map(int, sys.stdin.readline().strip().split())\n\ndef input():\n\treturn sys.stdin.readline().strip()\nn = int(input())\nArr = get_array()\nsumx = sum(Arr)\nans = 0\nif sumx % 3 == 0:\n\ts = 0\n\tcount = 0\n\tlsum = sumx // 3\n\trsum = 2 * lsum\n\tfor i in Arr[:-1]:\n\t\ts += i\n\t\tif s == rsum:\n\t\t\tans += count\n\t\tif s == lsum:\n\t\t\tcount += 1\nprint(ans)\n", "from __future__ import division, print_function\nimport os\nimport sys\nfrom io import BytesIO, IOBase\n\ndef main():\n\tn = int(input())\n\tnumbers = tuple(map(lambda x: int(x), input().split()))\n\tsuffix = [0] * n\n\taccumulated = 0\n\tfor i in range(n - 1, -1, -1):\n\t\taccumulated += numbers[i]\n\t\tsuffix[i] = accumulated\n\tnumOfWays = 0\n\taccumulated = numbers[0]\n\tamount = {accumulated: 1}\n\tfor i in range(1, n - 1):\n\t\taccumulated += numbers[i]\n\t\tif accumulated / 2 == suffix[i + 1] and accumulated / 2 in amount:\n\t\t\tnumOfWays += amount[accumulated / 2]\n\t\tif accumulated in amount:\n\t\t\tamount[accumulated] += 1\n\t\telse:\n\t\t\tamount[accumulated] = 1\n\tprint(numOfWays)\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\n\ndef print(*args, **kwargs):\n\tsep = kwargs.pop('sep', ' ')\n\tfile = kwargs.pop('file', sys.stdout)\n\tatStart = True\n\tfor x in args:\n\t\tif not atStart:\n\t\t\tfile.write(sep)\n\t\tfile.write(str(x))\n\t\tatStart = False\n\tfile.write(kwargs.pop('end', '\\n'))\n\tif kwargs.pop('flush', False):\n\t\tfile.flush()\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\nmain()\n", "n = int(input())\nL = list(map(int, input().split(' ')))\nans = 0\nfor i in range(n - 1):\n\tL[i + 1] = L[i + 1] + L[i]\nif L[n - 1] % 3 == 0:\n\tc = 0\n\ts = L[n - 1] // 3\n\tv = 2 * s\n\tans = 0\n\tfor i in range(0, n - 1):\n\t\tif L[i] == v:\n\t\t\tans = ans + c\n\t\tif L[i] == s:\n\t\t\tc = c + 1\nprint(ans)\n", "def find_f(j, t, start):\n\tfor i in range(start, len(t)):\n\t\tif t[i] > j:\n\t\t\treturn i\n\treturn -1\nn = int(input())\nif n > 2:\n\tsums = [0 for i in range(n)]\n\tst = input().split()\n\tsums[0] = int(st[0])\n\tfor i in range(n - 1):\n\t\tsums[i + 1] = sums[i] + int(st[i + 1])\n\ts = sums[n - 1] / 3.0\n\ts_end = sums[n - 1] * 2.0 / 3.0\n\tq = []\n\tt = []\n\tfin = 0\n\tstart = 0\n\tk = 0\n\tif s == 0:\n\t\tfor i in range(n):\n\t\t\tif sums[i] == 0:\n\t\t\t\tk += 1\n\t\tprint(int((k - 2) * (k - 1) / 2))\n\telse:\n\t\tfor i in range(n):\n\t\t\tif s == sums[i]:\n\t\t\t\tq.append(i)\n\t\t\tif s_end == sums[i]:\n\t\t\t\tt.append(i)\n\t\tt_length = len(t)\n\t\tfor j in q:\n\t\t\tg = find_f(j, t, start)\n\t\t\tif g == -1:\n\t\t\t\tbreak\n\t\t\telse:\n\t\t\t\tfin += t_length - g\n\t\t\t\tstart = g\n\t\tprint(fin)\nelse:\n\tprint('0')\n", "import sys\nn = int(sys.stdin.readline())\na = [int(x) for x in sys.stdin.readline().split()]\ns = [a[0]]\nsucet = a[0]\nfor i in range(1, len(a)):\n\ts.append(s[-1] + a[i])\n\tsucet += a[i]\nif sucet % 3 != 0:\n\tprint(0)\nelse:\n\tmoznosti = 0\n\tdvoj = 2 * (sucet // 3)\n\tkdedvoj = [(0, 1)[s[0] == dvoj]]\n\tfor i in range(1, len(s)):\n\t\tkdedvoj.append(kdedvoj[-1] + (0, 1)[s[i] == dvoj])\n\tif sucet == 0:\n\t\tif kdedvoj[-1] - 2 < 1:\n\t\t\tprint(0)\n\t\telif kdedvoj[-1] - 2 == 1:\n\t\t\tprint(1)\n\t\telse:\n\t\t\tprint((kdedvoj[-1] ** 2 - 3 * kdedvoj[-1] + 2) // 2)\n\telse:\n\t\tj = n\n\t\tfor i in range(len(a)):\n\t\t\tif s[i] == sucet // 3:\n\t\t\t\tj = i\n\t\t\t\tbreak\n\t\tk = -1\n\t\tfor i in range(len(a) - 1, -1, -1):\n\t\t\tif s[i] == sucet:\n\t\t\t\tk = i\n\t\t\t\tbreak\n\t\tfor i in range(j, k):\n\t\t\tif s[i] == sucet // 3:\n\t\t\t\tmoznosti += kdedvoj[-1] - kdedvoj[i]\n\t\tprint(moznosti)\n", "n = int(input())\na = list(map(int, input().split()))\nif a == [0] * n:\n\tprint(int((n - 2) * (n - 1) / 2))\nelse:\n\t(b, c, d, e) = (sum(a), 0, 0, 0)\n\tf = []\n\tif b % 3 != 0:\n\t\tprint(0)\n\telse:\n\t\tfor i in range(n - 2):\n\t\t\tc += a[i]\n\t\t\tif c == b / 3:\n\t\t\t\td = i + 1\n\t\t\t\tc = 0\n\t\t\t\tbreak\n\t\tif d == 0:\n\t\t\tprint(0)\n\t\telse:\n\t\t\tfor j in range(n - d - 1):\n\t\t\t\tc += a[j + d]\n\t\t\t\tif c == b / 3:\n\t\t\t\t\tf += [j + d + 1]\n\t\t\t\t\te += 1\n\t\t\tif e == 0:\n\t\t\t\tprint(0)\n\t\t\telse:\n\t\t\t\tc = 0\n\t\t\t\tg = e\n\t\t\t\tfor i in range(n - d - 2):\n\t\t\t\t\tc += a[i + d]\n\t\t\t\t\tif c == 0:\n\t\t\t\t\t\tfor j in range(e):\n\t\t\t\t\t\t\tif 1 + i + d < f[j]:\n\t\t\t\t\t\t\t\tg += e - j\n\t\t\t\t\t\t\t\tbreak\n\t\t\t\tprint(g)\n", "n = int(input())\na = [int(i) for i in input().split()]\ns = sum(a)\nif s % 3 != 0:\n\tprint(0)\n\texit()\ncount = [0] * n\ns //= 3\nss = 0\nfor i in range(n - 1, -1, -1):\n\tss += a[i]\n\tif ss == s:\n\t\tcount[i] = 1\nsums = [0] * n\nsums[-1] = count[-1]\nfor i in range(n - 2, -1, -1):\n\tsums[i] = sums[i + 1] + count[i]\nans = 0\nss = 0\nfor i in range(n - 2):\n\tss += a[i]\n\tif ss == s:\n\t\tans += sums[i + 2]\nprint(ans)\n", "t = int(input())\nnums = [int(i) for i in input().split()]\nSUM = sum(nums)\nif SUM % 3 != 0:\n\tprint(0)\nelse:\n\tone_third = SUM // 3\n\ttwo_third = one_third * 2\n\tcount = 0\n\tanswer = 0\n\ttemp_sum = 0\n\tfor i in range(len(nums) - 1):\n\t\ttemp_sum += nums[i]\n\t\tif temp_sum == two_third:\n\t\t\tanswer += count\n\t\tif temp_sum == one_third:\n\t\t\tcount += 1\n\tprint(answer)\n", "import sys\nimport itertools\nimport math\nimport collections\nfrom collections import Counter\n\ndef pow(x, y, mod):\n\tr = 1\n\tx = x % mod\n\twhile y > 0:\n\t\tif y & 1:\n\t\t\tr = r * x % mod\n\t\ty = y >> 1\n\t\tx = x * x % mod\n\treturn r\n\ndef sieve(n):\n\tprime = [True for i in range(n + 1)]\n\tp = 2\n\twhile p * p <= n:\n\t\tif prime[p] == True:\n\t\t\tfor i in range(p * 2, n + 1, p):\n\t\t\t\tprime[i] = False\n\t\tp += 1\n\tprime[0] = prime[1] = False\n\tr = [p for p in range(n + 1) if prime[p]]\n\treturn r\n\ndef divs(n, start=1):\n\tr = []\n\tfor i in range(start, int(math.sqrt(n) + 1)):\n\t\tif n % i == 0:\n\t\t\tif n / i == i:\n\t\t\t\tr.append(i)\n\t\t\telse:\n\t\t\t\tr.extend([i, n // i])\n\treturn r\n\ndef cdiv(n, k):\n\treturn n // k + (n % k != 0)\n\ndef ii():\n\treturn int(input())\n\ndef mi():\n\treturn map(int, input().split())\n\ndef li():\n\treturn list(map(int, input().split()))\n\ndef lcm(a, b):\n\treturn abs(a * b) // math.gcd(a, b)\n\ndef prr(a, sep=' '):\n\tprint(sep.join(map(str, a)))\n\ndef dd():\n\treturn collections.defaultdict(int)\nn = ii()\nd = li()\nif sum(d) % 3 != 0:\n\texit(print(0))\nt = sum(d) // 3\npre = [d[0]]\nfor i in range(1, n):\n\tpre.append(pre[-1] + d[i])\nh = [int(pre[0] == 2 * t)]\nfor i in range(1, n - 1):\n\th.append(h[-1] + (pre[i] == 2 * t))\nres = 0\nfor i in range(n - 2):\n\tif pre[i] == t:\n\t\tres += h[n - 2] - h[i]\nprint(res)\n", "n = int(input())\nline = list(map(int, input().split()))\nm = sum(line)\nif m % 3 != 0:\n\tprint(0)\n\texit(0)\nfor i in range(n):\n\tif i == 0:\n\t\tcontinue\n\tline[i] += line[i - 1]\na = b = 0\nfor i in line[:-1]:\n\tif i == m // 3:\n\t\ta += 1\n\tif i == m // 3 * 2:\n\t\tb += a\nif m == 0:\n\tprint(a * (a - 1) // 2)\nelse:\n\tprint(b)\n", "n = input()\nl = input()\nl = l.split(' ')\nx = 0\nm = 0\nfor i in range(0, len(l)):\n\tl[i] = int(l[i])\n\tm += l[i]\n\tl[i] = m\nm = m / 3\nans = 0\nfor i in range(0, len(l) - 2):\n\tif l[i] == m:\n\t\tx += 1\n\tif l[i + 1] == 2 * m:\n\t\tans += x\nprint(ans)\n", "import sys\nimport math\nimport collections\nimport operator as op\nfrom collections import deque\nfrom math import gcd, inf, sqrt, pi, cos, sin, ceil, log2\nfrom bisect import bisect_right, bisect_left\nfrom functools import reduce\nfrom sys import stdin, stdout, setrecursionlimit\nsetrecursionlimit(2 ** 20)\n\ndef ncr(n, r):\n\tr = min(r, n - r)\n\tnumer = reduce(op.mul, range(n, n - r, -1), 1)\n\tdenom = reduce(op.mul, range(1, r + 1), 1)\n\treturn numer // denom\n\ndef prime_factors(n):\n\ti = 2\n\tfactors = []\n\twhile i * i <= n:\n\t\tif n % i:\n\t\t\ti += 1\n\t\telse:\n\t\t\tn //= i\n\t\t\tfactors.append(i)\n\tif n > 1:\n\t\tfactors.append(n)\n\treturn list(factors)\n\ndef sumDigits(no):\n\treturn 0 if no == 0 else int(no % 10) + sumDigits(int(no / 10))\nMOD = 1000000007\nPMOD = 998244353\nN = 10 ** 9 + 7\nT = 1\nfor _ in range(T):\n\tn = int(stdin.readline())\n\ta = list(map(int, stdin.readline().rstrip().split()))\n\tp = [0] * n\n\tp[0] = a[0]\n\tfor i in range(1, n):\n\t\tp[i] = p[i - 1] + a[i]\n\tif p[-1] % 3 != 0:\n\t\tprint(0)\n\t\tcontinue\n\tdi = {}\n\tdj = {}\n\tfor i in range(n):\n\t\tif 3 * p[i] == p[-1]:\n\t\t\tdi[i] = 1\n\tfor j in range(n):\n\t\tif 3 * p[j] == 2 * p[-1]:\n\t\t\tif j != n - 1:\n\t\t\t\tdj[j] = 1\n\tja = list(dj.keys())\n\tia = list(di.keys())\n\tia.sort()\n\tja.sort()\n\tnj = len(ja)\n\tans = 0\n\tfor i in ia:\n\t\tix = bisect_right(ja, i)\n\t\tans += nj - ix\n\tprint(ans)\n"]

question:
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 ![image](1.png).

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

raw_problem:
C. Number of Ways

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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 .

Input

The first line contains integer n (1 ≤ n ≤ 5·105), showing how many numbers are in the array. The second line contains n integers a[1], a[2], ..., a[n] (|a[i]| ≤  109) — 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

tags: ['Sorting', 'Amortized analysis', 'Complete search', 'Dynamic programming', 'Data structures']

raw_tags: ['data structures', 'binary search', 'dp', 'two pointers', 'brute force']

data_split: test