#define _USE_MATH_DEFINES

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath> // pow(10, 3)
//#include <stdint.h>
#include <iomanip> // std::cout.precision(17);
#include <vector>
#include <algorithm> // vector.find
#include <unordered_map>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::swap;
using std::map;
//using namespace std;

//__int64 n, m, n2, q, p, m2, s2, d, e, r, t;
//int s[15];
//int x[1005][1005];
//int y[15][1005][1005];
//int Z = 2000000000;

__int64 res = 0, tmp;
__int64 i, j, k, l, m, n, p, q, r;//, s, t;
__int64 x, y, z;
//__int64 c, d;
//char a[505][505];
//vector<__int64> b[105];
string s[100005];
__int64 slen[100005];
vector<__int64> a(1005), v(1005), b(1005);
vector<__int64> c[1005];

//void partition2(vector<double> &a, int l, int r, int &m1, int &m2) {
void partition2(vector<__int64> &a, int l, int r, int &m1, int &m2) {
//void partition2(vector<__int64> &a, vector<__int64> &b, int l, int r, int &m1, int &m2) {
//  double xa = a[l];
//  __int64 xa = a[l];
//  __int64 xa = a[l];
//  __int64 xb = b[l];
  __int64 xa = v[a[l]];
  m1 = l;
  m2 = l;
  for (int i = l + 1; i <= r; ++i) {
//    if (a[i] == xa) {
//    if ((a[i] == xa) && (b[i] == xb)) {
    if (v[a[i]] == xa) {
      m2++;
      swap(a[i], a[m2]);
//      swap(a[i], a[m2]);
//      swap(b[i], b[m2]);
    }
//    else if (a[i] < xa) {
//    else if ((a[i] < xa) || ((a[i] == xa) && (b[i] < xb))) {
    else if (v[a[i]] < xa) {
      m2++;
      swap(a[i], a[m2]);
      swap(a[m1], a[m2]);
//      swap(a[i], a[m2]);
//      swap(a[m1], a[m2]);
//      swap(b[i], b[m2]);
//      swap(b[m1], b[m2]);
      m1++;
    }
  }
}

//void randomized_quick_sort(vector<double> &a, int l, int r) {
void randomized_quick_sort(vector<__int64> &a, int l, int r) {
//void randomized_quick_sort(vector<__int64> &a, vector<__int64> &b, int l, int r) {
  if (l >= r) {
    return;
  }

  int k = l + rand() % (r - l + 1);
  swap(a[l], a[k]);
//  swap(a[l], a[k]);
//  swap(b[l], b[k]);
  int m1, m2;
  partition2(a, l, r, m1, m2);
//  partition2(a, b, l, r, m1, m2);

  randomized_quick_sort(a, l, m1 - 1);
  randomized_quick_sort(a, m2 + 1, r);
//  randomized_quick_sort(a, b, l, m1 - 1);
//  randomized_quick_sort(a, b, m2 + 1, r);
}

__int64 gcd(__int64 a, __int64 b) {
    __int64 c;
    c = a % b;
    while (c) {
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

/*
int process(int i0, int i, int tmp){
    //std::cout << i << " start " << res << ' ' << tmp << std::endl;
	if (tmp > m){
        //std::cout << i << " A " << tmp << ' ' << m << std::endl;
		return 0;
	}
	if ((b[i].size() == 1) && (b[i][0] == i0)){
        //std::cout << i << " B " << std::endl;
		res++;
	}
	else{
        //std::cout << i << " C " << std::endl;
		for (int j = 0; j < b[i].size(); j++)
            if (b[i][j] != i0)
                process(i, b[i][j], (tmp + 1) * a[b[i][j]]);
	}
    //std::cout << i << "  end  " << res << ' ' << tmp << std::endl;
	return 0;
}
*/

/*
__int64 kn[105], km[105];
__int64 an[105][105], am[105][105];
__int64 bn[105], bm[105];

void process_m(int j);

void process_n(int i){
    if (bn[i])
        return;
	bn[i] = res;
	//std::cout << "n " << i+1 << ' ' << res << std::endl;
	for (int j = 0; j < kn[i]; j++)
		process_m(an[i][j]);
}

void process_m(int j){
    if (bm[j])
        return;
	bm[j] = res;
	//std::cout << "m " << j+1 << ' ' << res << std::endl;
	for (int i = 0; i < km[j]; i++)
	    process_n(am[j][i]);
}
*/

/*
void process377(int x, int y){
    if (s <= k)
        return;
    if ((x < 0) || (y < 0) || (x >= n) || (y >= m))
        return;
    switch (a[x][y]){
        case '+': break;
        case '#': break;
        case '.':
            a[x][y] = '+';
            s--;
            process377(x - 1, y);
            process377(x, y - 1);
            process377(x + 1, y);
            process377(x, y + 1);
            break;
    }
}
*/

int good(int i, int l, int r){
    int i0, i1, j00, j0d, j0n, j10, j1d, j1n, j0, j1, res;
    i0 = i - 1;
    i1 = i;
    j00 = l ? slen[i0] - 1 : 0;
    j0d = l ? -1 : +1;
    j0n = l ? -1 : slen[i0];
    j10 = r ? slen[i1] - 1 : 0;
    j1d = r ? -1 : +1;
    j1n = r ? -1 : slen[i1];
    j0 = j00;
    j1 = j10;
//cout << endl << i << ' ' << l << ' ' << r << endl;
    while (true){
        if (j0 == j0n) { res = 1; break; }
        if (j1 == j1n) { res = 0; break; }
//cout << s[i0][j0] << s[i1][j1] << ' ';
        if (s[i0][j0] < s[i1][j1]) { res = 1; break; }
        if (s[i0][j0] > s[i1][j1]) { res = 0; break; }

        j0 += j0d;
        j1 += j1d;
    }
//cout << i << ' ' << l << ' ' << r << " = " << res << endl;
    return res;
}

int goodll(int i){ return good(i, 0, 0); }
int goodlr(int i){ return good(i, 0, 1); }
int goodrl(int i){ return good(i, 1, 0); }
int goodrr(int i){ return good(i, 1, 1); }

__int64 min2(__int64 a, __int64 b){
    if (a == -1) return b;
    if (b == -1) return a;
    return std::min(a, b);
}

__int64 nok(__int64 a, __int64 b){
    return a * b / gcd(a, b);
}

__int64 task235a(__int64 a, __int64 b, __int64 c){
    __int64 d, e;
    d = nok(a, b);
    e = nok(d, c);
    return e;
}

__int64 is_prime(__int64 n){
    __int64 res = 1;
    __int64 i = sqrt(n) + 1;
    for (i; (i > 1) && res; i--){
        if (!(n % i))
            res = 0;
    }
    return res;
}

int main(){
    //std::unordered_map<int, std::unordered_map<int, int>> z;
    //std::string s;
    //vector<__int64> a(n);
    //randomized_quick_sort(a, 0, n - 1);
    //a.push_back(1);
    __int64 K = 1e9 + 7;

    __int64 a[500005], b[500005], c[500005], sum, a1, a2, a3, a4, a5;

    cin >> n;
    sum = 0;
    res = 0;
    for (i = 0; i < n; ++i){
    	cin >> tmp;
    	a[i] = tmp;
    	b[i] = -1;
    	c[i] = -1;
    	sum += tmp;
    	res += tmp >= 0 ? tmp : -tmp;
    }
    if (n < 3)
        res = 0;
    else if (sum % 3)
        res = 0;
    else if (!res)
        res = (n - 1) * (n - 2) / 2;
    else if (!sum){
        a1 = 0;
        tmp = 0;
        for (i = 0; i < n; i++){
            tmp += a[i];
            if (!tmp)
                a1++;
        }
        n = a1;
        res = (n - 1) * (n - 2) / 2;
    }
    else{
        sum /= 3;
        res = 0;

        i = 0;
        tmp = a[i];
        while ((tmp != sum) && (i + 1 < n))
            tmp += a[++i];
        b[i] = 0;
        a2 = 0;
        tmp = 0;
        int flag = 1;
        while (i + 1 < n){
            tmp += a[++i];
            if (!tmp){
                a2++;
                flag = 1;
            }
            if (flag && (tmp == sum)){
                b[i] = a2;
                flag = 0;
            }
        }

        i = n - 1;
        tmp = a[i];
        while ((tmp != sum) && (i - 1 >= 0))
            tmp += a[--i];
        c[i] = 0;
        a4 = 0;
        tmp = 0;
        while (i - 1 >= 0){
            tmp += a[--i];
            if (!tmp){
                a4++;
                c[i] = a4;
                if (i - 1 >= 0)
                    if ((b[i - 1] + 1) * (c[i] + 1) > res)
                        res = (b[i - 1] + 1) * (c[i] + 1);
            }
        }
    }

    cout << res;
    return 0;
}


#define _USE_MATH_DEFINES

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath> // pow(10, 3)
//#include <stdint.h>
#include <iomanip> // std::cout.precision(17);
#include <vector>
#include <algorithm> // vector.find
#include <unordered_map>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::swap;
using std::map;
//using namespace std;

//__int64 n, m, n2, q, p, m2, s2, d, e, r, t;
//int s[15];
//int x[1005][1005];
//int y[15][1005][1005];
//int Z = 2000000000;

__int64 res = 0, tmp;
__int64 i, j, k, l, m, n, p, q, r;//, s, t;
__int64 x, y, z;
//__int64 c, d;
//char a[505][505];
//vector<__int64> b[105];
string s[100005];
__int64 slen[100005];
vector<__int64> a(1005), v(1005), b(1005);
vector<__int64> c[1005];

//void partition2(vector<double> &a, int l, int r, int &m1, int &m2) {
void partition2(vector<__int64> &a, int l, int r, int &m1, int &m2) {
//void partition2(vector<__int64> &a, vector<__int64> &b, int l, int r, int &m1, int &m2) {
//  double xa = a[l];
//  __int64 xa = a[l];
//  __int64 xa = a[l];
//  __int64 xb = b[l];
  __int64 xa = v[a[l]];
  m1 = l;
  m2 = l;
  for (int i = l + 1; i <= r; ++i) {
//    if (a[i] == xa) {
//    if ((a[i] == xa) && (b[i] == xb)) {
    if (v[a[i]] == xa) {
      m2++;
      swap(a[i], a[m2]);
//      swap(a[i], a[m2]);
//      swap(b[i], b[m2]);
    }
//    else if (a[i] < xa) {
//    else if ((a[i] < xa) || ((a[i] == xa) && (b[i] < xb))) {
    else if (v[a[i]] < xa) {
      m2++;
      swap(a[i], a[m2]);
      swap(a[m1], a[m2]);
//      swap(a[i], a[m2]);
//      swap(a[m1], a[m2]);
//      swap(b[i], b[m2]);
//      swap(b[m1], b[m2]);
      m1++;
    }
  }
}

//void randomized_quick_sort(vector<double> &a, int l, int r) {
void randomized_quick_sort(vector<__int64> &a, int l, int r) {
//void randomized_quick_sort(vector<__int64> &a, vector<__int64> &b, int l, int r) {
  if (l >= r) {
    return;
  }

  int k = l + rand() % (r - l + 1);
  swap(a[l], a[k]);
//  swap(a[l], a[k]);
//  swap(b[l], b[k]);
  int m1, m2;
  partition2(a, l, r, m1, m2);
//  partition2(a, b, l, r, m1, m2);

  randomized_quick_sort(a, l, m1 - 1);
  randomized_quick_sort(a, m2 + 1, r);
//  randomized_quick_sort(a, b, l, m1 - 1);
//  randomized_quick_sort(a, b, m2 + 1, r);
}

__int64 gcd(__int64 a, __int64 b) {
    __int64 c;
    c = a % b;
    while (c) {
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

/*
int process(int i0, int i, int tmp){
    //std::cout << i << " start " << res << ' ' << tmp << std::endl;
	if (tmp > m){
        //std::cout << i << " A " << tmp << ' ' << m << std::endl;
		return 0;
	}
	if ((b[i].size() == 1) && (b[i][0] == i0)){
        //std::cout << i << " B " << std::endl;
		res++;
	}
	else{
        //std::cout << i << " C " << std::endl;
		for (int j = 0; j < b[i].size(); j++)
            if (b[i][j] != i0)
                process(i, b[i][j], (tmp + 1) * a[b[i][j]]);
	}
    //std::cout << i << "  end  " << res << ' ' << tmp << std::endl;
	return 0;
}
*/

/*
__int64 kn[105], km[105];
__int64 an[105][105], am[105][105];
__int64 bn[105], bm[105];

void process_m(int j);

void process_n(int i){
    if (bn[i])
        return;
	bn[i] = res;
	//std::cout << "n " << i+1 << ' ' << res << std::endl;
	for (int j = 0; j < kn[i]; j++)
		process_m(an[i][j]);
}

void process_m(int j){
    if (bm[j])
        return;
	bm[j] = res;
	//std::cout << "m " << j+1 << ' ' << res << std::endl;
	for (int i = 0; i < km[j]; i++)
	    process_n(am[j][i]);
}
*/

/*
void process377(int x, int y){
    if (s <= k)
        return;
    if ((x < 0) || (y < 0) || (x >= n) || (y >= m))
        return;
    switch (a[x][y]){
        case '+': break;
        case '#': break;
        case '.':
            a[x][y] = '+';
            s--;
            process377(x - 1, y);
            process377(x, y - 1);
            process377(x + 1, y);
            process377(x, y + 1);
            break;
    }
}
*/

int good(int i, int l, int r){
    int i0, i1, j00, j0d, j0n, j10, j1d, j1n, j0, j1, res;
    i0 = i - 1;
    i1 = i;
    j00 = l ? slen[i0] - 1 : 0;
    j0d = l ? -1 : +1;
    j0n = l ? -1 : slen[i0];
    j10 = r ? slen[i1] - 1 : 0;
    j1d = r ? -1 : +1;
    j1n = r ? -1 : slen[i1];
    j0 = j00;
    j1 = j10;
//cout << endl << i << ' ' << l << ' ' << r << endl;
    while (true){
        if (j0 == j0n) { res = 1; break; }
        if (j1 == j1n) { res = 0; break; }
//cout << s[i0][j0] << s[i1][j1] << ' ';
        if (s[i0][j0] < s[i1][j1]) { res = 1; break; }
        if (s[i0][j0] > s[i1][j1]) { res = 0; break; }

        j0 += j0d;
        j1 += j1d;
    }
//cout << i << ' ' << l << ' ' << r << " = " << res << endl;
    return res;
}

int goodll(int i){ return good(i, 0, 0); }
int goodlr(int i){ return good(i, 0, 1); }
int goodrl(int i){ return good(i, 1, 0); }
int goodrr(int i){ return good(i, 1, 1); }

__int64 min2(__int64 a, __int64 b){
    if (a == -1) return b;
    if (b == -1) return a;
    return std::min(a, b);
}

__int64 nok(__int64 a, __int64 b){
    return a * b / gcd(a, b);
}

__int64 task235a(__int64 a, __int64 b, __int64 c){
    __int64 d, e;
    d = nok(a, b);
    e = nok(d, c);
    return e;
}

__int64 is_prime(__int64 n){
    __int64 res = 1;
    __int64 i = sqrt(n) + 1;
    for (i; (i > 1) && res; i--){
        if (!(n % i))
            res = 0;
    }
    return res;
}

int sudoku(){
    char a[9][9];
    char b[9][9];
    char c[9][9][9];
    int i, j;
    for (i = 0; i < 9; i++)
        for (j = 0; j < 9; j++)
            cin >> a[i][j];

    res = 81;
    for (i = 0; i < 9; i++)
        for (j = 0; j < 9; j++)
            if (a[i][j] != '0')
                res--;
    while (res){

    }

    for (i = 0; i < 9; i++){
        for (j = 0; j < 9; j++)
            cout << a[i][j];
        cout << endl;
    }
    return 0;
}

int main(){
    //std::unordered_map<int, std::unordered_map<int, int>> z;
    //std::string s;
    //vector<__int64> a(n);
    //randomized_quick_sort(a, 0, n - 1);
    //a.push_back(1);
    __int64 K = 1e9 + 7;

    __int64 a[500005], b[500005], c[500005], sum, a1, a2, a3, a4, a5;

    cin >> n;
    sum = 0;
    res = 0;
    for (i = 0; i < n; ++i){
    	cin >> tmp;
    	a[i] = tmp;
    	b[i] = -1;
    	c[i] = -1;
    	sum += tmp;
    	res += tmp >= 0 ? tmp : -tmp;
    }
    if (n < 3)
        res = 0;
    else if (sum % 3)
        res = 0;
    else if (!res)
        res = (n - 1) * (n - 2) / 2;
    else if (!sum){
        a1 = 0;
        tmp = 0;
        for (i = 0; i < n; i++){
            tmp += a[i];
            if (!tmp)
                a1++;
        }
        n = a1;
        res = (n - 1) * (n - 2) / 2;
    }
    else{
        sum /= 3;
        a5 = 0;
        tmp = 0;
        for (i = n - 1; i > 0; i--){
            tmp += a[i];
            if (tmp == sum)
                a5++;
            b[i] = a5;
        }
        res = 0;
        tmp = 0;
        for (i = 0; i < n - 1; i++){
            tmp += a[i];
            if (tmp == sum)
                res += b[i + 1];

        }
    }
    cout << res;
    return 0;
}


#define _USE_MATH_DEFINES

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath> // pow(10, 3)
//#include <stdint.h>
#include <iomanip> // std::cout.precision(17);
#include <vector>
#include <algorithm> // vector.find
#include <unordered_map>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::swap;
using std::map;
//using namespace std;

//__int64 n, m, n2, q, p, m2, s2, d, e, r, t;
//int s[15];
//int x[1005][1005];
//int y[15][1005][1005];
//int Z = 2000000000;

__int64 res = 0, tmp;
__int64 i, j, k, l, m, n, p, q, r;//, s, t;
__int64 x, y, z;
//__int64 c, d;
//char a[505][505];
//vector<__int64> b[105];
string s[100005];
__int64 slen[100005];
vector<__int64> a(1005), v(1005), b(1005);
vector<__int64> c[1005];

//void partition2(vector<double> &a, int l, int r, int &m1, int &m2) {
void partition2(vector<__int64> &a, int l, int r, int &m1, int &m2) {
//void partition2(vector<__int64> &a, vector<__int64> &b, int l, int r, int &m1, int &m2) {
//  double xa = a[l];
//  __int64 xa = a[l];
//  __int64 xa = a[l];
//  __int64 xb = b[l];
  __int64 xa = v[a[l]];
  m1 = l;
  m2 = l;
  for (int i = l + 1; i <= r; ++i) {
//    if (a[i] == xa) {
//    if ((a[i] == xa) && (b[i] == xb)) {
    if (v[a[i]] == xa) {
      m2++;
      swap(a[i], a[m2]);
//      swap(a[i], a[m2]);
//      swap(b[i], b[m2]);
    }
//    else if (a[i] < xa) {
//    else if ((a[i] < xa) || ((a[i] == xa) && (b[i] < xb))) {
    else if (v[a[i]] < xa) {
      m2++;
      swap(a[i], a[m2]);
      swap(a[m1], a[m2]);
//      swap(a[i], a[m2]);
//      swap(a[m1], a[m2]);
//      swap(b[i], b[m2]);
//      swap(b[m1], b[m2]);
      m1++;
    }
  }
}

//void randomized_quick_sort(vector<double> &a, int l, int r) {
void randomized_quick_sort(vector<__int64> &a, int l, int r) {
//void randomized_quick_sort(vector<__int64> &a, vector<__int64> &b, int l, int r) {
  if (l >= r) {
    return;
  }

  int k = l + rand() % (r - l + 1);
  swap(a[l], a[k]);
//  swap(a[l], a[k]);
//  swap(b[l], b[k]);
  int m1, m2;
  partition2(a, l, r, m1, m2);
//  partition2(a, b, l, r, m1, m2);

  randomized_quick_sort(a, l, m1 - 1);
  randomized_quick_sort(a, m2 + 1, r);
//  randomized_quick_sort(a, b, l, m1 - 1);
//  randomized_quick_sort(a, b, m2 + 1, r);
}

__int64 gcd(__int64 a, __int64 b) {
    __int64 c;
    c = a % b;
    while (c) {
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

/*
int process(int i0, int i, int tmp){
    //std::cout << i << " start " << res << ' ' << tmp << std::endl;
	if (tmp > m){
        //std::cout << i << " A " << tmp << ' ' << m << std::endl;
		return 0;
	}
	if ((b[i].size() == 1) && (b[i][0] == i0)){
        //std::cout << i << " B " << std::endl;
		res++;
	}
	else{
        //std::cout << i << " C " << std::endl;
		for (int j = 0; j < b[i].size(); j++)
            if (b[i][j] != i0)
                process(i, b[i][j], (tmp + 1) * a[b[i][j]]);
	}
    //std::cout << i << "  end  " << res << ' ' << tmp << std::endl;
	return 0;
}
*/

/*
__int64 kn[105], km[105];
__int64 an[105][105], am[105][105];
__int64 bn[105], bm[105];

void process_m(int j);

void process_n(int i){
    if (bn[i])
        return;
	bn[i] = res;
	//std::cout << "n " << i+1 << ' ' << res << std::endl;
	for (int j = 0; j < kn[i]; j++)
		process_m(an[i][j]);
}

void process_m(int j){
    if (bm[j])
        return;
	bm[j] = res;
	//std::cout << "m " << j+1 << ' ' << res << std::endl;
	for (int i = 0; i < km[j]; i++)
	    process_n(am[j][i]);
}
*/

/*
void process377(int x, int y){
    if (s <= k)
        return;
    if ((x < 0) || (y < 0) || (x >= n) || (y >= m))
        return;
    switch (a[x][y]){
        case '+': break;
        case '#': break;
        case '.':
            a[x][y] = '+';
            s--;
            process377(x - 1, y);
            process377(x, y - 1);
            process377(x + 1, y);
            process377(x, y + 1);
            break;
    }
}
*/

int good(int i, int l, int r){
    int i0, i1, j00, j0d, j0n, j10, j1d, j1n, j0, j1, res;
    i0 = i - 1;
    i1 = i;
    j00 = l ? slen[i0] - 1 : 0;
    j0d = l ? -1 : +1;
    j0n = l ? -1 : slen[i0];
    j10 = r ? slen[i1] - 1 : 0;
    j1d = r ? -1 : +1;
    j1n = r ? -1 : slen[i1];
    j0 = j00;
    j1 = j10;
//cout << endl << i << ' ' << l << ' ' << r << endl;
    while (true){
        if (j0 == j0n) { res = 1; break; }
        if (j1 == j1n) { res = 0; break; }
//cout << s[i0][j0] << s[i1][j1] << ' ';
        if (s[i0][j0] < s[i1][j1]) { res = 1; break; }
        if (s[i0][j0] > s[i1][j1]) { res = 0; break; }

        j0 += j0d;
        j1 += j1d;
    }
//cout << i << ' ' << l << ' ' << r << " = " << res << endl;
    return res;
}

int goodll(int i){ return good(i, 0, 0); }
int goodlr(int i){ return good(i, 0, 1); }
int goodrl(int i){ return good(i, 1, 0); }
int goodrr(int i){ return good(i, 1, 1); }

__int64 min2(__int64 a, __int64 b){
    if (a == -1) return b;
    if (b == -1) return a;
    return std::min(a, b);
}

__int64 nok(__int64 a, __int64 b){
    return a * b / gcd(a, b);
}

__int64 task235a(__int64 a, __int64 b, __int64 c){
    __int64 d, e;
    d = nok(a, b);
    e = nok(d, c);
    return e;
}

__int64 is_prime(__int64 n){
    __int64 res = 1;
    __int64 i = sqrt(n) + 1;
    for (i; (i > 1) && res; i--){
        if (!(n % i))
            res = 0;
    }
    return res;
}

int sudoku(){
    char a[9][9];
    char b[9][9];
    char c[9][9][9];
    int i, j;
    for (i = 0; i < 9; i++)
        for (j = 0; j < 9; j++)
            cin >> a[i][j];

    res = 81;
    for (i = 0; i < 9; i++)
        for (j = 0; j < 9; j++)
            if (a[i][j] != '0')
                res--;
    while (res){

    }

    for (i = 0; i < 9; i++){
        for (j = 0; j < 9; j++)
            cout << a[i][j];
        cout << endl;
    }
    return 0;
}

int main(){
    //std::unordered_map<int, std::unordered_map<int, int>> z;
    //std::string s;
    //vector<__int64> a(n);
    //randomized_quick_sort(a, 0, n - 1);
    //a.push_back(1);
    __int64 K = 1e9 + 7;

    __int64 a[500005], b[500005], c[500005], sum, a1, a2, a3, a4, a5;

    cin >> n;
    sum = 0;
    res = 0;
    for (i = 0; i < n; ++i){
    	cin >> tmp;
    	a[i] = tmp;
    	b[i] = -1;
    	c[i] = -1;
    	sum += tmp;
    	res += tmp >= 0 ? tmp : -tmp;
    }
    if (n < 3)
        res = 0;
    else if (sum % 3)
        res = 0;
    else if (!res)
        res = (n - 1) * (n - 2) / 2;
    else if (!sum){
        a1 = 0;
        tmp = 0;
        for (i = 0; i < n; i++){
            tmp += a[i];
            if (!tmp)
                a1++;
        }
        n = a1;
        res = (n - 1) * (n - 2) / 2;
    }
    else{
        sum /= 3;
        a5 = 0;
        tmp = 0;
        for (i = n - 1; i > 0; i--){
            tmp += a[i];
            if (tmp == sum) a5++;
            b[i] = a5;
        }
        res = 0;
        a1 = 0;
        tmp = 0;
        for (i = 0; i < n - 1; i++){
            tmp += a[i];
            if (tmp == sum){
                a1++;
                res += a1 * b[i + 1];
            }
        }
    }
    cout << res;
    return 0;
}


#define _USE_MATH_DEFINES

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath> // pow(10, 3)
//#include <stdint.h>
#include <iomanip> // std::cout.precision(17);
#include <vector>
#include <algorithm> // vector.find
#include <unordered_map>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::swap;
using std::map;
//using namespace std;

//__int64 n, m, n2, q, p, m2, s2, d, e, r, t;
//int s[15];
//int x[1005][1005];
//int y[15][1005][1005];
//int Z = 2000000000;

__int64 res = 0, tmp;
__int64 i, j, k, l, m, n, p, q, r;//, s, t;
__int64 x, y, z;
//__int64 c, d;
//char a[505][505];
//vector<__int64> b[105];
string s[100005];
__int64 slen[100005];
vector<__int64> a(1005), v(1005), b(1005);
vector<__int64> c[1005];

//void partition2(vector<double> &a, int l, int r, int &m1, int &m2) {
void partition2(vector<__int64> &a, int l, int r, int &m1, int &m2) {
//void partition2(vector<__int64> &a, vector<__int64> &b, int l, int r, int &m1, int &m2) {
//  double xa = a[l];
//  __int64 xa = a[l];
//  __int64 xa = a[l];
//  __int64 xb = b[l];
  __int64 xa = v[a[l]];
  m1 = l;
  m2 = l;
  for (int i = l + 1; i <= r; ++i) {
//    if (a[i] == xa) {
//    if ((a[i] == xa) && (b[i] == xb)) {
    if (v[a[i]] == xa) {
      m2++;
      swap(a[i], a[m2]);
//      swap(a[i], a[m2]);
//      swap(b[i], b[m2]);
    }
//    else if (a[i] < xa) {
//    else if ((a[i] < xa) || ((a[i] == xa) && (b[i] < xb))) {
    else if (v[a[i]] < xa) {
      m2++;
      swap(a[i], a[m2]);
      swap(a[m1], a[m2]);
//      swap(a[i], a[m2]);
//      swap(a[m1], a[m2]);
//      swap(b[i], b[m2]);
//      swap(b[m1], b[m2]);
      m1++;
    }
  }
}

//void randomized_quick_sort(vector<double> &a, int l, int r) {
void randomized_quick_sort(vector<__int64> &a, int l, int r) {
//void randomized_quick_sort(vector<__int64> &a, vector<__int64> &b, int l, int r) {
  if (l >= r) {
    return;
  }

  int k = l + rand() % (r - l + 1);
  swap(a[l], a[k]);
//  swap(a[l], a[k]);
//  swap(b[l], b[k]);
  int m1, m2;
  partition2(a, l, r, m1, m2);
//  partition2(a, b, l, r, m1, m2);

  randomized_quick_sort(a, l, m1 - 1);
  randomized_quick_sort(a, m2 + 1, r);
//  randomized_quick_sort(a, b, l, m1 - 1);
//  randomized_quick_sort(a, b, m2 + 1, r);
}

__int64 gcd(__int64 a, __int64 b) {
    __int64 c;
    c = a % b;
    while (c) {
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

/*
int process(int i0, int i, int tmp){
    //std::cout << i << " start " << res << ' ' << tmp << std::endl;
	if (tmp > m){
        //std::cout << i << " A " << tmp << ' ' << m << std::endl;
		return 0;
	}
	if ((b[i].size() == 1) && (b[i][0] == i0)){
        //std::cout << i << " B " << std::endl;
		res++;
	}
	else{
        //std::cout << i << " C " << std::endl;
		for (int j = 0; j < b[i].size(); j++)
            if (b[i][j] != i0)
                process(i, b[i][j], (tmp + 1) * a[b[i][j]]);
	}
    //std::cout << i << "  end  " << res << ' ' << tmp << std::endl;
	return 0;
}
*/

/*
__int64 kn[105], km[105];
__int64 an[105][105], am[105][105];
__int64 bn[105], bm[105];

void process_m(int j);

void process_n(int i){
    if (bn[i])
        return;
	bn[i] = res;
	//std::cout << "n " << i+1 << ' ' << res << std::endl;
	for (int j = 0; j < kn[i]; j++)
		process_m(an[i][j]);
}

void process_m(int j){
    if (bm[j])
        return;
	bm[j] = res;
	//std::cout << "m " << j+1 << ' ' << res << std::endl;
	for (int i = 0; i < km[j]; i++)
	    process_n(am[j][i]);
}
*/

/*
void process377(int x, int y){
    if (s <= k)
        return;
    if ((x < 0) || (y < 0) || (x >= n) || (y >= m))
        return;
    switch (a[x][y]){
        case '+': break;
        case '#': break;
        case '.':
            a[x][y] = '+';
            s--;
            process377(x - 1, y);
            process377(x, y - 1);
            process377(x + 1, y);
            process377(x, y + 1);
            break;
    }
}
*/

int good(int i, int l, int r){
    int i0, i1, j00, j0d, j0n, j10, j1d, j1n, j0, j1, res;
    i0 = i - 1;
    i1 = i;
    j00 = l ? slen[i0] - 1 : 0;
    j0d = l ? -1 : +1;
    j0n = l ? -1 : slen[i0];
    j10 = r ? slen[i1] - 1 : 0;
    j1d = r ? -1 : +1;
    j1n = r ? -1 : slen[i1];
    j0 = j00;
    j1 = j10;
//cout << endl << i << ' ' << l << ' ' << r << endl;
    while (true){
        if (j0 == j0n) { res = 1; break; }
        if (j1 == j1n) { res = 0; break; }
//cout << s[i0][j0] << s[i1][j1] << ' ';
        if (s[i0][j0] < s[i1][j1]) { res = 1; break; }
        if (s[i0][j0] > s[i1][j1]) { res = 0; break; }

        j0 += j0d;
        j1 += j1d;
    }
//cout << i << ' ' << l << ' ' << r << " = " << res << endl;
    return res;
}

int goodll(int i){ return good(i, 0, 0); }
int goodlr(int i){ return good(i, 0, 1); }
int goodrl(int i){ return good(i, 1, 0); }
int goodrr(int i){ return good(i, 1, 1); }

__int64 min2(__int64 a, __int64 b){
    if (a == -1) return b;
    if (b == -1) return a;
    return std::min(a, b);
}

__int64 nok(__int64 a, __int64 b){
    return a * b / gcd(a, b);
}

__int64 task235a(__int64 a, __int64 b, __int64 c){
    __int64 d, e;
    d = nok(a, b);
    e = nok(d, c);
    return e;
}

__int64 is_prime(__int64 n){
    __int64 res = 1;
    __int64 i = sqrt(n) + 1;
    for (i; (i > 1) && res; i--){
        if (!(n % i))
            res = 0;
    }
    return res;
}

int main(){
    //std::unordered_map<int, std::unordered_map<int, int>> z;
    //std::string s;
    //vector<__int64> a(n);
    //randomized_quick_sort(a, 0, n - 1);
    //a.push_back(1);
    __int64 K = 1e9 + 7;

    __int64 a[500005], sum, a1, a2, a3, a4, a5;

    cin >> n;
    sum = 0;
    res = 0;
    for (i = 0; i < n; ++i){
    	cin >> tmp;
    	a[i] = tmp;
    	sum += tmp;
    	res += tmp >= 0 ? tmp : -tmp;
    }
    if (n < 3)
        res = 0;
    else if (sum % 3)
        res = 0;
    else if (!res)
        res = (n - 1) * (n - 2) / 2;
    else if (!sum){
        a1 = 0;
        tmp = 0;
        for (i = 0; i < n; i++){
            tmp += a[i];
            if (!tmp)
                a1++;
        }
        n = a1;
        res = (n - 1) * (n - 2) / 2;
    }
    else{
        res = 0;
        sum /= 3;
        a1 = 0;
        i = 0;
        tmp = a[i];
        while ((tmp != sum) && (i + 1 < n))
            tmp += a[++i];
        if (tmp == sum){
            a2 = 0;
            a3 = 0;
            i++;
            tmp = a[i];
            while ((tmp != sum) && (i + 1 < n)){
                if (!tmp)
                    a2++;
                tmp += a[++i];
            }
            if (tmp == sum){
                a4 = 0;
                a5 = 0;
                i++;
                tmp = a[i];
                while ((i + 1 < n)){
                    if (!tmp)
                        a4++;
                    tmp += a[++i];
                }
                a2++;
                a4++;
                res = a2 * a4;
            }
        }
    }

    cout << res;
    return 0;
}


#define _USE_MATH_DEFINES

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath> // pow(10, 3)
//#include <stdint.h>
#include <iomanip> // std::cout.precision(17);
#include <vector>
#include <algorithm> // vector.find
#include <unordered_map>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::swap;
using std::map;
//using namespace std;

//__int64 n, m, n2, q, p, m2, s2, d, e, r, t;
//int s[15];
//int x[1005][1005];
//int y[15][1005][1005];
//int Z = 2000000000;

__int64 res = 0, tmp;
__int64 i, j, k, l, m, n, p, q, r;//, s, t;
__int64 x, y, z;
//__int64 c, d;
//char a[505][505];
//vector<__int64> b[105];
string s[100005];
__int64 slen[100005];
vector<__int64> a(1005), v(1005), b(1005);
vector<__int64> c[1005];

//void partition2(vector<double> &a, int l, int r, int &m1, int &m2) {
void partition2(vector<__int64> &a, int l, int r, int &m1, int &m2) {
//void partition2(vector<__int64> &a, vector<__int64> &b, int l, int r, int &m1, int &m2) {
//  double xa = a[l];
//  __int64 xa = a[l];
//  __int64 xa = a[l];
//  __int64 xb = b[l];
  __int64 xa = v[a[l]];
  m1 = l;
  m2 = l;
  for (int i = l + 1; i <= r; ++i) {
//    if (a[i] == xa) {
//    if ((a[i] == xa) && (b[i] == xb)) {
    if (v[a[i]] == xa) {
      m2++;
      swap(a[i], a[m2]);
//      swap(a[i], a[m2]);
//      swap(b[i], b[m2]);
    }
//    else if (a[i] < xa) {
//    else if ((a[i] < xa) || ((a[i] == xa) && (b[i] < xb))) {
    else if (v[a[i]] < xa) {
      m2++;
      swap(a[i], a[m2]);
      swap(a[m1], a[m2]);
//      swap(a[i], a[m2]);
//      swap(a[m1], a[m2]);
//      swap(b[i], b[m2]);
//      swap(b[m1], b[m2]);
      m1++;
    }
  }
}

//void randomized_quick_sort(vector<double> &a, int l, int r) {
void randomized_quick_sort(vector<__int64> &a, int l, int r) {
//void randomized_quick_sort(vector<__int64> &a, vector<__int64> &b, int l, int r) {
  if (l >= r) {
    return;
  }

  int k = l + rand() % (r - l + 1);
  swap(a[l], a[k]);
//  swap(a[l], a[k]);
//  swap(b[l], b[k]);
  int m1, m2;
  partition2(a, l, r, m1, m2);
//  partition2(a, b, l, r, m1, m2);

  randomized_quick_sort(a, l, m1 - 1);
  randomized_quick_sort(a, m2 + 1, r);
//  randomized_quick_sort(a, b, l, m1 - 1);
//  randomized_quick_sort(a, b, m2 + 1, r);
}

__int64 gcd(__int64 a, __int64 b) {
    __int64 c;
    c = a % b;
    while (c) {
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

/*
int process(int i0, int i, int tmp){
    //std::cout << i << " start " << res << ' ' << tmp << std::endl;
	if (tmp > m){
        //std::cout << i << " A " << tmp << ' ' << m << std::endl;
		return 0;
	}
	if ((b[i].size() == 1) && (b[i][0] == i0)){
        //std::cout << i << " B " << std::endl;
		res++;
	}
	else{
        //std::cout << i << " C " << std::endl;
		for (int j = 0; j < b[i].size(); j++)
            if (b[i][j] != i0)
                process(i, b[i][j], (tmp + 1) * a[b[i][j]]);
	}
    //std::cout << i << "  end  " << res << ' ' << tmp << std::endl;
	return 0;
}
*/

/*
__int64 kn[105], km[105];
__int64 an[105][105], am[105][105];
__int64 bn[105], bm[105];

void process_m(int j);

void process_n(int i){
    if (bn[i])
        return;
	bn[i] = res;
	//std::cout << "n " << i+1 << ' ' << res << std::endl;
	for (int j = 0; j < kn[i]; j++)
		process_m(an[i][j]);
}

void process_m(int j){
    if (bm[j])
        return;
	bm[j] = res;
	//std::cout << "m " << j+1 << ' ' << res << std::endl;
	for (int i = 0; i < km[j]; i++)
	    process_n(am[j][i]);
}
*/

/*
void process377(int x, int y){
    if (s <= k)
        return;
    if ((x < 0) || (y < 0) || (x >= n) || (y >= m))
        return;
    switch (a[x][y]){
        case '+': break;
        case '#': break;
        case '.':
            a[x][y] = '+';
            s--;
            process377(x - 1, y);
            process377(x, y - 1);
            process377(x + 1, y);
            process377(x, y + 1);
            break;
    }
}
*/

int good(int i, int l, int r){
    int i0, i1, j00, j0d, j0n, j10, j1d, j1n, j0, j1, res;
    i0 = i - 1;
    i1 = i;
    j00 = l ? slen[i0] - 1 : 0;
    j0d = l ? -1 : +1;
    j0n = l ? -1 : slen[i0];
    j10 = r ? slen[i1] - 1 : 0;
    j1d = r ? -1 : +1;
    j1n = r ? -1 : slen[i1];
    j0 = j00;
    j1 = j10;
//cout << endl << i << ' ' << l << ' ' << r << endl;
    while (true){
        if (j0 == j0n) { res = 1; break; }
        if (j1 == j1n) { res = 0; break; }
//cout << s[i0][j0] << s[i1][j1] << ' ';
        if (s[i0][j0] < s[i1][j1]) { res = 1; break; }
        if (s[i0][j0] > s[i1][j1]) { res = 0; break; }

        j0 += j0d;
        j1 += j1d;
    }
//cout << i << ' ' << l << ' ' << r << " = " << res << endl;
    return res;
}

int goodll(int i){ return good(i, 0, 0); }
int goodlr(int i){ return good(i, 0, 1); }
int goodrl(int i){ return good(i, 1, 0); }
int goodrr(int i){ return good(i, 1, 1); }

__int64 min2(__int64 a, __int64 b){
    if (a == -1) return b;
    if (b == -1) return a;
    return std::min(a, b);
}

__int64 nok(__int64 a, __int64 b){
    return a * b / gcd(a, b);
}

__int64 task235a(__int64 a, __int64 b, __int64 c){
    __int64 d, e;
    d = nok(a, b);
    e = nok(d, c);
    return e;
}

__int64 is_prime(__int64 n){
    __int64 res = 1;
    __int64 i = sqrt(n) + 1;
    for (i; (i > 1) && res; i--){
        if (!(n % i))
            res = 0;
    }
    return res;
}

int main(){
    //std::unordered_map<int, std::unordered_map<int, int>> z;
    //std::string s;
    //vector<__int64> a(n);
    //randomized_quick_sort(a, 0, n - 1);
    //a.push_back(1);
    __int64 K = 1e9 + 7;

    __int64 a[500005], sum, a1, a2, a3, a4, a5;

    cin >> n;
    sum = 0;
    for (i = 0; i < n; ++i){
    	cin >> tmp;
    	a[i] = tmp;
    	sum += tmp;
    	res += tmp >= 0 ? tmp : -tmp;
    }
    if (n < 3)
        res = 0;
    else if (sum % 3)
        res = 0;
    else if (!res)
        res = (n - 1) * (n - 2) / 2;
    else if (!sum){
        a1 = 0;
        tmp = 0;
        for (i = 0; i < n; i++){
            tmp += a[i];
            if (!tmp)
                a1++;
        }
        n = a1;
        res = (n - 1) * (n - 2) / 2;
    }
    else{
        res = 0;
        sum /= 3;
        a1 = 0;
        i = 0;
        tmp = a[i];
        while ((tmp != sum) && (i + 1 < n))
            tmp += a[++i];
        if (tmp == sum){
            a2 = 0;
            a3 = 0;
            i++;
            tmp = a[i];
            while ((tmp != sum) && (i + 1 < n)){
                if (!tmp)
                    a2++;
                tmp += a[++i];
            }
            if (tmp == sum){
                a4 = 0;
                a5 = 0;
                i++;
                tmp = a[i];
                while ((tmp != sum) && (i + 1 < n)){
                    if (!tmp)
                        a4++;
                    tmp += a[++i];
                }
                a2++;
                a4++;
                res = a2 * a4;
            }
        }
    }

    cout << res;
    return 0;
}


#define _USE_MATH_DEFINES

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath> // pow(10, 3)
//#include <stdint.h>
#include <iomanip> // std::cout.precision(17);
#include <vector>
#include <algorithm> // vector.find
#include <unordered_map>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::swap;
using std::map;
//using namespace std;

//__int64 n, m, n2, q, p, m2, s2, d, e, r, t;
//int s[15];
//int x[1005][1005];
//int y[15][1005][1005];
//int Z = 2000000000;

__int64 res = 0, tmp;
__int64 i, j, k, l, m, n, p, q, r;//, s, t;
__int64 x, y, z;
//__int64 c, d;
//char a[505][505];
//vector<__int64> b[105];
string s[100005];
__int64 slen[100005];
vector<__int64> a(1005), v(1005), b(1005);
vector<__int64> c[1005];

//void partition2(vector<double> &a, int l, int r, int &m1, int &m2) {
void partition2(vector<__int64> &a, int l, int r, int &m1, int &m2) {
//void partition2(vector<__int64> &a, vector<__int64> &b, int l, int r, int &m1, int &m2) {
//  double xa = a[l];
//  __int64 xa = a[l];
//  __int64 xa = a[l];
//  __int64 xb = b[l];
  __int64 xa = v[a[l]];
  m1 = l;
  m2 = l;
  for (int i = l + 1; i <= r; ++i) {
//    if (a[i] == xa) {
//    if ((a[i] == xa) && (b[i] == xb)) {
    if (v[a[i]] == xa) {
      m2++;
      swap(a[i], a[m2]);
//      swap(a[i], a[m2]);
//      swap(b[i], b[m2]);
    }
//    else if (a[i] < xa) {
//    else if ((a[i] < xa) || ((a[i] == xa) && (b[i] < xb))) {
    else if (v[a[i]] < xa) {
      m2++;
      swap(a[i], a[m2]);
      swap(a[m1], a[m2]);
//      swap(a[i], a[m2]);
//      swap(a[m1], a[m2]);
//      swap(b[i], b[m2]);
//      swap(b[m1], b[m2]);
      m1++;
    }
  }
}

//void randomized_quick_sort(vector<double> &a, int l, int r) {
void randomized_quick_sort(vector<__int64> &a, int l, int r) {
//void randomized_quick_sort(vector<__int64> &a, vector<__int64> &b, int l, int r) {
  if (l >= r) {
    return;
  }

  int k = l + rand() % (r - l + 1);
  swap(a[l], a[k]);
//  swap(a[l], a[k]);
//  swap(b[l], b[k]);
  int m1, m2;
  partition2(a, l, r, m1, m2);
//  partition2(a, b, l, r, m1, m2);

  randomized_quick_sort(a, l, m1 - 1);
  randomized_quick_sort(a, m2 + 1, r);
//  randomized_quick_sort(a, b, l, m1 - 1);
//  randomized_quick_sort(a, b, m2 + 1, r);
}

__int64 gcd(__int64 a, __int64 b) {
    __int64 c;
    c = a % b;
    while (c) {
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

/*
int process(int i0, int i, int tmp){
    //std::cout << i << " start " << res << ' ' << tmp << std::endl;
	if (tmp > m){
        //std::cout << i << " A " << tmp << ' ' << m << std::endl;
		return 0;
	}
	if ((b[i].size() == 1) && (b[i][0] == i0)){
        //std::cout << i << " B " << std::endl;
		res++;
	}
	else{
        //std::cout << i << " C " << std::endl;
		for (int j = 0; j < b[i].size(); j++)
            if (b[i][j] != i0)
                process(i, b[i][j], (tmp + 1) * a[b[i][j]]);
	}
    //std::cout << i << "  end  " << res << ' ' << tmp << std::endl;
	return 0;
}
*/

/*
__int64 kn[105], km[105];
__int64 an[105][105], am[105][105];
__int64 bn[105], bm[105];

void process_m(int j);

void process_n(int i){
    if (bn[i])
        return;
	bn[i] = res;
	//std::cout << "n " << i+1 << ' ' << res << std::endl;
	for (int j = 0; j < kn[i]; j++)
		process_m(an[i][j]);
}

void process_m(int j){
    if (bm[j])
        return;
	bm[j] = res;
	//std::cout << "m " << j+1 << ' ' << res << std::endl;
	for (int i = 0; i < km[j]; i++)
	    process_n(am[j][i]);
}
*/

/*
void process377(int x, int y){
    if (s <= k)
        return;
    if ((x < 0) || (y < 0) || (x >= n) || (y >= m))
        return;
    switch (a[x][y]){
        case '+': break;
        case '#': break;
        case '.':
            a[x][y] = '+';
            s--;
            process377(x - 1, y);
            process377(x, y - 1);
            process377(x + 1, y);
            process377(x, y + 1);
            break;
    }
}
*/

int good(int i, int l, int r){
    int i0, i1, j00, j0d, j0n, j10, j1d, j1n, j0, j1, res;
    i0 = i - 1;
    i1 = i;
    j00 = l ? slen[i0] - 1 : 0;
    j0d = l ? -1 : +1;
    j0n = l ? -1 : slen[i0];
    j10 = r ? slen[i1] - 1 : 0;
    j1d = r ? -1 : +1;
    j1n = r ? -1 : slen[i1];
    j0 = j00;
    j1 = j10;
//cout << endl << i << ' ' << l << ' ' << r << endl;
    while (true){
        if (j0 == j0n) { res = 1; break; }
        if (j1 == j1n) { res = 0; break; }
//cout << s[i0][j0] << s[i1][j1] << ' ';
        if (s[i0][j0] < s[i1][j1]) { res = 1; break; }
        if (s[i0][j0] > s[i1][j1]) { res = 0; break; }

        j0 += j0d;
        j1 += j1d;
    }
//cout << i << ' ' << l << ' ' << r << " = " << res << endl;
    return res;
}

int goodll(int i){ return good(i, 0, 0); }
int goodlr(int i){ return good(i, 0, 1); }
int goodrl(int i){ return good(i, 1, 0); }
int goodrr(int i){ return good(i, 1, 1); }

__int64 min2(__int64 a, __int64 b){
    if (a == -1) return b;
    if (b == -1) return a;
    return std::min(a, b);
}

__int64 nok(__int64 a, __int64 b){
    return a * b / gcd(a, b);
}

__int64 task235a(__int64 a, __int64 b, __int64 c){
    __int64 d, e;
    d = nok(a, b);
    e = nok(d, c);
    return e;
}

__int64 is_prime(__int64 n){
    __int64 res = 1;
    __int64 i = sqrt(n) + 1;
    for (i; (i > 1) && res; i--){
        if (!(n % i))
            res = 0;
    }
    return res;
}

int main(){
    //std::unordered_map<int, std::unordered_map<int, int>> z;
    //std::string s;
    //vector<__int64> a(n);
    //randomized_quick_sort(a, 0, n - 1);
    //a.push_back(1);
    __int64 K = 1e9 + 7;

    __int64 a[500005], sum, a1, a2, a3, a4, a5;

    cin >> n;
    sum = 0;
    for (i = 0; i < n; ++i){
    	cin >> tmp;
    	a[i] = tmp;
    	sum += tmp;
    	res += tmp >= 0 ? tmp : -tmp;
    }
    if (n < 3)
        res = 0;
    else if (sum % 3)
        res = 0;
    else if (!res)
        res = (n - 1) * (n - 2) / 2;
    else if (!sum){
        a1 = 0;
        tmp = 0;
        for (i = 0; i < n; i++){
            tmp += a[i];
            if (!tmp)
                a1++;
        }
        n = a1;
        res = (n - 1) * (n - 2) / 2;
    }
    else{
        res = 0;
        sum /= 3;
        a1 = 0;
        i = 0;
        tmp = a[i];
        while (tmp < sum)
            tmp += a[++i];
        if (tmp == sum){
            a2 = 0;
            a3 = 0;
            i++;
            tmp = a[i];
            while (tmp < sum){
                if (!tmp)
                    a2++;
                tmp += a[++i];
            }
            if (tmp == sum){
                a4 = 0;
                a5 = 0;
                i++;
                tmp = a[i];
                while (tmp < sum){
                    if (!tmp)
                        a4++;
                    tmp += a[++i];
                }
                a2++;
                a4++;
                res = a2 * a4;
            }
        }
    }

    cout << res;
    return 0;
}


#define _USE_MATH_DEFINES

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath> // pow(10, 3)
//#include <stdint.h>
#include <iomanip> // std::cout.precision(17);
#include <vector>
#include <algorithm> // vector.find
#include <unordered_map>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::swap;
using std::map;
//using namespace std;

//__int64 n, m, n2, q, p, m2, s2, d, e, r, t;
//int s[15];
//int x[1005][1005];
//int y[15][1005][1005];
//int Z = 2000000000;

__int64 res = 0, tmp;
__int64 i, j, k, l, m, n, p, q, r;//, s, t;
__int64 x, y, z;
//__int64 c, d;
//char a[505][505];
//vector<__int64> b[105];
string s[100005];
__int64 slen[100005];
vector<__int64> a(1005), v(1005), b(1005);
vector<__int64> c[1005];

//void partition2(vector<double> &a, int l, int r, int &m1, int &m2) {
void partition2(vector<__int64> &a, int l, int r, int &m1, int &m2) {
//void partition2(vector<__int64> &a, vector<__int64> &b, int l, int r, int &m1, int &m2) {
//  double xa = a[l];
//  __int64 xa = a[l];
//  __int64 xa = a[l];
//  __int64 xb = b[l];
  __int64 xa = v[a[l]];
  m1 = l;
  m2 = l;
  for (int i = l + 1; i <= r; ++i) {
//    if (a[i] == xa) {
//    if ((a[i] == xa) && (b[i] == xb)) {
    if (v[a[i]] == xa) {
      m2++;
      swap(a[i], a[m2]);
//      swap(a[i], a[m2]);
//      swap(b[i], b[m2]);
    }
//    else if (a[i] < xa) {
//    else if ((a[i] < xa) || ((a[i] == xa) && (b[i] < xb))) {
    else if (v[a[i]] < xa) {
      m2++;
      swap(a[i], a[m2]);
      swap(a[m1], a[m2]);
//      swap(a[i], a[m2]);
//      swap(a[m1], a[m2]);
//      swap(b[i], b[m2]);
//      swap(b[m1], b[m2]);
      m1++;
    }
  }
}

//void randomized_quick_sort(vector<double> &a, int l, int r) {
void randomized_quick_sort(vector<__int64> &a, int l, int r) {
//void randomized_quick_sort(vector<__int64> &a, vector<__int64> &b, int l, int r) {
  if (l >= r) {
    return;
  }

  int k = l + rand() % (r - l + 1);
  swap(a[l], a[k]);
//  swap(a[l], a[k]);
//  swap(b[l], b[k]);
  int m1, m2;
  partition2(a, l, r, m1, m2);
//  partition2(a, b, l, r, m1, m2);

  randomized_quick_sort(a, l, m1 - 1);
  randomized_quick_sort(a, m2 + 1, r);
//  randomized_quick_sort(a, b, l, m1 - 1);
//  randomized_quick_sort(a, b, m2 + 1, r);
}

__int64 gcd(__int64 a, __int64 b) {
    __int64 c;
    c = a % b;
    while (c) {
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

/*
int process(int i0, int i, int tmp){
    //std::cout << i << " start " << res << ' ' << tmp << std::endl;
	if (tmp > m){
        //std::cout << i << " A " << tmp << ' ' << m << std::endl;
		return 0;
	}
	if ((b[i].size() == 1) && (b[i][0] == i0)){
        //std::cout << i << " B " << std::endl;
		res++;
	}
	else{
        //std::cout << i << " C " << std::endl;
		for (int j = 0; j < b[i].size(); j++)
            if (b[i][j] != i0)
                process(i, b[i][j], (tmp + 1) * a[b[i][j]]);
	}
    //std::cout << i << "  end  " << res << ' ' << tmp << std::endl;
	return 0;
}
*/

/*
__int64 kn[105], km[105];
__int64 an[105][105], am[105][105];
__int64 bn[105], bm[105];

void process_m(int j);

void process_n(int i){
    if (bn[i])
        return;
	bn[i] = res;
	//std::cout << "n " << i+1 << ' ' << res << std::endl;
	for (int j = 0; j < kn[i]; j++)
		process_m(an[i][j]);
}

void process_m(int j){
    if (bm[j])
        return;
	bm[j] = res;
	//std::cout << "m " << j+1 << ' ' << res << std::endl;
	for (int i = 0; i < km[j]; i++)
	    process_n(am[j][i]);
}
*/

/*
void process377(int x, int y){
    if (s <= k)
        return;
    if ((x < 0) || (y < 0) || (x >= n) || (y >= m))
        return;
    switch (a[x][y]){
        case '+': break;
        case '#': break;
        case '.':
            a[x][y] = '+';
            s--;
            process377(x - 1, y);
            process377(x, y - 1);
            process377(x + 1, y);
            process377(x, y + 1);
            break;
    }
}
*/

int good(int i, int l, int r){
    int i0, i1, j00, j0d, j0n, j10, j1d, j1n, j0, j1, res;
    i0 = i - 1;
    i1 = i;
    j00 = l ? slen[i0] - 1 : 0;
    j0d = l ? -1 : +1;
    j0n = l ? -1 : slen[i0];
    j10 = r ? slen[i1] - 1 : 0;
    j1d = r ? -1 : +1;
    j1n = r ? -1 : slen[i1];
    j0 = j00;
    j1 = j10;
//cout << endl << i << ' ' << l << ' ' << r << endl;
    while (true){
        if (j0 == j0n) { res = 1; break; }
        if (j1 == j1n) { res = 0; break; }
//cout << s[i0][j0] << s[i1][j1] << ' ';
        if (s[i0][j0] < s[i1][j1]) { res = 1; break; }
        if (s[i0][j0] > s[i1][j1]) { res = 0; break; }

        j0 += j0d;
        j1 += j1d;
    }
//cout << i << ' ' << l << ' ' << r << " = " << res << endl;
    return res;
}

int goodll(int i){ return good(i, 0, 0); }
int goodlr(int i){ return good(i, 0, 1); }
int goodrl(int i){ return good(i, 1, 0); }
int goodrr(int i){ return good(i, 1, 1); }

__int64 min2(__int64 a, __int64 b){
    if (a == -1) return b;
    if (b == -1) return a;
    return std::min(a, b);
}

__int64 nok(__int64 a, __int64 b){
    return a * b / gcd(a, b);
}

__int64 task235a(__int64 a, __int64 b, __int64 c){
    __int64 d, e;
    d = nok(a, b);
    e = nok(d, c);
    return e;
}

__int64 is_prime(__int64 n){
    __int64 res = 1;
    __int64 i = sqrt(n) + 1;
    for (i; (i > 1) && res; i--){
        if (!(n % i))
            res = 0;
    }
    return res;
}

int main(){
    //std::unordered_map<int, std::unordered_map<int, int>> z;
    //std::string s;
    //vector<__int64> a(n);
    //randomized_quick_sort(a, 0, n - 1);
    //a.push_back(1);
    __int64 K = 1e9 + 7;

    __int64 a[500005], b[500005], c[500005], sum, a1, a2, a3, a4, a5;

    cin >> n;
    sum = 0;
    res = 0;
    for (i = 0; i < n; ++i){
    	cin >> tmp;
    	a[i] = tmp;
    	b[i] = 0;
    	c[i] = 0;
    	sum += tmp;
    	res += tmp >= 0 ? tmp : -tmp;
    }
    if (n < 3)
        res = 0;
    else if (sum % 3)
        res = 0;
    else if (!res)
        res = (n - 1) * (n - 2) / 2;
    else if (!sum){
        a1 = 0;
        tmp = 0;
        for (i = 0; i < n; i++){
            tmp += a[i];
            if (!tmp)
                a1++;
        }
        n = a1;
        res = (n - 1) * (n - 2) / 2;
    }
    else{
        sum /= 3;
        res = 0;
        i = 0;
        tmp = a[i];
        while ((tmp != sum) && (i + 1 < n))
            tmp += a[++i];
        a2 = 0;
        tmp = 0;
        while (i + 1 < n){
            tmp += a[++i];
            if (!tmp)
                a2++;
            if (tmp == sum)
                b[i] = a2;
        }

        i = n - 1;
        tmp = a[i];
        while ((tmp != sum) && (i - 1 >= 0))
            tmp += a[--i];
        a4 = 0;
        tmp = 0;
        while (i - 1 >= 0){
            tmp += a[--i];
            if (!tmp){
                a4++;
                c[i] = a4;
                if (i - 1 >= 0)
                    if ((b[i - 1] + 1) * (c[i] + 1) > res)
                        res = (b[i - 1] + 1) * (c[i] + 1);
            }
        }
    }

    cout << res;
    return 0;
}


#define _USE_MATH_DEFINES

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath> // pow(10, 3)
//#include <stdint.h>
#include <iomanip> // std::cout.precision(17);
#include <vector>
#include <algorithm> // vector.find
#include <unordered_map>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::swap;
using std::map;
//using namespace std;

//__int64 n, m, n2, q, p, m2, s2, d, e, r, t;
//int s[15];
//int x[1005][1005];
//int y[15][1005][1005];
//int Z = 2000000000;

__int64 res = 0, tmp;
__int64 i, j, k, l, m, n, p, q, r;//, s, t;
__int64 x, y, z;
//__int64 c, d;
//char a[505][505];
//vector<__int64> b[105];
string s[100005];
__int64 slen[100005];
vector<__int64> a(1005), v(1005), b(1005);
vector<__int64> c[1005];

//void partition2(vector<double> &a, int l, int r, int &m1, int &m2) {
void partition2(vector<__int64> &a, int l, int r, int &m1, int &m2) {
//void partition2(vector<__int64> &a, vector<__int64> &b, int l, int r, int &m1, int &m2) {
//  double xa = a[l];
//  __int64 xa = a[l];
//  __int64 xa = a[l];
//  __int64 xb = b[l];
  __int64 xa = v[a[l]];
  m1 = l;
  m2 = l;
  for (int i = l + 1; i <= r; ++i) {
//    if (a[i] == xa) {
//    if ((a[i] == xa) && (b[i] == xb)) {
    if (v[a[i]] == xa) {
      m2++;
      swap(a[i], a[m2]);
//      swap(a[i], a[m2]);
//      swap(b[i], b[m2]);
    }
//    else if (a[i] < xa) {
//    else if ((a[i] < xa) || ((a[i] == xa) && (b[i] < xb))) {
    else if (v[a[i]] < xa) {
      m2++;
      swap(a[i], a[m2]);
      swap(a[m1], a[m2]);
//      swap(a[i], a[m2]);
//      swap(a[m1], a[m2]);
//      swap(b[i], b[m2]);
//      swap(b[m1], b[m2]);
      m1++;
    }
  }
}

//void randomized_quick_sort(vector<double> &a, int l, int r) {
void randomized_quick_sort(vector<__int64> &a, int l, int r) {
//void randomized_quick_sort(vector<__int64> &a, vector<__int64> &b, int l, int r) {
  if (l >= r) {
    return;
  }

  int k = l + rand() % (r - l + 1);
  swap(a[l], a[k]);
//  swap(a[l], a[k]);
//  swap(b[l], b[k]);
  int m1, m2;
  partition2(a, l, r, m1, m2);
//  partition2(a, b, l, r, m1, m2);

  randomized_quick_sort(a, l, m1 - 1);
  randomized_quick_sort(a, m2 + 1, r);
//  randomized_quick_sort(a, b, l, m1 - 1);
//  randomized_quick_sort(a, b, m2 + 1, r);
}

__int64 gcd(__int64 a, __int64 b) {
    __int64 c;
    c = a % b;
    while (c) {
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

/*
int process(int i0, int i, int tmp){
    //std::cout << i << " start " << res << ' ' << tmp << std::endl;
	if (tmp > m){
        //std::cout << i << " A " << tmp << ' ' << m << std::endl;
		return 0;
	}
	if ((b[i].size() == 1) && (b[i][0] == i0)){
        //std::cout << i << " B " << std::endl;
		res++;
	}
	else{
        //std::cout << i << " C " << std::endl;
		for (int j = 0; j < b[i].size(); j++)
            if (b[i][j] != i0)
                process(i, b[i][j], (tmp + 1) * a[b[i][j]]);
	}
    //std::cout << i << "  end  " << res << ' ' << tmp << std::endl;
	return 0;
}
*/

/*
__int64 kn[105], km[105];
__int64 an[105][105], am[105][105];
__int64 bn[105], bm[105];

void process_m(int j);

void process_n(int i){
    if (bn[i])
        return;
	bn[i] = res;
	//std::cout << "n " << i+1 << ' ' << res << std::endl;
	for (int j = 0; j < kn[i]; j++)
		process_m(an[i][j]);
}

void process_m(int j){
    if (bm[j])
        return;
	bm[j] = res;
	//std::cout << "m " << j+1 << ' ' << res << std::endl;
	for (int i = 0; i < km[j]; i++)
	    process_n(am[j][i]);
}
*/

/*
void process377(int x, int y){
    if (s <= k)
        return;
    if ((x < 0) || (y < 0) || (x >= n) || (y >= m))
        return;
    switch (a[x][y]){
        case '+': break;
        case '#': break;
        case '.':
            a[x][y] = '+';
            s--;
            process377(x - 1, y);
            process377(x, y - 1);
            process377(x + 1, y);
            process377(x, y + 1);
            break;
    }
}
*/

int good(int i, int l, int r){
    int i0, i1, j00, j0d, j0n, j10, j1d, j1n, j0, j1, res;
    i0 = i - 1;
    i1 = i;
    j00 = l ? slen[i0] - 1 : 0;
    j0d = l ? -1 : +1;
    j0n = l ? -1 : slen[i0];
    j10 = r ? slen[i1] - 1 : 0;
    j1d = r ? -1 : +1;
    j1n = r ? -1 : slen[i1];
    j0 = j00;
    j1 = j10;
//cout << endl << i << ' ' << l << ' ' << r << endl;
    while (true){
        if (j0 == j0n) { res = 1; break; }
        if (j1 == j1n) { res = 0; break; }
//cout << s[i0][j0] << s[i1][j1] << ' ';
        if (s[i0][j0] < s[i1][j1]) { res = 1; break; }
        if (s[i0][j0] > s[i1][j1]) { res = 0; break; }

        j0 += j0d;
        j1 += j1d;
    }
//cout << i << ' ' << l << ' ' << r << " = " << res << endl;
    return res;
}

int goodll(int i){ return good(i, 0, 0); }
int goodlr(int i){ return good(i, 0, 1); }
int goodrl(int i){ return good(i, 1, 0); }
int goodrr(int i){ return good(i, 1, 1); }

__int64 min2(__int64 a, __int64 b){
    if (a == -1) return b;
    if (b == -1) return a;
    return std::min(a, b);
}

__int64 nok(__int64 a, __int64 b){
    return a * b / gcd(a, b);
}

__int64 task235a(__int64 a, __int64 b, __int64 c){
    __int64 d, e;
    d = nok(a, b);
    e = nok(d, c);
    return e;
}

__int64 is_prime(__int64 n){
    __int64 res = 1;
    __int64 i = sqrt(n) + 1;
    for (i; (i > 1) && res; i--){
        if (!(n % i))
            res = 0;
    }
    return res;
}

int main(){
    //std::unordered_map<int, std::unordered_map<int, int>> z;
    //std::string s;
    //vector<__int64> a(n);
    //randomized_quick_sort(a, 0, n - 1);
    //a.push_back(1);
    __int64 K = 1e9 + 7;

    __int64 a[500005], sum, a1, a2, a3, a4, a5;

    cin >> n;
    sum = 0;
    for (i = 0; i < n; ++i){
    	cin >> tmp;
    	a[i] = tmp;
    	sum += tmp;
    	res += tmp >= 0 ? tmp : -tmp;
    }
    if (n < 3)
        res = 0;
    else if (sum % 3)
        res = 0;
    else if (!res)
        res = (n - 1) * (n - 2) / 2;
    else if (!sum){
        a1 = 0;
        tmp = 0;
        for (i = 0; i < n; i++){
            tmp += a[i];
            if (!tmp)
                a1++;
        }
        n = a1;
        res = (n - 1) * (n - 2) / 2;
    }
    else{
        res = 0;
        sum /= 3;
        a1 = 0;
        i = 0;
        tmp = a[i];
        while (tmp != sum)
            tmp += a[++i];
        if (tmp == sum){
            a2 = 0;
            a3 = 0;
            i++;
            tmp = a[i];
            while (tmp != sum){
                if (!tmp)
                    a2++;
                tmp += a[++i];
            }
            if (tmp == sum){
                a4 = 0;
                a5 = 0;
                i++;
                tmp = a[i];
                while (tmp < sum){
                    if (!tmp)
                        a4++;
                    tmp += a[++i];
                }
                a2++;
                a4++;
                res = a2 * a4;
            }
        }
    }

    cout << res;
    return 0;
}


#define _USE_MATH_DEFINES

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath> // pow(10, 3)
//#include <stdint.h>
#include <iomanip> // std::cout.precision(17);
#include <vector>
#include <algorithm> // vector.find
#include <unordered_map>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::swap;
using std::map;
//using namespace std;

//__int64 n, m, n2, q, p, m2, s2, d, e, r, t;
//int s[15];
//int x[1005][1005];
//int y[15][1005][1005];
//int Z = 2000000000;

__int64 res = 0, tmp;
__int64 i, j, k, l, m, n, p, q, r;//, s, t;
__int64 x, y, z;
//__int64 c, d;
//char a[505][505];
//vector<__int64> b[105];
string s[100005];
__int64 slen[100005];
vector<__int64> a(1005), v(1005), b(1005);
vector<__int64> c[1005];

//void partition2(vector<double> &a, int l, int r, int &m1, int &m2) {
void partition2(vector<__int64> &a, int l, int r, int &m1, int &m2) {
//void partition2(vector<__int64> &a, vector<__int64> &b, int l, int r, int &m1, int &m2) {
//  double xa = a[l];
//  __int64 xa = a[l];
//  __int64 xa = a[l];
//  __int64 xb = b[l];
  __int64 xa = v[a[l]];
  m1 = l;
  m2 = l;
  for (int i = l + 1; i <= r; ++i) {
//    if (a[i] == xa) {
//    if ((a[i] == xa) && (b[i] == xb)) {
    if (v[a[i]] == xa) {
      m2++;
      swap(a[i], a[m2]);
//      swap(a[i], a[m2]);
//      swap(b[i], b[m2]);
    }
//    else if (a[i] < xa) {
//    else if ((a[i] < xa) || ((a[i] == xa) && (b[i] < xb))) {
    else if (v[a[i]] < xa) {
      m2++;
      swap(a[i], a[m2]);
      swap(a[m1], a[m2]);
//      swap(a[i], a[m2]);
//      swap(a[m1], a[m2]);
//      swap(b[i], b[m2]);
//      swap(b[m1], b[m2]);
      m1++;
    }
  }
}

//void randomized_quick_sort(vector<double> &a, int l, int r) {
void randomized_quick_sort(vector<__int64> &a, int l, int r) {
//void randomized_quick_sort(vector<__int64> &a, vector<__int64> &b, int l, int r) {
  if (l >= r) {
    return;
  }

  int k = l + rand() % (r - l + 1);
  swap(a[l], a[k]);
//  swap(a[l], a[k]);
//  swap(b[l], b[k]);
  int m1, m2;
  partition2(a, l, r, m1, m2);
//  partition2(a, b, l, r, m1, m2);

  randomized_quick_sort(a, l, m1 - 1);
  randomized_quick_sort(a, m2 + 1, r);
//  randomized_quick_sort(a, b, l, m1 - 1);
//  randomized_quick_sort(a, b, m2 + 1, r);
}

__int64 gcd(__int64 a, __int64 b) {
    __int64 c;
    c = a % b;
    while (c) {
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

/*
int process(int i0, int i, int tmp){
    //std::cout << i << " start " << res << ' ' << tmp << std::endl;
	if (tmp > m){
        //std::cout << i << " A " << tmp << ' ' << m << std::endl;
		return 0;
	}
	if ((b[i].size() == 1) && (b[i][0] == i0)){
        //std::cout << i << " B " << std::endl;
		res++;
	}
	else{
        //std::cout << i << " C " << std::endl;
		for (int j = 0; j < b[i].size(); j++)
            if (b[i][j] != i0)
                process(i, b[i][j], (tmp + 1) * a[b[i][j]]);
	}
    //std::cout << i << "  end  " << res << ' ' << tmp << std::endl;
	return 0;
}
*/

/*
__int64 kn[105], km[105];
__int64 an[105][105], am[105][105];
__int64 bn[105], bm[105];

void process_m(int j);

void process_n(int i){
    if (bn[i])
        return;
	bn[i] = res;
	//std::cout << "n " << i+1 << ' ' << res << std::endl;
	for (int j = 0; j < kn[i]; j++)
		process_m(an[i][j]);
}

void process_m(int j){
    if (bm[j])
        return;
	bm[j] = res;
	//std::cout << "m " << j+1 << ' ' << res << std::endl;
	for (int i = 0; i < km[j]; i++)
	    process_n(am[j][i]);
}
*/

/*
void process377(int x, int y){
    if (s <= k)
        return;
    if ((x < 0) || (y < 0) || (x >= n) || (y >= m))
        return;
    switch (a[x][y]){
        case '+': break;
        case '#': break;
        case '.':
            a[x][y] = '+';
            s--;
            process377(x - 1, y);
            process377(x, y - 1);
            process377(x + 1, y);
            process377(x, y + 1);
            break;
    }
}
*/

int good(int i, int l, int r){
    int i0, i1, j00, j0d, j0n, j10, j1d, j1n, j0, j1, res;
    i0 = i - 1;
    i1 = i;
    j00 = l ? slen[i0] - 1 : 0;
    j0d = l ? -1 : +1;
    j0n = l ? -1 : slen[i0];
    j10 = r ? slen[i1] - 1 : 0;
    j1d = r ? -1 : +1;
    j1n = r ? -1 : slen[i1];
    j0 = j00;
    j1 = j10;
//cout << endl << i << ' ' << l << ' ' << r << endl;
    while (true){
        if (j0 == j0n) { res = 1; break; }
        if (j1 == j1n) { res = 0; break; }
//cout << s[i0][j0] << s[i1][j1] << ' ';
        if (s[i0][j0] < s[i1][j1]) { res = 1; break; }
        if (s[i0][j0] > s[i1][j1]) { res = 0; break; }

        j0 += j0d;
        j1 += j1d;
    }
//cout << i << ' ' << l << ' ' << r << " = " << res << endl;
    return res;
}

int goodll(int i){ return good(i, 0, 0); }
int goodlr(int i){ return good(i, 0, 1); }
int goodrl(int i){ return good(i, 1, 0); }
int goodrr(int i){ return good(i, 1, 1); }

__int64 min2(__int64 a, __int64 b){
    if (a == -1) return b;
    if (b == -1) return a;
    return std::min(a, b);
}

__int64 nok(__int64 a, __int64 b){
    return a * b / gcd(a, b);
}

__int64 task235a(__int64 a, __int64 b, __int64 c){
    __int64 d, e;
    d = nok(a, b);
    e = nok(d, c);
    return e;
}

__int64 is_prime(__int64 n){
    __int64 res = 1;
    __int64 i = sqrt(n) + 1;
    for (i; (i > 1) && res; i--){
        if (!(n % i))
            res = 0;
    }
    return res;
}

int main(){
    //std::unordered_map<int, std::unordered_map<int, int>> z;
    //std::string s;
    //vector<__int64> a(n);
    //randomized_quick_sort(a, 0, n - 1);
    //a.push_back(1);
    __int64 K = 1e9 + 7;

    __int64 a[500005], b[500005], c[500005], sum, a1, a2, a3, a4, a5;

    cin >> n;
    sum = 0;
    res = 0;
    for (i = 0; i < n; ++i){
    	cin >> tmp;
    	a[i] = tmp;
    	b[i] = -1;
    	c[i] = -1;
    	sum += tmp;
    	res += tmp >= 0 ? tmp : -tmp;
    }
    if (n < 3)
        res = 0;
    else if (sum % 3)
        res = 0;
    else if (!res)
        res = (n - 1) * (n - 2) / 2;
    else if (!sum){
        a1 = 0;
        tmp = 0;
        for (i = 0; i < n; i++){
            tmp += a[i];
            if (!tmp)
                a1++;
        }
        n = a1;
        res = (n - 1) * (n - 2) / 2;
    }
    else{
        sum /= 3;
        res = 0;

        i = 0;
        tmp = a[i];
        while ((tmp != sum) && (i + 1 < n))
            tmp += a[++i];
        b[i] = 0;
        a2 = 0;
        tmp = 0;
        while (i + 1 < n){
            tmp += a[++i];
            if (!tmp)
                a2++;
            if (tmp == sum)
                b[i] = a2;
        }

        i = n - 1;
        tmp = a[i];
        while ((tmp != sum) && (i - 1 >= 0))
            tmp += a[--i];
        c[i] 0;
        a4 = 0;
        tmp = 0;
        while (i - 1 >= 0){
            tmp += a[--i];
            if (!tmp){
                a4++;
                c[i] = a4;
                if (i - 1 >= 0)
                    if ((b[i - 1] + 1) * (c[i] + 1) > res)
                        res = (b[i - 1] + 1) * (c[i] + 1);
            }
        }
    }

    cout << res;
    return 0;
}


#define _USE_MATH_DEFINES

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath> // pow(10, 3)
//#include <stdint.h>
#include <iomanip> // std::cout.precision(17);
#include <vector>
#include <algorithm> // vector.find
#include <unordered_map>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::swap;
using std::map;
//using namespace std;

//__int64 n, m, n2, q, p, m2, s2, d, e, r, t;
//int s[15];
//int x[1005][1005];
//int y[15][1005][1005];
//int Z = 2000000000;

__int64 res = 0, tmp;
__int64 i, j, k, l, m, n, p, q, r;//, s, t;
__int64 x, y, z;
//__int64 c, d;
//char a[505][505];
//vector<__int64> b[105];
string s[100005];
__int64 slen[100005];
vector<__int64> a(1005), v(1005), b(1005);
vector<__int64> c[1005];

//void partition2(vector<double> &a, int l, int r, int &m1, int &m2) {
void partition2(vector<__int64> &a, int l, int r, int &m1, int &m2) {
//void partition2(vector<__int64> &a, vector<__int64> &b, int l, int r, int &m1, int &m2) {
//  double xa = a[l];
//  __int64 xa = a[l];
//  __int64 xa = a[l];
//  __int64 xb = b[l];
  __int64 xa = v[a[l]];
  m1 = l;
  m2 = l;
  for (int i = l + 1; i <= r; ++i) {
//    if (a[i] == xa) {
//    if ((a[i] == xa) && (b[i] == xb)) {
    if (v[a[i]] == xa) {
      m2++;
      swap(a[i], a[m2]);
//      swap(a[i], a[m2]);
//      swap(b[i], b[m2]);
    }
//    else if (a[i] < xa) {
//    else if ((a[i] < xa) || ((a[i] == xa) && (b[i] < xb))) {
    else if (v[a[i]] < xa) {
      m2++;
      swap(a[i], a[m2]);
      swap(a[m1], a[m2]);
//      swap(a[i], a[m2]);
//      swap(a[m1], a[m2]);
//      swap(b[i], b[m2]);
//      swap(b[m1], b[m2]);
      m1++;
    }
  }
}

//void randomized_quick_sort(vector<double> &a, int l, int r) {
void randomized_quick_sort(vector<__int64> &a, int l, int r) {
//void randomized_quick_sort(vector<__int64> &a, vector<__int64> &b, int l, int r) {
  if (l >= r) {
    return;
  }

  int k = l + rand() % (r - l + 1);
  swap(a[l], a[k]);
//  swap(a[l], a[k]);
//  swap(b[l], b[k]);
  int m1, m2;
  partition2(a, l, r, m1, m2);
//  partition2(a, b, l, r, m1, m2);

  randomized_quick_sort(a, l, m1 - 1);
  randomized_quick_sort(a, m2 + 1, r);
//  randomized_quick_sort(a, b, l, m1 - 1);
//  randomized_quick_sort(a, b, m2 + 1, r);
}

__int64 gcd(__int64 a, __int64 b) {
    __int64 c;
    c = a % b;
    while (c) {
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

/*
int process(int i0, int i, int tmp){
    //std::cout << i << " start " << res << ' ' << tmp << std::endl;
	if (tmp > m){
        //std::cout << i << " A " << tmp << ' ' << m << std::endl;
		return 0;
	}
	if ((b[i].size() == 1) && (b[i][0] == i0)){
        //std::cout << i << " B " << std::endl;
		res++;
	}
	else{
        //std::cout << i << " C " << std::endl;
		for (int j = 0; j < b[i].size(); j++)
            if (b[i][j] != i0)
                process(i, b[i][j], (tmp + 1) * a[b[i][j]]);
	}
    //std::cout << i << "  end  " << res << ' ' << tmp << std::endl;
	return 0;
}
*/

/*
__int64 kn[105], km[105];
__int64 an[105][105], am[105][105];
__int64 bn[105], bm[105];

void process_m(int j);

void process_n(int i){
    if (bn[i])
        return;
	bn[i] = res;
	//std::cout << "n " << i+1 << ' ' << res << std::endl;
	for (int j = 0; j < kn[i]; j++)
		process_m(an[i][j]);
}

void process_m(int j){
    if (bm[j])
        return;
	bm[j] = res;
	//std::cout << "m " << j+1 << ' ' << res << std::endl;
	for (int i = 0; i < km[j]; i++)
	    process_n(am[j][i]);
}
*/

/*
void process377(int x, int y){
    if (s <= k)
        return;
    if ((x < 0) || (y < 0) || (x >= n) || (y >= m))
        return;
    switch (a[x][y]){
        case '+': break;
        case '#': break;
        case '.':
            a[x][y] = '+';
            s--;
            process377(x - 1, y);
            process377(x, y - 1);
            process377(x + 1, y);
            process377(x, y + 1);
            break;
    }
}
*/

int good(int i, int l, int r){
    int i0, i1, j00, j0d, j0n, j10, j1d, j1n, j0, j1, res;
    i0 = i - 1;
    i1 = i;
    j00 = l ? slen[i0] - 1 : 0;
    j0d = l ? -1 : +1;
    j0n = l ? -1 : slen[i0];
    j10 = r ? slen[i1] - 1 : 0;
    j1d = r ? -1 : +1;
    j1n = r ? -1 : slen[i1];
    j0 = j00;
    j1 = j10;
//cout << endl << i << ' ' << l << ' ' << r << endl;
    while (true){
        if (j0 == j0n) { res = 1; break; }
        if (j1 == j1n) { res = 0; break; }
//cout << s[i0][j0] << s[i1][j1] << ' ';
        if (s[i0][j0] < s[i1][j1]) { res = 1; break; }
        if (s[i0][j0] > s[i1][j1]) { res = 0; break; }

        j0 += j0d;
        j1 += j1d;
    }
//cout << i << ' ' << l << ' ' << r << " = " << res << endl;
    return res;
}

int goodll(int i){ return good(i, 0, 0); }
int goodlr(int i){ return good(i, 0, 1); }
int goodrl(int i){ return good(i, 1, 0); }
int goodrr(int i){ return good(i, 1, 1); }

__int64 min2(__int64 a, __int64 b){
    if (a == -1) return b;
    if (b == -1) return a;
    return std::min(a, b);
}

__int64 nok(__int64 a, __int64 b){
    return a * b / gcd(a, b);
}

__int64 task235a(__int64 a, __int64 b, __int64 c){
    __int64 d, e;
    d = nok(a, b);
    e = nok(d, c);
    return e;
}

__int64 is_prime(__int64 n){
    __int64 res = 1;
    __int64 i = sqrt(n) + 1;
    for (i; (i > 1) && res; i--){
        if (!(n % i))
            res = 0;
    }
    return res;
}

int main(){
    //std::unordered_map<int, std::unordered_map<int, int>> z;
    //std::string s;
    //vector<__int64> a(n);
    //randomized_quick_sort(a, 0, n - 1);
    //a.push_back(1);
    __int64 K = 1e9 + 7;

    __int64 a[500005], b[500005], c[500005], sum, a1, a2, a3, a4, a5;

    cin >> n;
    sum = 0;
    res = 0;
    for (i = 0; i < n; ++i){
    	cin >> tmp;
    	a[i] = tmp;
    	b[i] = -1;
    	c[i] = -1;
    	sum += tmp;
    	res += tmp >= 0 ? tmp : -tmp;
    }
    if (n < 3)
        res = 0;
    else if (sum % 3)
        res = 0;
    else if (!res)
        res = (n - 1) * (n - 2) / 2;
    else if (!sum){
        a1 = 0;
        tmp = 0;
        for (i = 0; i < n; i++){
            tmp += a[i];
            if (!tmp)
                a1++;
        }
        n = a1;
        res = (n - 1) * (n - 2) / 2;
    }
    else{
        sum /= 3;
        res = 0;

        i = 0;
        tmp = a[i];
        while ((tmp != sum) && (i + 1 < n))
            tmp += a[++i];
        b[i] = 0;
        a2 = 0;
        tmp = 0;
        while (i + 1 < n){
            tmp += a[++i];
            if (!tmp)
                a2++;
            if (tmp == sum)
                b[i] = a2;
        }

        i = n - 1;
        tmp = a[i];
        while ((tmp != sum) && (i - 1 >= 0))
            tmp += a[--i];
        c[i] = 0;
        a4 = 0;
        tmp = 0;
        while (i - 1 >= 0){
            tmp += a[--i];
            if (!tmp){
                a4++;
                c[i] = a4;
                if (i - 1 >= 0)
                    if ((b[i - 1] + 1) * (c[i] + 1) > res)
                        res = (b[i - 1] + 1) * (c[i] + 1);
            }
        }
    }

    cout << res;
    return 0;
}


#define _USE_MATH_DEFINES

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath> // pow(10, 3)
//#include <stdint.h>
#include <iomanip> // std::cout.precision(17);
#include <vector>
#include <algorithm> // vector.find
#include <unordered_map>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::swap;
using std::map;
//using namespace std;

//__int64 n, m, n2, q, p, m2, s2, d, e, r, t;
//int s[15];
//int x[1005][1005];
//int y[15][1005][1005];
//int Z = 2000000000;

__int64 res = 0, tmp;
__int64 i, j, k, l, m, n, p, q, r;//, s, t;
__int64 x, y, z;
//__int64 c, d;
//char a[505][505];
//vector<__int64> b[105];
string s[100005];
__int64 slen[100005];
vector<__int64> a(1005), v(1005), b(1005);
vector<__int64> c[1005];

//void partition2(vector<double> &a, int l, int r, int &m1, int &m2) {
void partition2(vector<__int64> &a, int l, int r, int &m1, int &m2) {
//void partition2(vector<__int64> &a, vector<__int64> &b, int l, int r, int &m1, int &m2) {
//  double xa = a[l];
//  __int64 xa = a[l];
//  __int64 xa = a[l];
//  __int64 xb = b[l];
  __int64 xa = v[a[l]];
  m1 = l;
  m2 = l;
  for (int i = l + 1; i <= r; ++i) {
//    if (a[i] == xa) {
//    if ((a[i] == xa) && (b[i] == xb)) {
    if (v[a[i]] == xa) {
      m2++;
      swap(a[i], a[m2]);
//      swap(a[i], a[m2]);
//      swap(b[i], b[m2]);
    }
//    else if (a[i] < xa) {
//    else if ((a[i] < xa) || ((a[i] == xa) && (b[i] < xb))) {
    else if (v[a[i]] < xa) {
      m2++;
      swap(a[i], a[m2]);
      swap(a[m1], a[m2]);
//      swap(a[i], a[m2]);
//      swap(a[m1], a[m2]);
//      swap(b[i], b[m2]);
//      swap(b[m1], b[m2]);
      m1++;
    }
  }
}

//void randomized_quick_sort(vector<double> &a, int l, int r) {
void randomized_quick_sort(vector<__int64> &a, int l, int r) {
//void randomized_quick_sort(vector<__int64> &a, vector<__int64> &b, int l, int r) {
  if (l >= r) {
    return;
  }

  int k = l + rand() % (r - l + 1);
  swap(a[l], a[k]);
//  swap(a[l], a[k]);
//  swap(b[l], b[k]);
  int m1, m2;
  partition2(a, l, r, m1, m2);
//  partition2(a, b, l, r, m1, m2);

  randomized_quick_sort(a, l, m1 - 1);
  randomized_quick_sort(a, m2 + 1, r);
//  randomized_quick_sort(a, b, l, m1 - 1);
//  randomized_quick_sort(a, b, m2 + 1, r);
}

__int64 gcd(__int64 a, __int64 b) {
    __int64 c;
    c = a % b;
    while (c) {
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

/*
int process(int i0, int i, int tmp){
    //std::cout << i << " start " << res << ' ' << tmp << std::endl;
	if (tmp > m){
        //std::cout << i << " A " << tmp << ' ' << m << std::endl;
		return 0;
	}
	if ((b[i].size() == 1) && (b[i][0] == i0)){
        //std::cout << i << " B " << std::endl;
		res++;
	}
	else{
        //std::cout << i << " C " << std::endl;
		for (int j = 0; j < b[i].size(); j++)
            if (b[i][j] != i0)
                process(i, b[i][j], (tmp + 1) * a[b[i][j]]);
	}
    //std::cout << i << "  end  " << res << ' ' << tmp << std::endl;
	return 0;
}
*/

/*
__int64 kn[105], km[105];
__int64 an[105][105], am[105][105];
__int64 bn[105], bm[105];

void process_m(int j);

void process_n(int i){
    if (bn[i])
        return;
	bn[i] = res;
	//std::cout << "n " << i+1 << ' ' << res << std::endl;
	for (int j = 0; j < kn[i]; j++)
		process_m(an[i][j]);
}

void process_m(int j){
    if (bm[j])
        return;
	bm[j] = res;
	//std::cout << "m " << j+1 << ' ' << res << std::endl;
	for (int i = 0; i < km[j]; i++)
	    process_n(am[j][i]);
}
*/

/*
void process377(int x, int y){
    if (s <= k)
        return;
    if ((x < 0) || (y < 0) || (x >= n) || (y >= m))
        return;
    switch (a[x][y]){
        case '+': break;
        case '#': break;
        case '.':
            a[x][y] = '+';
            s--;
            process377(x - 1, y);
            process377(x, y - 1);
            process377(x + 1, y);
            process377(x, y + 1);
            break;
    }
}
*/

int good(int i, int l, int r){
    int i0, i1, j00, j0d, j0n, j10, j1d, j1n, j0, j1, res;
    i0 = i - 1;
    i1 = i;
    j00 = l ? slen[i0] - 1 : 0;
    j0d = l ? -1 : +1;
    j0n = l ? -1 : slen[i0];
    j10 = r ? slen[i1] - 1 : 0;
    j1d = r ? -1 : +1;
    j1n = r ? -1 : slen[i1];
    j0 = j00;
    j1 = j10;
//cout << endl << i << ' ' << l << ' ' << r << endl;
    while (true){
        if (j0 == j0n) { res = 1; break; }
        if (j1 == j1n) { res = 0; break; }
//cout << s[i0][j0] << s[i1][j1] << ' ';
        if (s[i0][j0] < s[i1][j1]) { res = 1; break; }
        if (s[i0][j0] > s[i1][j1]) { res = 0; break; }

        j0 += j0d;
        j1 += j1d;
    }
//cout << i << ' ' << l << ' ' << r << " = " << res << endl;
    return res;
}

int goodll(int i){ return good(i, 0, 0); }
int goodlr(int i){ return good(i, 0, 1); }
int goodrl(int i){ return good(i, 1, 0); }
int goodrr(int i){ return good(i, 1, 1); }

__int64 min2(__int64 a, __int64 b){
    if (a == -1) return b;
    if (b == -1) return a;
    return std::min(a, b);
}

__int64 nok(__int64 a, __int64 b){
    return a * b / gcd(a, b);
}

__int64 task235a(__int64 a, __int64 b, __int64 c){
    __int64 d, e;
    d = nok(a, b);
    e = nok(d, c);
    return e;
}

__int64 is_prime(__int64 n){
    __int64 res = 1;
    __int64 i = sqrt(n) + 1;
    for (i; (i > 1) && res; i--){
        if (!(n % i))
            res = 0;
    }
    return res;
}

int main(){
    //std::unordered_map<int, std::unordered_map<int, int>> z;
    //std::string s;
    //vector<__int64> a(n);
    //randomized_quick_sort(a, 0, n - 1);
    //a.push_back(1);
    __int64 K = 1e9 + 7;

    __int64 a[500005], sum, a1, a2, a3, a4, a5;

    cin >> n;
    sum = 0;
    res = 0;
    for (i = 0; i < n; ++i){
    	cin >> tmp;
    	a[i] = tmp;
    	sum += tmp;
    	res += tmp >= 0 ? tmp : -tmp;
    }
    if (n < 3)
        res = 0;
    else if (sum % 3)
        res = 0;
    else if (!res)
        res = (n - 1) * (n - 2) / 2;
    else if (!sum){
        a1 = 0;
        tmp = 0;
        for (i = 0; i < n; i++){
            tmp += a[i];
            if (!tmp)
                a1++;
        }
        n = a1;
        res = (n - 1) * (n - 2) / 2;
    }
    else{
        res = 0;
        sum /= 3;
        a1 = 0;
        i = 0;
        tmp = a[i];
        while ((tmp != sum) && (i + 1 < n))
            tmp += a[++i];
        if (tmp == sum){
            a2 = 0;
            a3 = 0;
            i++;
            tmp = a[i];
            while ((tmp != sum) && (i + 1 < n)){
                if (!tmp)
                    a2++;
                tmp += a[++i];
            }
            if (tmp == sum){
                a4 = 0;
                a5 = 0;
                i++;
                tmp = a[i];
                while ((tmp != sum) && (i + 1 < n)){
                    if (!tmp)
                        a4++;
                    tmp += a[++i];
                }
                a2++;
                a4++;
                res = a2 * a4;
            }
        }
    }

    cout << res;
    return 0;
}


#define _USE_MATH_DEFINES

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath> // pow(10, 3)
//#include <stdint.h>
#include <iomanip> // std::cout.precision(17);
#include <vector>
#include <algorithm> // vector.find
#include <unordered_map>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::swap;
using std::map;
//using namespace std;

//__int64 n, m, n2, q, p, m2, s2, d, e, r, t;
//int s[15];
//int x[1005][1005];
//int y[15][1005][1005];
//int Z = 2000000000;

__int64 res = 0, tmp;
__int64 i, j, k, l, m, n, p, q, r;//, s, t;
__int64 x, y, z;
//__int64 c, d;
//char a[505][505];
//vector<__int64> b[105];
string s[100005];
__int64 slen[100005];
vector<__int64> a(1005), v(1005), b(1005);
vector<__int64> c[1005];

//void partition2(vector<double> &a, int l, int r, int &m1, int &m2) {
void partition2(vector<__int64> &a, int l, int r, int &m1, int &m2) {
//void partition2(vector<__int64> &a, vector<__int64> &b, int l, int r, int &m1, int &m2) {
//  double xa = a[l];
//  __int64 xa = a[l];
//  __int64 xa = a[l];
//  __int64 xb = b[l];
  __int64 xa = v[a[l]];
  m1 = l;
  m2 = l;
  for (int i = l + 1; i <= r; ++i) {
//    if (a[i] == xa) {
//    if ((a[i] == xa) && (b[i] == xb)) {
    if (v[a[i]] == xa) {
      m2++;
      swap(a[i], a[m2]);
//      swap(a[i], a[m2]);
//      swap(b[i], b[m2]);
    }
//    else if (a[i] < xa) {
//    else if ((a[i] < xa) || ((a[i] == xa) && (b[i] < xb))) {
    else if (v[a[i]] < xa) {
      m2++;
      swap(a[i], a[m2]);
      swap(a[m1], a[m2]);
//      swap(a[i], a[m2]);
//      swap(a[m1], a[m2]);
//      swap(b[i], b[m2]);
//      swap(b[m1], b[m2]);
      m1++;
    }
  }
}

//void randomized_quick_sort(vector<double> &a, int l, int r) {
void randomized_quick_sort(vector<__int64> &a, int l, int r) {
//void randomized_quick_sort(vector<__int64> &a, vector<__int64> &b, int l, int r) {
  if (l >= r) {
    return;
  }

  int k = l + rand() % (r - l + 1);
  swap(a[l], a[k]);
//  swap(a[l], a[k]);
//  swap(b[l], b[k]);
  int m1, m2;
  partition2(a, l, r, m1, m2);
//  partition2(a, b, l, r, m1, m2);

  randomized_quick_sort(a, l, m1 - 1);
  randomized_quick_sort(a, m2 + 1, r);
//  randomized_quick_sort(a, b, l, m1 - 1);
//  randomized_quick_sort(a, b, m2 + 1, r);
}

__int64 gcd(__int64 a, __int64 b) {
    __int64 c;
    c = a % b;
    while (c) {
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

/*
int process(int i0, int i, int tmp){
    //std::cout << i << " start " << res << ' ' << tmp << std::endl;
	if (tmp > m){
        //std::cout << i << " A " << tmp << ' ' << m << std::endl;
		return 0;
	}
	if ((b[i].size() == 1) && (b[i][0] == i0)){
        //std::cout << i << " B " << std::endl;
		res++;
	}
	else{
        //std::cout << i << " C " << std::endl;
		for (int j = 0; j < b[i].size(); j++)
            if (b[i][j] != i0)
                process(i, b[i][j], (tmp + 1) * a[b[i][j]]);
	}
    //std::cout << i << "  end  " << res << ' ' << tmp << std::endl;
	return 0;
}
*/

/*
__int64 kn[105], km[105];
__int64 an[105][105], am[105][105];
__int64 bn[105], bm[105];

void process_m(int j);

void process_n(int i){
    if (bn[i])
        return;
	bn[i] = res;
	//std::cout << "n " << i+1 << ' ' << res << std::endl;
	for (int j = 0; j < kn[i]; j++)
		process_m(an[i][j]);
}

void process_m(int j){
    if (bm[j])
        return;
	bm[j] = res;
	//std::cout << "m " << j+1 << ' ' << res << std::endl;
	for (int i = 0; i < km[j]; i++)
	    process_n(am[j][i]);
}
*/

/*
void process377(int x, int y){
    if (s <= k)
        return;
    if ((x < 0) || (y < 0) || (x >= n) || (y >= m))
        return;
    switch (a[x][y]){
        case '+': break;
        case '#': break;
        case '.':
            a[x][y] = '+';
            s--;
            process377(x - 1, y);
            process377(x, y - 1);
            process377(x + 1, y);
            process377(x, y + 1);
            break;
    }
}
*/

int good(int i, int l, int r){
    int i0, i1, j00, j0d, j0n, j10, j1d, j1n, j0, j1, res;
    i0 = i - 1;
    i1 = i;
    j00 = l ? slen[i0] - 1 : 0;
    j0d = l ? -1 : +1;
    j0n = l ? -1 : slen[i0];
    j10 = r ? slen[i1] - 1 : 0;
    j1d = r ? -1 : +1;
    j1n = r ? -1 : slen[i1];
    j0 = j00;
    j1 = j10;
//cout << endl << i << ' ' << l << ' ' << r << endl;
    while (true){
        if (j0 == j0n) { res = 1; break; }
        if (j1 == j1n) { res = 0; break; }
//cout << s[i0][j0] << s[i1][j1] << ' ';
        if (s[i0][j0] < s[i1][j1]) { res = 1; break; }
        if (s[i0][j0] > s[i1][j1]) { res = 0; break; }

        j0 += j0d;
        j1 += j1d;
    }
//cout << i << ' ' << l << ' ' << r << " = " << res << endl;
    return res;
}

int goodll(int i){ return good(i, 0, 0); }
int goodlr(int i){ return good(i, 0, 1); }
int goodrl(int i){ return good(i, 1, 0); }
int goodrr(int i){ return good(i, 1, 1); }

__int64 min2(__int64 a, __int64 b){
    if (a == -1) return b;
    if (b == -1) return a;
    return std::min(a, b);
}

__int64 nok(__int64 a, __int64 b){
    return a * b / gcd(a, b);
}

__int64 task235a(__int64 a, __int64 b, __int64 c){
    __int64 d, e;
    d = nok(a, b);
    e = nok(d, c);
    return e;
}

__int64 is_prime(__int64 n){
    __int64 res = 1;
    __int64 i = sqrt(n) + 1;
    for (i; (i > 1) && res; i--){
        if (!(n % i))
            res = 0;
    }
    return res;
}

int main(){
    //std::unordered_map<int, std::unordered_map<int, int>> z;
    //std::string s;
    //vector<__int64> a(n);
    //randomized_quick_sort(a, 0, n - 1);
    //a.push_back(1);
    __int64 K = 1e9 + 7;

    __int64 a[500005], sum, a1, a2, a3, a4, a5;

    cin >> n;
    sum = 0;
    for (i = 0; i < n; ++i){
    	cin >> tmp;
    	a[i] = tmp;
    	sum += tmp;
    	res += tmp >= 0 ? tmp : -tmp;
    }
    if (n < 3)
        res = 0;
    else if (sum % 3)
        res = 0;
    else if (!res)
        res = (n - 1) * (n - 2) / 2;
    else if (!sum){
        a1 = 0;
        tmp = 0;
        for (i = 0; i < n; i++){
            tmp += a[i];
            if (!tmp)
                a1++;
        }
        n = a1;
        res = (n - 1) * (n - 2) / 2;
    }
    else{
        res = 0;
        sum /= 3;
        a1 = 0;
        i = 0;
        tmp = a[i];
        while ((tmp != sum) && (i + 1 < n)){
            tmp += a[++i];
        if (tmp == sum){
            a2 = 0;
            a3 = 0;
            i++;
            tmp = a[i];
            while ((tmp != sum) && (i + 1 < n)){
                if (!tmp)
                    a2++;
                tmp += a[++i];
            }
            if (tmp == sum){
                a4 = 0;
                a5 = 0;
                i++;
                tmp = a[i];
                while ((tmp != sum) && (i + 1 < n)){
                    if (!tmp)
                        a4++;
                    tmp += a[++i];
                }
                a2++;
                a4++;
                res = a2 * a4;
            }
        }
    }

    cout << res;
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    cin>>n;
    //if(n<3) {cout<<0<<'\n';return 0;}
    long int NUM[n];
    cin>>NUM[0];
    int temp;
    for(int i=1;i<n;i++)
        {cin>>temp;NUM[i]=temp+NUM[i-1];}
    long int ANS=0;

    // if( 2*NUM[I]==NUM[n-1]-NUM[I])   ->   3*NUM[I] == NUM[n-1]

    int I=-1;
    for(int i=0;i<n-1;i++)
        if(3*NUM[i]==NUM[n-1])
            {I=i;break;}
    if(I!=-1)
        {
            for(int i=I+1;i<n-1;i++)
                if(2*NUM[I]==NUM[i])    ANS++;
        }

    for(I=0;I<n;I++)
        if(NUM[I]!=0)   break;
    if(I==n)
        {
            ANS=0;
            for(int i=1;i<n-1;i++)
                ANS+=i;
        }

    cout<<ANS;
    cout<<'\n';
    return 0;
}


#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
    int n;
    scanf("%d", &n);
    int input[500100];
    long long int sum = 0;
    for(int i=0; i<n; i++)
    {
        scanf("%d", input + i);
        sum += input[i];
    }

    if(sum % 3 != 0)
        printf("0\n");
    else
    {
        long long int buf = 0;
        long long int part = sum / 3;
        int pos[500100];
        for(int i=0; i<n; i++)
        {
            if(i == 0)
                pos[0] = 0;
            else
                pos[i] = pos[i-1];
            buf += input[i];
            if(buf == part)
                pos[i]++;
        }

        buf = 0;
        long long int ans = 0;
        for(int i=n-1; i>=2; i--)
        {
            buf += input[i];
            if(buf == part)
            {
                ans += pos[i-2];
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
using namespace std;

int I=1,J=2;

int main()
{
    int n;
    cin>>n;
    if(n<3) {cout<<0<<'\n';return 0;}
    int NUM[n];
    for(int i=0;i<n;i++)    cin>>NUM[i];

    long int F=0,S=0,T=0;
    F = NUM[0];
    S = NUM[1];
    for(int i=2;i<n;i++)    T+=NUM[i];

    int ANS=0;

    for(J=2;J<n;J++)
        {

            long int f=F,s=S,t=T;
            for(I=1;I<J;I++)
                {
                    if(f==s&&s==t)  ANS++;

                    f+=NUM[I];
                    s-=NUM[I];
                }
            S+=NUM[J];
            T-=NUM[J];

        }
    cout<<ANS;
    cout<<'\n';
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    cin>>n;
    long int NUM[n];
    cin>>NUM[0];
    int temp;
    for(int i=1;i<n;i++)
        {cin>>temp;NUM[i]=temp+NUM[i-1];}
    long long int ANS=0;

    // if( 2*NUM[I]==NUM[n-1]-NUM[I])   ->   3*NUM[I] == NUM[n-1]

    int I=-1;
    for(int i=0;i<n-1;i++)
        if(3*NUM[i]==NUM[n-1])
            {I=i;break;}

    long int mark_1[n]={0},mark_2[n]={0},mark_3[n]={0};
    if(NUM[I]!=0)
    {//if(NUM[0]==NUM[I])  mark_1[0]++;
    for(int i=1;i<n;i++)
        {
            mark_1[i] = mark_1[i-1];
            if(NUM[i-1]==NUM[I])  mark_1[i]++;
        }
    for(int i=0;i<n;i++)
        if(NUM[i]==2*NUM[I])    mark_2[i]=1;

    //if(NUM[n-1]==3*NUM[I])  mark_3[n-1]++;
    for(int i=n-2;i>=0;i--)
        {
            mark_3[i] = mark_3[i+1];
            if(NUM[i+1]==3*NUM[I])  mark_3[i]++;
        }
    for(int i=1;i<n-1;i++)
        {
            if(mark_2[i]==1)    ANS+=mark_1[i]*mark_3[i];
        }
    }

    else
    {
        //if(NUM[0]==NUM[I])  mark_1[0]++;
    for(int i=1;i<n;i++)
        {
            mark_1[i] = mark_1[i-1];
            if(NUM[i-1]==NUM[I])  mark_1[i]++;
        }
    for(int i=i;i<n-1;i++)
        if(NUM[i]==2*NUM[I])    mark_2[i]=1;

    for(int i=n-1;i>=0;i--)
        {
            mark_3[i] = 1;
        }
    for(int i=1;i<n-1;i++)
        {
            if(mark_2[i]==1)    ANS+=mark_1[i]*mark_3[i];
        }
    }
    /*for(int i=0;i<n;i++)    cout<<NUM[i]<<' ';
    cout<<'\n';
    for(int i=0;i<n;i++)    cout<<mark_1[i]<<' ';
    cout<<'\n';
    for(int i=0;i<n;i++)    cout<<mark_2[i]<<' ';
    cout<<'\n';
    for(int i=0;i<n;i++)    cout<<mark_3[i]<<' ';
    cout<<'\n';
    */
    cout<<ANS;
    cout<<'\n';
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    cin>>n;
    //if(n<3) {cout<<0<<'\n';return 0;}
    long int NUM[n];
    cin>>NUM[0];
    int temp;
    for(int i=1;i<n;i++)
        {cin>>temp;NUM[i]=temp+NUM[i-1];}
    int ANS=0;

    // if( 2*NUM[I]==NUM[n-1]-NUM[I])   ->   3*NUM[I] == NUM[n-1]

    int I=-1;
    for(int i=0;i<n-1;i++)
        if(3*NUM[i]==NUM[n-1])
            {I=i;break;}
    if(I!=-1)
        {
            for(int i=I+1;i<n-1;i++)
                if(2*NUM[I]==NUM[i])    ANS++;
        }
    for(I=0;I<n;I++)
        if(NUM[I]!=0)   break;
    if(I==n)    ANS++;

    cout<<ANS;
    cout<<'\n';
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    cin>>n;
    long int NUM[n];
    cin>>NUM[0];
    int temp;
    for(int i=1;i<n;i++)
        {cin>>temp;NUM[i]=temp+NUM[i-1];}
    long int ANS=0;

    // if( 2*NUM[I]==NUM[n-1]-NUM[I])   ->   3*NUM[I] == NUM[n-1]

    int I=-1;
    for(int i=0;i<n-1;i++)
        if(3*NUM[i]==NUM[n-1])
            {I=i;break;}

    int mark_1[n]={0},mark_2[n]={0},mark_3[n]={0};
    if(NUM[I]!=0)
    {if(NUM[0]==NUM[I])  mark_1[0]++;
    for(int i=1;i<n;i++)
        {
            mark_1[i] = mark_1[i-1];
            if(NUM[i]==NUM[I])  mark_1[i]++;
        }
    for(int i=0;i<n;i++)
        if(NUM[i]==2*NUM[I])    mark_2[i]=1;

    if(NUM[n-1]==3*NUM[I])  mark_3[n-1]++;
    for(int i=n-2;i>=0;i--)
        {
            mark_3[i] = mark_3[i+1];
            if(NUM[i]==3*NUM[I])  mark_3[i]++;
        }
    for(int i=1;i<n-1;i++)
        {
            if(mark_2[i]==1)    ANS+=mark_1[i]*mark_3[i];
        }
    }
    else
    {
        if(NUM[0]==NUM[I])  mark_1[0]++;
    for(int i=1;i<n;i++)
        {
            mark_1[i] = mark_1[i-1];
            if(NUM[i]==NUM[I])  mark_1[i]++;
        }
    for(int i=i;i<n-1;i++)
        if(NUM[i]==2*NUM[I])    mark_2[i]=1;

    for(int i=n-1;i>=0;i--)
        {
            mark_3[i] = 1;
        }
    for(int i=1;i<n-1;i++)
        {
            if(mark_2[i]==1)    ANS+=mark_1[i]*mark_3[i];
        }
    }
    /*for(int i=0;i<n;i++)    cout<<NUM[i]<<' ';
    cout<<'\n';
    for(int i=0;i<n;i++)    cout<<mark_1[i]<<' ';
    cout<<'\n';
    for(int i=0;i<n;i++)    cout<<mark_2[i]<<' ';
    cout<<'\n';
    for(int i=0;i<n;i++)    cout<<mark_3[i]<<' ';
    cout<<'\n';
    */
    cout<<ANS;
    cout<<'\n';
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    cin>>n;
    long int NUM[n];
    cin>>NUM[0];
    int temp;
    for(int i=1;i<n;i++)
        {cin>>temp;NUM[i]=temp+NUM[i-1];}
    long int ANS=0;

    // if( 2*NUM[I]==NUM[n-1]-NUM[I])   ->   3*NUM[I] == NUM[n-1]

    int I=-1;
    for(int i=0;i<n-1;i++)
        if(3*NUM[i]==NUM[n-1])
            {I=i;break;}

    int mark_1[n]={0},mark_2[n]={0},mark_3[n]={0};
    if(NUM[I]!=0)
    {if(NUM[0]==NUM[I])  mark_1[0]++;
    for(int i=1;i<n;i++)
        {
            mark_1[i] = mark_1[i-1];
            if(NUM[i]==NUM[I])  mark_1[i]++;
        }
    for(int i=0;i<n;i++)
        if(NUM[i]==2*NUM[I])    mark_2[i]=1;

    if(NUM[n-1]==3*NUM[I])  mark_3[n-1]++;
    for(int i=n-2;i>=0;i--)
        {
            mark_3[i] = mark_3[i+1];
            if(NUM[i]==3*NUM[I])  mark_3[i]++;
        }
    for(int i=1;i<n-1;i++)
        {
            if(mark_2[i]==1)    ANS+=mark_1[i]*mark_3[i];
        }
    }
    else
    {
        if(NUM[0]==NUM[I])  mark_1[0]++;
    for(int i=1;i<n;i++)
        {
            mark_1[i] = mark_1[i-1];
            if(NUM[i]==NUM[I])  mark_1[i]++;
        }
    for(int i=i;i<n-1;i++)
        if(NUM[i]==2*NUM[I])    mark_2[i]=1;

    for(int i=n-1;i>=0;i--)
        {
            mark_3[i] = 1;
        }
    for(int i=1;i<n-1;i++)
        {
            if(mark_2[i]==1)    ANS+=mark_1[i]*mark_3[i];
        }
    }
    for(int i=0;i<n;i++)    cout<<NUM[i]<<' ';
    cout<<'\n';
    for(int i=0;i<n;i++)    cout<<mark_1[i]<<' ';
    cout<<'\n';
    for(int i=0;i<n;i++)    cout<<mark_2[i]<<' ';
    cout<<'\n';
    for(int i=0;i<n;i++)    cout<<mark_3[i]<<' ';
    cout<<'\n';

    cout<<ANS;
    cout<<'\n';
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    cin>>n;
    //if(n<3) {cout<<0<<'\n';return 0;}
    long int NUM[n];
    cin>>NUM[0];
    int temp;
    for(int i=1;i<n;i++)
        {cin>>temp;NUM[i]=temp+NUM[i-1];}
    long int ANS=0;

    // if( 2*NUM[I]==NUM[n-1]-NUM[I])   ->   3*NUM[I] == NUM[n-1]

    int I=-1;
    for(int i=0;i<n-1;i++)
        if(3*NUM[i]==NUM[n-1])
            {I=i;break;}
    if(I!=-1)
        {
            for(int i=I+1;i<n-1;i++)
                if(2*NUM[I]==NUM[i])    ANS++;
        }

    for(I=0;I<n;I++)
        if(NUM[I]!=0)   break;
    if(I==n)
        {
            ANS=0;
            for(int i=1;i<n-1;i++)
                ANS+=i;
        }

    cout<<ANS;
    cout<<'\n';
    ;return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    cin>>n;
    //if(n<3) {cout<<0<<'\n';return 0;}
    long int NUM[n];
    cin>>NUM[0];
    int temp;
    for(int i=1;i<n;i++)
        {cin>>temp;NUM[i]=temp+NUM[i-1];}
    int ANS=0;

    // if( 2*NUM[I]==NUM[n-1]-NUM[I])   ->   3*NUM[I] == NUM[n-1]

    int I=-1;
    for(int i=0;i<n-1;i++)
        if(3*NUM[i]==NUM[n-1])
            {I=i;break;}
    if(I!=-1)
        {
            for(int i=I+1;i<n-1;i++)
                if(2*NUM[I]==NUM[i])    ANS++;
        }

    for(I=0;I<n;I++)
        if(NUM[I]!=0)   break;
    if(I==n)
        {
            ANS=0;
            for(int i=1;i<n-1;i++)
                ANS+=i;
        }

    cout<<ANS;
    cout<<'\n';
    return 0;
}


#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
    int n;
    scanf("%d", &n);
    int input[500100];
    long long int sum = 0;
    for(int i=0; i<n; i++)
    {
        scanf("%d", input + i);
        sum += input[i];
    }

    if(sum % 3 != 0)
        printf("0\n");
    else
    {
        long long int buf = 0;
        long long int part = sum / 3;
        int pos[500100];
        for(int i=0; i<n; i++)
        {
            if(i == 0)
                pos[0] = 0;
            else
                pos[i] = pos[i-1];
            buf += input[i];
            if(buf == part)
                pos[i]++;
        }

        buf = 0;
        long long int ans = 0;
        for(int i=n-1; i>=2; i--)
        {
            buf += input[i];
            if(buf == part)
            {
                ans += pos[i-2];
            }
        }
        cout << ans << endl;
    }
    return 0;;
}


#include <iostream>
#include <fstream>
using namespace std;
ifstream in("poarta.in");
ofstream out("poarta.out");
long long n,v[500005],nr;
int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
        cin>>v[i],v[i]+=v[i-1];
    if(v[n]%3==0){
        long long pre=0,sum=v[n]/3;
        for(int i=1;i<=n;++i){
            if(v[i]==2*sum && i!=1 && i!=n)
                nr+=pre;
            if(v[i]==sum && i<n-1)
                ++pre;
        }
    }
    cout<<nr;
    return 0;
}


#include <iostream>
#include <fstream>
using namespace std;
ifstream in("poarta.in");
ofstream out("poarta.out");
int n,v[100001];
long long alt[100001],nr;
int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
        cin>>v[i],alt[i]=alt[i-1]+v[i];
    if(alt[n]%3==0){
        long long pre=0,sum=alt[n]/3;
        for(int i=1;i<=n;++i){
            if(alt[i]==2*sum && i!=1 && i!=n)
                nr+=pre;
            if(alt[i]==sum && i!=n-1 && i!=n)
                ++pre;
        }
    }
    cout<<nr;
    return 0;
}


#include <iostream>
//#include <fstream>
using namespace std;
//ifstream in("poarta.in");
//ofstream out("poarta.out");
long long n,v[500005],nr;
int main()
{
    cin>>n;
    cin>>v[0];
    for(int i=1;i<n;++i)
        cin>>v[i],v[i]+=v[i-1];
    if(v[n-1]%3==0){
        long long pre=0,sum=v[n-1]/3;
        for(int i=0;i<n;++i){
            if(v[i]==2*sum && i!=0 && i!=n-1)
                nr+=pre;
            if(v[i]==sum && i<n-2)
                ++pre;
        }
    }
    cout<<nr;
    return 0;
}


#include <iostream>
//#include <fstream>
using namespace std;
//ifstream in("poarta.in");
//ofstream out("poarta.out");
int n,v[100001],c/*ount*/[100001];
long long s,nr,tmp;
int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
        cin>>v[i],s+=v[i];
    if(s%3==0){
        for(int i=n;i>=1;--i){
            tmp+=v[i];
            if(tmp==s/3)
                ++c[i];
        }
        for(int i=n-1;i>=1;--i)
            c[i]+=c[i+1];
        tmp=0;
        for(int i=1;i+2<=n;++i){
            tmp+=v[i];
            if(tmp==s/3)
                nr+=c[i+2];
        }
    }
    cout<<nr;
    return 0;
}


 #include <iostream>
//#include <fstream>
using namespace std;
//ifstream in("poarta.in");
//ofstream out("poarta.out");
long long n,v[100001],nr;
int main()
{
    cin>>n;
    cin>>v[0];
    for(int i=1;i<n;++i)
        cin>>v[i],v[i]+=v[i-1];
    if(v[n-1]%3==0){
        long long pre=0,sum=v[n-1]/3;
        for(int i=0;i<n;++i){
            if(v[i]==2*sum && i!=0 && i!=n-1)
                nr+=pre;
            if(v[i]==sum && i<n-2)
                ++pre;
        }
    }
    cout<<nr;
    return 0;
}


#include <iostream>
//#include <fstream>
using namespace std;
/*ifstream in("poarta.in");
ofstream out("poarta.out");*/
int n,v[100001],poz[100001];
long long s,nr;
int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
        cin>>v[i],s+=v[i];
    if(s%3==0){
        long long buf=v[1];
        if(buf==s/3)
            poz[1]++;
        for(int i=2;i<=n;++i){
            poz[i]=poz[i-1];
            buf+=v[i];
            if(buf==s/3)
                poz[i]++;
        }
        buf=0;
        for(int i=n;i>=3;--i){
            buf+=v[i];
            if(buf==s/3)
                nr+=poz[i-2];
        }
    }
    cout<<nr;
    return 0;
}


#include <iostream>
#include <fstream>
using namespace std;
ifstream in("poarta.in");
ofstream out("poarta.out");
long long n,v[100001],nr;
int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
        cin>>v[i],v[i]+=v[i-1];
    if(v[n]%3==0){
        long long pre=0,sum=v[n]/3;
        for(int i=1;i<=n;++i){
            if(v[i]==2*sum && i!=1 && i!=n)
                nr+=pre;
            if(v[i]==sum && i!=n-1 && i!=n)
                ++pre;
        }
    }
    cout<<nr;
    return 0;
}


#include <iostream>
//#include <fstream>
using namespace std;
//ifstream in("poarta.in");
//ofstream out("poarta.out");
long long n,v[100001],nr;
int main()
{
    cin>>n;
    in>>v[0];
    for(int i=1;i<n;++i)
        cin>>v[i],v[i]+=v[i-1];
    if(v[n-1]%3==0){
        long long pre=0,sum=v[n-1]/3;
        for(int i=0;i<n;++i){
            if(v[i]==2*sum && i!=0 && i!=n-1)
                nr+=pre;
            if(v[i]==sum && i<n-2)
                ++pre;
        }
    }
    cout<<nr;
    return 0;
}