Counting Equilateral Triangles

Given arc lengths around a circle and points at the boundaries, count equilateral triangles whose three vertices are among the points.

Medium4Prefix sumMathTwo pointersInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

NN points lie on a circle. Write a program that counts the distinct equilateral triangles whose three vertices are all among the given points.

The positions of the points are fixed by the arc lengths between neighboring points along the circle. In the figure below, (a) shows eight points whose consecutive arc lengths are 4, 2, 4, 2, 2, 6, 2, 2, and (b) shows the two equilateral triangles that can be built from them.

Input

The first line contains the number of points NN. The second line contains NN integers X1,X2,,XNX_1, X_2, \dots, X_N. For 1iN11 \le i \le N-1, XiX_i is the length of the arc between point ii and point i+1i+1, and XNX_N is the length of the arc between point NN and point 11. Points 11 through NN are numbered in one direction around the circle.

Restrictions

  • 3N1053 \le N \le 10^5
  • 1Xi1031 \le X_i \le 10^3 for 1iN1 \le i \le N

Output

Print one line containing a single integer, the number of distinct equilateral triangles whose vertices are given points.