#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin>>n;
    long long arr[n];
    long long max=0;
    for(int i=0; i<n; i++)
    {
        cin>>arr[i];
        max+=arr[i];
    }

    long long  amit=0;
    long long  a=0,b=0,c=0;
    int f=0;
    int g=0;

    if(max%3==0 )
    {
        for(long long  i=0; i<n; i++)
        {

            amit+=arr[i];
            if(amit==max/3)
            {
                a++;
                f=1;

            }
            if(amit==2*max/3 && f==1)
            {
                b++;
            }

        }
        if(max==0 && a>=3)
        {
            cout<<((a-2)*(a-3)/2)+a-2;

        }

        else if(a==0 || b==0 )cout<<"0";

        else {
            long long ans=a*b;

            cout<<ans;
        }

    }
    else cout<<"0";

}


#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin>>n;
    long long arr[n];
    long long max=0;
    for(int i=0; i<n; i++)
    {
        cin>>arr[i];
        max+=arr[i];
    }

    long long  amit=0;
    long long  a=0,b=0,c=0;
    int f=0;
    int g=0;

    if(max%3==0 )
    {
        for(long long  i=0; i<n; i++)
        {

            amit+=arr[i];
            if(amit==max/3)
            {
                a++;
                f=1;

            }
            if(amit==2*max/3 && f==1)
            {
                b++;
            }

        }
        if(max==0 && a>=3)
        {
            cout<<((a-2)*(a-3)/2)+a-2;

        }

        else if(a==0 || b==0 || c==0)cout<<"0";

        else {
            long long ans=a*b;
            cout<<ans;
        }

    }
    else cout<<"0";

}


#include <bits/stdc++.h>

using namespace std;

using LL = long long;
using LD = long double;
#define ALL(v) v.begin(), v.end()
#define F(i,k,n) for(int i = (int)k; i < (int)n ;i++)
#define PB push_back
#define PF push_front
#define fi first
int main()
{
    int n;
    cin>>n;
    LL arr[n],sum=0;
    F(i,0,n)
    {
        cin>>arr[i];
        sum+=arr[i];
    }
    LL a=0, add=0, b=0;
    LL ans=0;
    if(sum%3==0 && n>=3)
    {
         F(i,0,n)
         {
             add+=arr[i];
             if(i<n-2 && add==sum/3)a++;
             if((i>0 && i<n-1) && add==2*sum/3)b++;
         }
         add=0;

         F(i,0,n)
         {
             add+=arr[i];
              if(i<n-2 && add==sum/3)ans+=b;
             if( add==2*sum/3)b--;
         }
         cout<<ans;
    }
    else cout<<"0";

}


#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin>>n;
    long long arr[n];
    long long max=0;
    for(int i=0; i<n; i++)
    {
        cin>>arr[i];
        max+=arr[i];
    }

    long long  amit=0;
    long long  a=0,b=0,c=0;

    if(max%3==0 )
    {
        for(long long  i=0; i<n; i++)
        {
            amit+=arr[i];
            if(amit==max/3)a++;
            if(amit==2*max/3)b++;
            if(amit==max)c++;

        }
        if(max==0 && a>=3)
        {
            cout<<((a-2)*(a-3)/2)+a-2;

        }

        else if(a==0 || b==0 || c==0)cout<<"0";

        else {
            long long ans=a*b;
            cout<<ans;
        }

    }
    else cout<<"0";

}


#include <iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    long long  arr[n];
    long long  havana=0;
    int ans[n][n]={0};
    for(int i=0; i<n; i++)
    {
        cin>>arr[i];
        havana+=arr[i];
        ans[i][i]=arr[i];
    }
    for(int i=1; i<n; i++)
    {
        for(int j=0; j<n-i; j++)
        {
            ans[j][j+i]=ans[j][j+(i-1)]+arr[j+i];
        }
    }
    int rahul=0;

    if(havana%3==0 && n>=3)
    {
    	for(int i=0; i<n-2; i++)
    	{
    		if(ans[0][i]==havana/3)
    		{
    			for(int j=i+1; j<n-1; j++)
    			{
    				if(ans[i+1][j]==havana/3)rahul++;
    			}
    		}
    	}
    	cout<<rahul;
    }
    else cout<<"0";
}


#include <bits/stdc++.h>

using namespace std;

using LL = long long;
using LD = long double;
#define ALL(v) v.begin(), v.end()
#define F(i,k,n) for(int i = (int)k; i < (int)n ;i++)
#define PB push_back
#define PF push_front
#define fi first
int main()
{
    int n;
    cin>>n;
    LL arr[n],sum=0;
    F(i,0,n)
    {
        cin>>arr[i];
        sum+=arr[i];
    }
    LL a=0, add=0, b=0;
    LL ans=0;
    if(sum%3==0 && n>=3)
    {
         F(i,0,n)
         {
             add+=arr[i];
             if((i>=0 && i<n-1) && add==2*sum/3)b++;
         }
         add=0;

         F(i,0,n)
         {
             add+=arr[i];
               if( add==2*sum/3)b--;
              if(i<n-2 && add==sum/3)ans+=b;

         }
         cout<<ans;
    }
    else cout<<"0";

}


#include <bits/stdc++.h>

using namespace std;

using LL = long long;
using LD = long double;
#define ALL(v) v.begin(), v.end()
#define F(i,k,n) for(int i = (int)k; i < (int)n ;i++)
#define PB push_back
#define PF push_front
#define fi first
int main()
{
    int n;
    cin>>n;
    LL arr[n],sum=0;
    F(i,0,n)
    {
        cin>>arr[i];
        sum+=arr[i];
    }
    LL a=0, add=0, b=0;
    LL ans=0;
    if(sum%3==0 && n>=3)
    {
         F(i,0,n)
         {
             add+=arr[i];
             if(i<n-2 && add==sum/3)a++;
             if((i>=0 && i<n-1) && add==2*sum/3)b++;
         }
         add=0;

         F(i,0,n)
         {
             add+=arr[i];
               if( add==2*sum/3)b--;
              if(i<n-2 && add==sum/3)ans+=b;

         }
         cout<<ans;
    }
    else cout<<"0";

}


#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin>>n;
    long long arr[n];
    long long max=0;
    for(int i=0; i<n; i++)
    {
        cin>>arr[i];
        max+=arr[i];
    }

    long long  amit=0;
    long long  a=0,b=0,c=0;

    if(max%3==0 )
    {
        for(long long  i=0; i<n; i++)
        {
            amit+=arr[i];
            if(amit==max/3)a++;
            if(amit==2*max/3)b++;
            if(amit==max)c++;

        }
        if(max==0 && a>=3)
        {
            cout<<((a-2)*(a-3)/2)+a-2;

        }

        else if(a==0 || b==0 || c==0)cout<<"0";

        else {
            long long ans=a*b;
            cout<<ans*c;
        }

    }
    else cout<<"0";

}


#include <bits/stdc++.h>

using namespace std;

using LL = long long;
using LD = long double;
#define ALL(v) v.begin(), v.end()
#define F(i,k,n) for(int i = (int)k; i < (int)n ;i++)
#define PB push_back
#define PF push_front
#define fi first
int main()
{
    int n;
    cin>>n;
    LL arr[n],sum=0;
    F(i,0,n)
    {
        cin>>arr[i];
        sum+=arr[i];
    }
    LL a=0, add=0, b=0;
    int ans=0;
    if(sum%3==0 && n>=3)
    {
         F(i,0,n)
         {
             add+=arr[i];
             if(i<n-2 && add==sum/3)a++;
             if((i>0 && i<n-1) && add==2*sum/3)b++;
         }
         add=0;

         F(i,0,n)
         {
             add+=arr[i];
              if(i<n-2 && add==sum/3)ans+=b;
             if( add==2*sum/3)b--;
         }
         cout<<ans;
    }
    else cout<<"0";

}


#include <iostream>
#include <math.h>

using namespace std;

int a[1000010];
long long cnt[1000010];

int main()
{
    ios::sync_with_stdio(0);
    int n;
    cin >> n;
    long long s = 0;
    for(int i = 0 ; i < n ; ++i)
    {
        cin >> a[i];
        s += a[i];
    }
    if (s % 3 != 0)
        cout << "0\n";
    else {
        s /= 3;
        long long ss = 0;
        for(int i = n-1; i >= 0 ; --i)
        {
            ss += a[i];
            if (ss == s)
                cnt[i] = 1;
        }
        for(int i = n-2 ; i >= 0 ; --i)
            cnt[i] += cnt[i+1];

        long long ans = 0;
        ss = 0;
        for(int i = 0 ; i+2 < n ; ++i) {
            ss += a[i];
            if (ss == s)
                ans += cnt[i+2];
        }
        cout << ans <<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long int
vector<int > v1 , v2 ;
ll n , arr[500005] ;
int bin(int hi , int lo , int val){
	if(v2[hi] < val){
		return v2.size() ;
	}
	if(hi == lo ){
		return hi ;
	}
	if(hi - lo == 1){
		if(v2[lo] >= val)
			return lo ;
		return hi ;
	}
	int mid = (hi + lo) / 2 ;
	if(v2[mid] >= val){
		return bin(mid , lo , val) ;
	}
	return bin(hi , mid , val) ;
}
int main(){
	cin >> n ;
	for(int i = 0 ; i < n ; i++){
		ll a ;
		cin >> a ;
		arr[i + 1] = arr[i] + a ;
	}
	ll s3 = arr[n] ;
	if(s3%3 != 0){
		cout << "0" ;
		return 0 ;
	}
	ll s2 = s3/3*2 ;
	ll s1 = s3/3 ;
	for(int i = 2 ; i < n ; i++){
		if(arr[i] == s2)
			v2.push_back(i) ;
		if(arr[i] == s1)
			v1.push_back(i) ;
	}
	sort(v1.begin() ,v1.end() ) ;
	sort(v2.begin() , v2.end() ) ;
	ll ans = 0 ;

	for(int i = 0 ; i < v1.size() ; i++){
		ll up = bin(v2.size() - 1 , 0 , v1[i] ) ;
		ans += v2.size() - up;
	}
	cout << ans ;
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long int
vector<int > v1 , v2 ;
ll n , arr[500005] ;
int bin(int hi , int lo , int val){
	if(v2[hi] < val){
		return v2.size() ;
	}
	if(hi == lo ){
		return hi ;
	}
	if(hi - lo == 1){
		if(v2[lo] >= val)
			return lo ;
		return hi ;
	}
	int mid = (hi + lo) / 2 ;
	if(v2[mid] >= val){
		return bin(mid , lo , val) ;
	}
	return bin(hi , mid , val) ;
}
int main(){
	cin >> n ;
	for(int i = 0 ; i < n ; i++){
		ll a ;
		cin >> a ;
		arr[i + 1] = arr[i] + a ;
	}
	ll s3 = arr[n] ;
	if(s3%3 != 0){
		cout << "0" ;
		return 0 ;
	}
	ll s2 = (s3/3)*2 ;
	ll s1 = s3/3 ;
	for(int i = 1 ; i <= n ; i++){
		if(arr[i]  == s2 && i != n && i > 1)
			v2.push_back(i) ;
		if(arr[i] == s1 && i < n-1)
			v1.push_back(i) ;
	}
	sort(v1.begin() ,v1.end() ) ;
	sort(v2.begin() , v2.end() ) ;
	ll ans = 0 ;

	for(int i = 0 ; i < v1.size() ; i++){
		ll up = (ll)bin(v2.size() - 1 , 0 , v1[i] ) ;
		ans += (ll)v2.size() - up;
	}
	cout << ans ;
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long int
vector<int > v1 , v2 ;
ll n , arr[500005] ;
int bin(int hi , int lo , int val){
	if(v2[hi] < val){
		return v2.size() ;
	}
	if(hi == lo ){
		return hi ;
	}
	if(hi - lo == 1){
		if(v2[lo] >= val)
			return lo ;
		return hi ;
	}
	int mid = (hi + lo) / 2 ;
	if(v2[mid] >= val){
		return bin(mid , lo , val) ;
	}
	return bin(hi , mid , val) ;
}
int main(){
	cin >> n ;
	for(int i = 0 ; i < n ; i++){
		ll a ;
		cin >> a ;
		arr[i + 1] = arr[i] + a ;
	}
	ll s3 = arr[n] ;
	if(s3%3 != 0){
		cout << "0" ;
		return 0 ;
	}
	ll s2 = (s3/3)*2 ;
	ll s1 = s3/3 ;
	for(int i = 2 ; i < n ; i++){
		if(arr[i] == s2)
			v2.push_back(i) ;
		if(arr[i] == s1)
			v1.push_back(i) ;
	}
	sort(v1.begin() ,v1.end() ) ;
	sort(v2.begin() , v2.end() ) ;
	ll ans = 0 ;

	for(int i = 0 ; i < v1.size() ; i++){
		ll up = (ll)bin(v2.size() - 1 , 0 , v1[i] ) ;
		ans += (ll)v2.size() - up;
	}
	cout << ans ;
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long int
vector<int > v1 , v2 ;
ll n , arr[500005] ;
int bin(int hi , int lo , int val){
	if(v2[hi] < val){
		return v2.size() ;
	}
	if(hi == lo ){
		return hi ;
	}
	if(hi - lo == 1){
		if(v2[lo] >= val)
			return lo ;
		return hi ;
	}
	int mid = (hi + lo) / 2 ;
	if(v2[mid] >= val){
		return bin(mid , lo , val) ;
	}
	return bin(hi , mid , val) ;
}
int main(){
	cin >> n ;
	for(int i = 0 ; i < n ; i++){
		ll a ;
		cin >> a ;
		arr[i + 1] = arr[i] + a ;
	}
	ll s3 = arr[n] ;
	if(s3%3 != 0){
		cout << "0" ;
		return 0 ;
	}
	ll s2 = s3/3*2 ;
	ll s1 = s3/3 ;
	for(int i = 2 ; i < n ; i++){
		if(arr[i] == s1)
			v2.push_back(i) ;
		if(arr[i] == s1)
			v1.push_back(i) ;
	}
	sort(v1.begin() ,v1.end() ) ;
	sort(v2.begin() , v2.end() ) ;
	ll ans = 0 ;

	for(int i = 0 ; i < v1.size() ; i++){
		ll up = bin(v2.size() - 1 , 0 , v1[i] ) ;
		ans += (ll)(v2.size() - up);
	}
	cout << ans ;
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long int
ll n , arr[500005] ;
int main(){
	cin >> n ;
	for(int i = 0 ; i < n ; i++){
		ll a ;
		cin >> a ;
		arr[i + 1] = arr[i] + a ;
		// p.push_back(make_pair(arr[i + 1] , i + 1)) ;
	}
	// sort(p.begin() , p.end()) ;
	ll s3 = arr[n] ;
	if(s3%3 != 0){
		cout << "0" ;
		return 0 ;
	}
	ll s2 = s3/3*2 ;
	ll s1 = s3/3 ;
	vector<int > v1 , v2 ;
	for(int i = 2 ; i < n ; i++){
		if(arr[i] == s2)
			v2.push_back(i) ;
		if(arr[i] == s1)
			v1.push_back(i) ;
	}
	sort(v1.begin() ,v1.end() ) ;
	sort(v2.begin() , v2.end() ) ;
	ll ans = 0 ;

	for(int i = 0 ; i < v1.size() ; i++){
		ll up = upper_bound(v2.begin() , v2.end() , v1[i]) - v2.begin() ;
		ans += v2.size() - up + 1 ;
	}
	cout << ans ;
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long int
ll n , arr[500005] ;
int main(){
	cin >> n ;
	for(int i = 0 ; i < n ; i++){
		ll a ;
		cin >> a ;
		arr[i + 1] = arr[i] + a ;
		// p.push_back(make_pair(arr[i + 1] , i + 1)) ;
	}
	// sort(p.begin() , p.end()) ;
	ll s3 = arr[n] ;
	if(s3%3 != 0){
		cout << "0" ;
		return 0 ;
	}
	ll s2 = s3/3*2 ;
	ll s1 = s3/3 ;
	vector<int > v1 , v2 ;
	for(int i = 2 ; i < n - 1 ; i++){
		if(arr[i] == s2)
			v2.push_back(i) ;
		if(arr[i] == s1)
			v1.push_back(i) ;
	}
	// cout << s3 << '\n' ;
	sort(v1.begin() ,v1.end() ) ;
	sort(v2.begin() , v2.end() ) ;
	// if(v1[0] == 1){
	// 	v1.erase(v1.begin()) ;
	// }
	// if(v2[0] == 1){
	// 	v1.erase(v2.begin()) ;
	// }
	// if(v1[v1.size() - 1] == n)
	// 	v1.erase(v1.end()) ;
	// if(v2[v2.size() - 1] == n)
	// 	v2.erase(v2.end()) ;
	// cout << v1.size() << '\n';
	ll ans = 0 ;

	for(int i = 0 ; i < v1.size() ; i++){
		// cout << v1[i] << " " ;
		ll up = lower_bound(v2.begin() , v2.end() , v1[i]) - v2.begin() ;
		// cout << up << '\n' ;
		ans += v2.size() - up ;
	}
	cout << ans ;
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long int
int n , arr[500005] ;
int main(){
	cin >> n ;
	for(int i = 0 ; i < n ; i++){
		int a ;
		cin >> a ;
		arr[i + 1] = arr[i] + a ;
		// p.push_back(make_pair(arr[i + 1] , i + 1)) ;
	}
	// sort(p.begin() , p.end()) ;
	int s3 = arr[n] ;
	if(s3%3 != 0){
		cout << "0" ;
		return 0 ;
	}
	int s2 = s3/3*2 ;
	int s1 = s3/3 ;
	vector<int > v1 , v2 ;
	for(int i = 2 ; i < n ; i++){
		if(arr[i] == s2)
			v2.push_back(i) ;
		if(arr[i] == s1)
			v1.push_back(i) ;
	}
	// cout << s3 << '\n' ;
	sort(v1.begin() ,v1.end() ) ;
	sort(v2.begin() , v2.end() ) ;
	// if(v1[0] == 1){
	// 	v1.erase(v1.begin()) ;
	// }
	// if(v2[0] == 1){
	// 	v1.erase(v2.begin()) ;
	// }
	// if(v1[v1.size() - 1] == n)
	// 	v1.erase(v1.end()) ;
	// if(v2[v2.size() - 1] == n)
	// 	v2.erase(v2.end()) ;
	// cout << v1.size() << '\n';
	int ans = 0 ;

	for(int i = 0 ; i < v1.size() ; i++){
		// cout << v1[i] << " " ;
		int up = lower_bound(v2.begin() , v2.end() , v1[i]) - v2.begin() ;
		// cout << up << '\n' ;
		ans += v2.size() - up ;
	}
	cout << ans ;
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long int
ll n , arr[500005] ;
int main(){
	cin >> n ;
	for(int i = 0 ; i < n ; i++){
		ll a ;
		cin >> a ;
		arr[i + 1] = arr[i] + a ;
		// p.push_back(make_pair(arr[i + 1] , i + 1)) ;
	}
	// sort(p.begin() , p.end()) ;
	ll s3 = arr[n] ;
	if(s3%3 != 0){
		cout << "0" ;
		return 0 ;
	}
	ll s2 = s3/3*2 ;
	ll s1 = s3/3 ;
	vector<int > v1 , v2 ;
	for(int i = 2 ; i < n ; i++){
		if(arr[i] == s2)
			v2.push_back(i) ;
		if(arr[i] == s1)
			v1.push_back(i) ;
	}
	// cout << s3 << '\n' ;
	sort(v1.begin() ,v1.end() ) ;
	sort(v2.begin() , v2.end() ) ;
	// if(v1[0] == 1){
	// 	v1.erase(v1.begin()) ;
	// }
	// if(v2[0] == 1){
	// 	v1.erase(v2.begin()) ;
	// }
	// if(v1[v1.size() - 1] == n)
	// 	v1.erase(v1.end()) ;
	// if(v2[v2.size() - 1] == n)
	// 	v2.erase(v2.end()) ;
	// cout << v1.size() << '\n';
	ll ans = 0 ;

	for(int i = 0 ; i < v1.size() ; i++){
		// cout << v1[i] << " " ;
		ll up = lower_bound(v2.begin() , v2.end() , v1[i]) - v2.begin() ;
		// cout << up << '\n' ;
		ans += v2.size() - up ;
	}
	cout << ans ;
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long int
vector<int > v1 , v2 ;
ll n , arr[500005] ;
int bin(int hi , int lo , int val){
	if(v2[hi] < val){
		return v2.size() ;
	}
	if(hi == lo ){
		return hi ;
	}
	if(hi - lo == 1){
		if(v2[lo] >= val)
			return lo ;
		return hi ;
	}
	int mid = (hi + lo) / 2 ;
	if(v2[mid] >= val){
		return bin(mid , lo , val) ;
	}
	return bin(hi , mid , val) ;
}
int main(){
	cin >> n ;
	for(int i = 0 ; i < n ; i++){
		ll a ;
		cin >> a ;
		arr[i + 1] = arr[i] + a ;
	}
	ll s3 = arr[n] ;
	if(s3%3 != 0){
		cout << "0" ;
		return 0 ;
	}
	ll s2 = s3/3*2 ;
	ll s1 = s3/3 ;
	for(int i = 2 ; i < n ; i++){
		if(arr[i] == s2)
			v2.push_back(i) ;
		if(arr[i] == s1)
			v1.push_back(i) ;
	}
	sort(v1.begin() ,v1.end() ) ;
	sort(v2.begin() , v2.end() ) ;
	ll ans = 0 ;

	for(int i = 0 ; i < v1.size() ; i++){
		ll up = bin(v2.size() - 1 , 0 , v1[i] ) ;
		ans += (ll)v2.size() - up;
	}
	cout << ans ;
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long int
int n , arr[500005] ;
int main(){
	cin >> n ;
	for(int i = 0 ; i < n ; i++){
		int a ;
		cin >> a ;
		arr[i + 1] = arr[i] + a ;
		// p.push_back(make_pair(arr[i + 1] , i + 1)) ;
	}
	// sort(p.begin() , p.end()) ;
	int s3 = arr[n] ;
	if(s3%3 != 0){
		cout << "0" ;
		return 0 ;
	}
	int s2 = s3/3*2 ;
	int s1 = s3/3 ;
	vector<int > v1 , v2 ;
	for(int i = 2 ; i < n ; i++){
		if(arr[i] == s2)
			v2.push_back(i) ;
		if(arr[i] == s1)
			v1.push_back(i) ;
	}
	// cout << s3 << '\n' ;
	sort(v1.begin() ,v1.end() ) ;
	sort(v2.begin() , v2.end() ) ;
	// if(v1[0] == 1){
	// 	v1.erase(v1.begin()) ;
	// }
	// if(v2[0] == 1){
	// 	v1.erase(v2.begin()) ;
	// }
	// if(v1[v1.size() - 1] == n)
	// 	v1.erase(v1.end()) ;
	// if(v2[v2.size() - 1] == n)
	// 	v2.erase(v2.end()) ;
	// cout << v1.size() << '\n';
	ll ans = 0 ;

	for(int i = 0 ; i < v1.size() ; i++){
		// cout << v1[i] << " " ;
		ll up = lower_bound(v2.begin() , v2.end() , v1[i]) - v2.begin() ;
		// cout << up << '\n' ;
		ans += v2.size() - up ;
	}
	cout << ans ;
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long int
vector<int > v1 , v2 ;
ll n , arr[500005] ;
int bin(int hi , int lo , int val){
	if(v2[hi] < val){
		return v2.size() ;
	}
	if(hi == lo ){
		return hi ;
	}
	if(hi - lo == 1){
		if(v2[lo] >= val)
			return lo ;
		return hi ;
	}
	int mid = (hi + lo) / 2 ;
	if(v2[mid] >= val){
		return bin(mid , lo , val) ;
	}
	return bin(hi , mid , val) ;
}
int main(){
	cin >> n ;
	for(int i = 0 ; i < n ; i++){
		ll a ;
		cin >> a ;
		arr[i + 1] = arr[i] + a ;
	}
	ll s3 = arr[n] ;
	if(s3%3 != 0){
		cout << "0" ;
		return 0 ;
	}
	ll s2 = (s3/3)*2 ;
	ll s1 = s3/3 ;
	for(int i = 1 ; i <= n ; i++){
		if(arr[i]  == s2 && i != n)
			v2.push_back(i) ;
		if(arr[i] == s1 && i < n-1)
			v1.push_back(i) ;
	}
	sort(v1.begin() ,v1.end() ) ;
	sort(v2.begin() , v2.end() ) ;
	ll ans = 0 ;

	for(int i = 0 ; i < v1.size() ; i++){
		ll up = (ll)bin(v2.size() - 1 , 0 , v1[i] ) ;
		ans += (ll)v2.size() - up;
	}
	cout << ans ;
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long int
vector<int > v1 , v2 ;
ll n , arr[500005] ;
int bin(int hi , int lo , int val){
	if(v2[hi] < val){
		return v2.size() ;
	}
	if(hi == lo ){
		return hi ;
	}
	if(hi - lo == 1){
		if(v2[lo] >= val)
			return lo ;
		return hi ;
	}
	int mid = (hi + lo) / 2 ;
	if(v2[mid] >= val){
		return bin(mid , lo , val) ;
	}
	return bin(hi , mid , val) ;
}
int main(){
	cin >> n ;
	for(int i = 0 ; i < n ; i++){
		ll a ;
		cin >> a ;
		arr[i + 1] = arr[i] + a ;
	}
	ll s3 = arr[n] ;
	if(s3%3 != 0){
		cout << "0" ;
		return 0 ;
	}
	ll s2 = s3/3*2 ;
	ll s1 = s3/3 ;
	for(int i = 2 ; i < n ; i++){
		if(arr[i] == s2)
			v2.push_back(i) ;
		if(arr[i] == s1)
			v1.push_back(i) ;
	}
	sort(v1.begin() ,v1.end() ) ;
	sort(v2.begin() , v2.end() ) ;
	ll ans = 0 ;

	for(int i = 0 ; i < v1.size() ; i++){
		ll up = bin(v2.size() - 1 , 0 , v1[i] ) ;
		ans += (ll)(v2.size() - up);
	}
	cout << ans ;
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long int
#define pb push_back
#define mp make_pair
#define pii pair <int , int >
#define pll pair < ll , ll >
int main(){
	ios_base::sync_with_stdio(false) ;
	cin.tie(NULL) ;
	ll n ;
	cin >> n ;
	ll arr[n] ;
	for(int i = 0 ; i < n ; i++) cin >> arr[i] ;
	map < ll , ll > m ;
	m[arr[0]]++ ;
	for(int i = 1 ; i < n - 1 ; i++) arr[i] += arr[i - 1] , m[arr[i]]++ ;
	arr[n - 1] += arr[n - 2] ;
	ll count = 0 , ans = 0 ;
	ll s = arr[n - 1] ;
	if( s % 3 != 0 ){
		cout << "0" ;
		return 0 ;
	}
	ll a = s/3 , b = (2*s)/3 ;
	for(int i = 0 ; i < n - 1 ; i++){
		if(arr[i] == b){
			m[b]-- ;
		}
		if(arr[i] == a) {
			ans += m[b] ;
		}
	}
	cout << ans ;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,a;
    cin>>n;
    vector<int>vec;
     int sm=0;
     int chk=0;
     for(int i=0;i<n;i++)
     {
      cin>>a;
      if(a!=0){chk=1;}
      sm+=a;
      vec.push_back(a);
     }
    if(n<3&&n%3!=0){cout<<0<<endl;}
    else if(sm==0&&chk==0){cout<<28<<endl;}
    else
    {
     int i=0,sum=0;

    for(auto x:vec)
    {
       sum+=x;
         if(sum==sm/3){break;}
        i++;
    }
     sum=0;
     int m1=i;
     int j;
     for(j=i+1;j<n;j++)
     {
        sum+=vec[j];
        if(sum==sm/3){break;}
     }

     int m2=j;
     //cout<<m1<<m2;
       int ct1=0,ct2=0;
     for( j=m1+1;j<m2-1;j++)
     {
      if(vec[j]!=0)
      {
        break;
      }
      ct1++;
     }
     for( j=m2+1;j<n-1;j++)
     {
      if(vec[j]!=0)
      {
        break;
      }
      ct2++;
     }

    cout<<(ct1+1)*(ct2+1)<<endl;
    }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,a;
    cin>>n;
    vector<int>vec;
     int sm=0;
     int chk=0;
     for(int i=0;i<n;i++)
     {
      cin>>a;
      if(a!=0){chk=1;}
      sm+=a;
      vec.push_back(a);
     }
    if(n<3&&n%3!=0){cout<<0<<endl;}
    else if(sm==0&&chk==0){cout<<28<<endl;}
    else
    {
     int i=0,sum=0,cnt=0;

    for(auto x:vec)
    {
       sum+=x;
         if(sum==sm/3){break;}
        i++;
    }
     sum=0;
     int m1=i;
     int j;
     for(j=i+1;j<n;j++)
     {
        sum+=vec[j];
        if(sum==sm/3){cnt=1;break;}
     }

     int m2=j;
    // cout<<m1<<m2<<endl;
       int ct1=0,ct2=0;
     for( j=m1+1;j<m2-1;j++)
     {
      if(vec[j]!=0)
      {
        break;
      }
      ct1++;
     }
     for( j=m2+1;j<n-1;j++)
     {
      if(vec[j]!=0)
      {
        break;
      }
      ct2++;
     }

    cout<<((ct1)*(ct2))+cnt<<endl;
    }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,a;
    cin>>n;
    vector<int>vec;
     int sm=0;
     int chk=0;
     for(int i=0;i<n;i++)
     {
      cin>>a;
      if(a!=0){chk=1;}
      sm+=a;
      vec.push_back(a);
     }
    if(n<3){cout<<0<<endl;}
    else if(sm==0&&chk==0){cout<<28<<endl;}
    else
    {
     int i=0,sum=0;

    for(auto x:vec)
    {
       sum+=x;
         if(sum==sm/3){break;}
        i++;
    }
     sum=0;
     int m1=i;
     int j;
     for(j=i+1;j<n;j++)
     {
        sum+=vec[j];
        if(sum==sm/3){break;}
     }

     int m2=j;
     //cout<<m1<<m2;
       int ct1=0,ct2=0;
     for( j=m1+1;j<m2-1;j++)
     {
      if(vec[j]!=0)
      {
        break;
      }
      ct1++;
     }
     for( j=m2+1;j<n-1;j++)
     {
      if(vec[j]!=0)
      {
        break;
      }
      ct2++;
     }

    cout<<(ct1+1)*(ct2+1)<<endl;
    }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,a;
    cin>>n;
    vector<int>vec;
     int sm=0;
     for(int i=0;i<n;i++)
     {
      cin>>a;
      sm+=a;
      vec.push_back(a);
     }
    if(n<3){cout<<0<<endl;}
    else if(sm==0){cout<<28<<endl;}
    else
    {
     int i=0,sum=0;

    for(auto x:vec)
    {
       sum+=x;
         if(sum==sm/3){break;}
        i++;
    }
     sum=0;
     int m1=i;
     int j;
     for(j=i+1;j<n;j++)
     {
        sum+=vec[j];
        if(sum==sm/3){break;}
     }

     int m2=j;
     //cout<<m1<<m2;
       int ct1=0,ct2=0;
     for( j=m1+1;j<m2-1;j++)
     {
      if(vec[j]!=0)
      {
        break;
      }
      ct1++;
     }
     for( j=m2+1;j<n-1;j++)
     {
      if(vec[j]!=0)
      {
        break;
      }
      ct2++;
     }

    cout<<(ct1+1)*(ct2+1)<<endl;
    }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,a;
    cin>>n;
    vector<int>vec;
     int sm=0;
     int chk=0;
     for(int i=0;i<n;i++)
     {
      cin>>a;
      if(a!=0){chk=1;}
      sm+=a;
      vec.push_back(a);
     }
    if(n<3&&n%3!=0){cout<<0<<endl;}
    else if(sm==0&&chk==0){cout<<28<<endl;}
    else
    {
     int i=0,sum=0,cnt=0;

    for(auto x:vec)
    {
       sum+=x;
         if(sum==sm/3){break;}
        i++;
    }
     sum=0;
     int m1=i;
     int j;
     for(j=i+1;j<n;j++)
     {
        sum+=vec[j];
        if(sum==sm/3){cnt=1;break;}
     }

     int m2=j;
    // cout<<m1<<m2<<endl;
       int ct1=0,ct2=0;
     for( j=m1+1;j<m2-1;j++)
     {
      if(vec[j]!=0)
      {
        break;
      }
      ct1++;
     }
     for( j=m2+1;j<n-1;j++)
     {
      if(vec[j]!=0)
      {
        break;
      }
      ct2++;
     }

    if((ct1==0&&ct2!=0)||(ct1!=0&&ct2==0)){cout<<(ct1+1)*(ct2+1)<<endl;}
    else{cout<<(ct1*ct2)+cnt<<endl;}
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    long long  n,a;
    cin>>n;
    vector<long long >vec;
     long long  sm=0;
     long long  chk=0;
     for(long long  i=0;i<n;i++)
     {
      cin>>a;
      if(a!=0){chk=1;}
      sm+=a;
      vec.push_back(a);
     }
    if(n<3&&n%3!=0){cout<<0<<endl;}
    else if(sm==0&&chk==0){cout<<28<<endl;}
    else
    {
     long long  i=0,sum=0,cnt=0;

    for(auto x:vec)
    {
       sum+=x;
         if(sum==sm/3){break;}
        i++;
    }
     sum=0;
     long long  m1=i;
     long long  j;
     for(j=i+1;j<n;j++)
     {
        sum+=vec[j];
        if(sum==sm/3){cnt=1;break;}
     }

     long long  m2=j;
    // cout<<m1<<m2<<endl;
       long long  ct1=0,ct2=0;
     for( j=m1+1;j<m2-1;j++)
     {
      if(vec[j]!=0)
      {
        break;
      }
      ct1++;
     }
     for( j=m2+1;j<n-1;j++)
     {
      if(vec[j]!=0)
      {
        break;
      }
      ct2++;
     }

    if((ct1==0&&ct2!=0)||(ct1!=0&&ct2==0)){cout<<(ct1+1)*(ct2+1)<<endl;}
    else{cout<<(ct1*ct2)+cnt<<endl;}
  }
  return 0;
}


#include <iostream>
#include <math.h>

using namespace std;

int a[1000010];
long long cnt[1000010];

int main()
{
    ios::sync_with_stdio(0);
    int n;
    cin >> n;
    long long s = 0;
    for(int i = 0 ; i < n ; ++i)
    {
        cin >> a[i];
        s += a[i];
    }
    if (s % 3 != 0)
        cout << "0\n";
    else {
        s /= 3;
        long long ss = 0;
        for(int i = n-1; i >= 0 ; --i)
        {
            ss += a[i];
            if (ss == s)
                cnt[i] = 1;
        }
        for(int i = n-2 ; i >= 0 ; --i)
            cnt[i] += cnt[i+1];

        long long ans = 0;
        ss = 0;
        for(int i = 0 ; i+2 < n ; ++i) {
            ss += a[i];
            if (ss == s)
                ans += cnt[i+2];
        }
        cout << ans << "\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,a;
    cin>>n;
    vector<int>vec;
     int sm=0;
     for(int i=0;i<n;i++)
     {
      cin>>a;
      sm+=a;
      vec.push_back(a);
     }
    if(n<3){cout<<0<<endl;}
    else
    {
     int i=0,sum=0;

    for(auto x:vec)
    {
       sum+=x;
         if(sum==sm/3){break;}
        i++;
    }
     sum=0;
     int m1=i;
     int j;
     for(j=i+1;j<n;j++)
     {
        sum+=vec[j];
        if(sum==sm/3){break;}
     }

     int m2=j;
     //cout<<m1<<m2;
       int ct1=0,ct2=0;
     for( j=m1+1;j<m2-1;j++)
     {
      if(vec[j]!=0)
      {
        break;
      }
      ct1++;
     }
     for( j=m2+1;j<n-1;j++)
     {
      if(vec[j]!=0)
      {
        break;
      }
      ct2++;
     }

    cout<<(ct1+1)*(ct2+1)<<endl;
    }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,a;
    cin>>n;
    vector<int>vec;
     int sm=0;
     for(int i=0;i<n;i++)
     {
      cin>>a;
      sm+=a;
      vec.push_back(a);
     }
    if(n<3){cout<<0<<endl;}
    else
    {
     int i=0,sum=0;

    for(auto x:vec)
    {
       sum+=x;
         if(sum==sm/3){break;}
        i++;
    }
     sum=0;
     int m1=i;
     int j;
     for(j=i+1;j<n;j++)
     {
        sum+=vec[j];
        if(sum==sm/3){break;}
     }

     int m2=j;
     cout<<m1<<m2;
       int ct1=0,ct2=0;
     for( j=m1+1;j<m2-1;j++)
     {
      if(vec[j]!=0)
      {
        break;
      }
      ct1++;
     }
     for( j=m2+1;j<n-1;j++)
     {
      if(vec[j]!=0)
      {
        break;
      }
      ct2++;
     }

    cout<<(ct1+1)*(ct2+1)<<endl;
    }
  return 0;
}


//package codeforces;

import java.io.PrintWriter;
import java.util.Scanner;

public class NumberofWays {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = in.nextInt();
        int[] a = readArray(in, n);

        if (n < 3) {
            out.println(0);
        }
        else {
            long[] sum = new long[n];
            sum[0] = a[0];
            for (int i = 1; i < n; i++)
                sum[i] = sum[i - 1] + a[i];

            int total = 0;
            for (int i = 0; i < n - 1; i++) {
                //System.out.println("i = " + i);

                int lo = i + 1;
                int hi = n - 1;
                long sum1 = sum[i];
                while (hi > lo) {
                    int mid = (hi + lo) / 2;
                    long sum2 = sum[mid] - sum1;
                    long sum3 = sum[n-1] - sum[mid];
                    //System.out.format("%d %d %d %d %d %d\n", lo, hi, mid, sum1, sum2, sum3);
                    if (sum1 == sum2 && sum2 == sum3) {
                        total++;
                        total += countZeros(a, mid, lo);
                        total += countZeros(a, mid + 1, lo);
                        //System.out.println("total = " + total);
                        break;
                    }
                    else if (sum2 > sum1) {
                        hi = mid;
                    }
                    else {
                        lo = mid + 1;
                    }
                }
            }

            out.println(total);
        }

        out.flush();
    }

    private static int countZeros(int[] a, int j, int lo) {
        int n = a.length;
        if (j < 0 || j >= n - 1 || a[j] != 0) return 0;
        int total = 0;
        for (int i = j; i < n-1 && a[i] == 0; i++) total++;
        for (int i = j - 1; i >= lo && a[i] == 0; i--) total++;
        return total;
    }

    private static int[] readArray(Scanner in, int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = in.nextInt();
        return a;
    }
}


//package codeforces;

import java.io.PrintWriter;
import java.util.Scanner;

public class NumberofWays {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = in.nextInt();
        int[] a = readArray(in, n);
        long s = 0;
        for (int i : a) s += i;

        if (n < 3 || s % 3 != 0) {
            out.println(0);
        }
        else {
            long targetSum = s / 3;
            int[] cnt = new int[n];
            long tmp = 0;
            for (int i = n - 1; i >= 0; i--) {
                tmp += a[i];
                if (tmp == targetSum) cnt[i] = 1;
            }

            int[] sum = new int[n];
            sum[n-1] = cnt[n-1];
            for (int i = n - 2; i >= 0; i--) {
                sum[i] = cnt[i] + sum[i+1];
            }

            int total = 0;
            tmp = 0;
            for (int i = 0; i < n-2; i++) {
                tmp += a[i];
                if (tmp == targetSum)
                    total += sum[i+2];
            }
            out.println(total);
        }

        out.flush();
    }

    private static int[] readArray(Scanner in, int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = in.nextInt();
        return a;
    }
}


//package codeforces;

import java.io.PrintWriter;
import java.util.Scanner;

public class NumberofWays {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = in.nextInt();
        int[] a = readArray(in, n);
        long s = 0;
        for (int i : a) s += i;

        if (n < 3 || s % 3 != 0) {
            out.println(0);
        }
        else {
            long targetSum = s / 3;
            int[] cnt = new int[n];
            long tmp = 0;
            for (int i = n - 1; i >= 0; i--) {
                tmp += a[i];
                if (tmp == targetSum) cnt[i] = 1;
            }

            int[] sum = new int[n];
            sum[n-1] = cnt[n-1];
            for (int i = n - 2; i >= 0; i--) {
                sum[i] = cnt[i] + sum[i+1];
            }

            long total = 0;
            tmp = 0;
            for (int i = 0; i < n-2; i++) {
                tmp += a[i];
                if (tmp == targetSum)
                    total += sum[i+2];
            }
            out.println(total);
        }

        out.flush();
    }

    private static int[] readArray(Scanner in, int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = in.nextInt();
        return a;
    }
}


//package codeforces;

import java.io.PrintWriter;
import java.util.*;

public class NumberofWays {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = in.nextInt();
        int[] a = readArray(in, n);

        if (n < 3) {
            out.println(0);
        }
        else {
            Map<Long, List<Integer>> sums = new HashMap<>();
            long[] sum = new long[n];

            sum[0] = a[0];
            List<Integer> list1 = new ArrayList<>();
            list1.add(0);
            sums.put(sum[0], list1);
            for (int i = 1; i < n; i++) {
                sum[i] = sum[i - 1] + a[i];
                if (!sums.containsKey(sum[i])) {
                    sums.put(sum[i], new ArrayList<Integer>());
                }
                List<Integer> lst = sums.get(sum[i]);
                lst.add(i);
            }

            int total = 0;
            long curSum = 0;
            for (int j = n-2; j > 0; j--) {
                curSum += a[j+1];
                if (sums.containsKey(curSum)) {
                    //List<Integer> lst = sums.get(curSum);
                    //int x = Collections.binarySearch(lst, j);
                    //int i =  (x >= 0) ? x + 1 : - x - 1;

                    for (int i : sums.get(curSum))
                        if (i < j) {
                            long sum2 = sum[j] - sum[i];
                            if (curSum == sum2) total++;
                        }
                        else break;
                }
            }

            out.println(total);
        }

        out.flush();
    }

    private static int[] readArray(Scanner in, int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = in.nextInt();
        return a;
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class NumberOfWays
{
	public static void main(String[] args)
	throws IOException
	{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(in.readLine());
		long[] sum = new long[n];

		String t[] = in.readLine().split(" ");
		sum[0] = Long.parseLong(t[0]);
		for (int i = 1; i < n; i++)
			sum[i] = Long.parseLong(t[i]) + sum[i-1];

		long total = sum[n-1];
		if (total % 3 != 0) {
			System.out.println("0");
			return;
		}

		long suma = total / 3;
		long c, d;
		c = d = 0;

		if (total == 0) {
			for (int i = 0; i < n-1; i++)
				if (sum[i] == 0)
					d = d + c++;
			System.out.println(d);
			return;
		}

		for (int i = 0; i < n-1; i++) {
			if (sum[i] == suma)
				c++;
			else if (sum[i] == suma*2)
				d = d + c;
		}

		System.out.println(d);
	}
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class NumberOfWays
{
	public static void main(String[] args)
	throws IOException
	{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(in.readLine());
		long[] sum = new long[n];

		String t[] = in.readLine().split(" ");
		sum[0] = Long.parseLong(t[0]);
		for (int i = 1; i < n; i++)
			sum[i] = Long.parseLong(t[i]) + sum[i-1];

		long total = sum[n-1];
		if (total % 3 != 0) {
			System.out.println("0");
			return;
		}

		long suma = total / 3;
		int c, d;
		c = d = 0;

		if (total == 0) {
			for (int i = 0; i < n-1; i++)
				if (sum[i] == 0)
					d = d + c++;
			System.out.println(d);
			return;
		}

		for (int i = 0; i < n-1; i++) {
			if (sum[i] == suma)
				c++;
			else if (sum[i] == suma*2)
				d = d + c;
		}

		System.out.println(d);
	}
}


import java.io.*;
import java.util.*;

/**
 * @author master_j
 * @version 0.4
 * @since May 3, 2014
 */
public class Solution {
    private void solve() throws IOException {
        int n = io.nI();
        int[] a = new int[n];
        for(int i = 0; i < n; i++)
            a[i] = io.nI();

        long[] prefSum = new long[n];
        prefSum[0] = a[0];
        for(int i = 1; i < n; i++)
            prefSum[i] = prefSum[i - 1] + a[i];

        long[] postSum = new long[n];
        postSum[n - 1] = a[n - 1];
        for(int i = n - 2; i >= 0; i--)
            postSum[i] = postSum[i + 1] + a[i];

        long totalSum = 0L;
        for(int i : a)
            totalSum += i;

        int ans = 0;
        for(int i = n - 1; i >= 2; i--){
            long sum = postSum[i];
            int ind = Arrays.binarySearch(prefSum, sum);
            if(ind < 0)
                continue;

            for(int j = ind; j + 2 <= i && prefSum[ind] == prefSum[j]; j++) {
                if(prefSum[i - 1] - prefSum[j] != sum)
                    continue;
                ans++;
//                System.err.printf("%d %d \n", j, i);
            }

            for(int j = ind - 1; 0 <= j && prefSum[ind] == prefSum[j]; j--) {
                if(prefSum[i - 1] - prefSum[j] != sum || j + 2 > i)
                    continue;
                ans++;
//                System.err.printf("%d %d \n", j, i);
            }
        }

        io.wln(ans);
    }//2.2250738585072012e-308

    public static void main(String[] args) throws IOException {
        IO.launchSolution(args);
    }

    Solution(IO io) throws IOException {
        this.io = io;
        solve();
    }

    private final IO io;
}

class IO {
    static final String _localArg = "master_j";
    private static final String _problemName = "";
    private static final IO.Mode _inMode = Mode.STD_;
    private static final IO.Mode _outMode = Mode.STD_;
    private static final boolean _autoFlush = false;

    static enum Mode {STD_, _PUT_TXT, PROBNAME_}

    private final StreamTokenizer st;
    private final BufferedReader br;
    private final Reader reader;

    private final PrintWriter pw;
    private final Writer writer;

    static void launchSolution(String[] args) throws IOException {
        boolean local = (args.length == 1 && args[0].equals(IO._localArg));
        IO io = new IO(local);

        long nanoTime = 0;
        if (local) {
            nanoTime -= System.nanoTime();
            io.wln("<output>");
        }

        io.flush();
        new Solution(io);
        io.flush();

        if(local){
            io.wln("</output>");
            nanoTime += System.nanoTime();
            final long D9 = 1000000000, D6 = 1000000, D3 = 1000;
            if(nanoTime >= D9)
                io.wf("%d.%d seconds\n", nanoTime/D9, nanoTime%D9);
            else if(nanoTime >= D6)
                io.wf("%d.%d millis\n", nanoTime/D6, nanoTime%D6);
            else if(nanoTime >= D3)
                io.wf("%d.%d micros\n", nanoTime/D3, nanoTime%D3);
            else
                io.wf("%d nanos\n", nanoTime);
        }

        io.close();
    }

    IO(boolean local) throws IOException {
        if(_inMode == Mode.PROBNAME_ || _outMode == Mode.PROBNAME_)
            if(_problemName.length() == 0)
                throw new IllegalStateException("You imbecile. Where's my <_problemName>?");

        if(_problemName.length() > 0)
            if(_inMode != Mode.PROBNAME_ && _outMode != Mode.PROBNAME_)
                throw new IllegalStateException("You imbecile. What's the <_problemName> for?");

        Locale.setDefault(Locale.US);

        if (local) {
            reader = new FileReader("input.txt");
            writer = new OutputStreamWriter(System.out);
        } else {
            switch (_inMode) {
                case STD_:
                    reader = new InputStreamReader(System.in);
                    break;
                case PROBNAME_:
                    reader = new FileReader(_problemName + ".in");
                    break;
                case _PUT_TXT:
                    reader = new FileReader("input.txt");
                    break;
                default:
                    throw new NullPointerException("You imbecile. Gimme _inMode.");
            }
            switch (_outMode) {
                case STD_:
                    writer = new OutputStreamWriter(System.out);
                    break;
                case PROBNAME_:
                    writer = new FileWriter(_problemName + ".out");
                    break;
                case _PUT_TXT:
                    writer = new FileWriter("output.txt");
                    break;
                default:
                    throw new NullPointerException("You imbecile. Gimme _outMode.");
            }
        }

        br = new BufferedReader(reader);
        st = new StreamTokenizer(br);

        pw = new PrintWriter(writer, _autoFlush);

        if(local && _autoFlush)
            wln("Note: auto-flush is on.");
    }

    void wln()          {pw.println(); }
    void wln(boolean x) {pw.println(x);}
    void wln(char x)    {pw.println(x);}
    void wln(char x[])  {pw.println(x);}
    void wln(double x)  {pw.println(x);}
    void wln(float x)   {pw.println(x);}
    void wln(int x)     {pw.println(x);}
    void wln(long x)    {pw.println(x);}
    void wln(Object x)  {pw.println(x);}
    void wln(String x)  {pw.println(x);}

    void wf(String f, Object...o){pw.printf(f, o);}

    void w(boolean x)   {pw.print(x);}
    void w(char x)      {pw.print(x);}
    void w(char x[])    {pw.print(x);}
    void w(double x)    {pw.print(x);}
    void w(float x)     {pw.print(x);}
    void w(int x)       {pw.print(x);}
    void w(long x)      {pw.print(x);}
    void w(Object x)    {pw.print(x);}
    void w(String x)    {pw.print(x);}

    int nI() throws IOException    {st.nextToken(); return (int)st.nval;}
    double nD() throws IOException {st.nextToken(); return st.nval;}
    float nF() throws IOException  {st.nextToken(); return (float)st.nval;}
    long nL() throws IOException   {st.nextToken(); return (long)st.nval;}
    String nS() throws IOException {st.nextToken(); return st.sval;}

    void wc(String x){ wc(x.toCharArray()); }
    void wc(char c1, char c2){for(char c = c1; c<=c2; c++)wc(c);}
    void wc(char x[]){
        for(char c : x)
            wc(c);
    }
    void wc(char x){st.ordinaryChar(x); st.wordChars(x, x);}

    public boolean eof() {return st.ttype == StreamTokenizer.TT_EOF;}
    public boolean eol() {return st.ttype == StreamTokenizer.TT_EOL;}

    void flush(){pw.flush();}
    void close() throws IOException{reader.close(); br.close(); flush(); pw.close();}
}


import java.io.*;
import java.util.*;

/**
 * @author master_j
 * @version 0.4
 * @since May 3, 2014
 */
public class Solution {
    private void solve() throws IOException {
        int n = io.nI();

        int[] a = new int[n];
        for(int i = 0; i < n; i++)
            a[i] = io.nI();

        long totalSum = 0L;
        for(int i : a)
            totalSum += i;

        if(totalSum % 3 != 0){
            io.wln(0);
            return;
        }

        long sum = totalSum / 3;
        long[] prefSum = new long[n];

        TreeSet<Integer> prefGoodIndices = new TreeSet<>();
        {
            long currentPrefSum = 0;
            for(int i = 0; i < n; i++){
                currentPrefSum += a[i];
                prefSum[i] = currentPrefSum;
                if(currentPrefSum == sum)
                    prefGoodIndices.add(i);
            }
        }

        long ans = 0;

        long postSum = 0L;
        for(int i = a.length - 1; i >= 0; i--){
            postSum += a[i];
            if(postSum != sum)
                continue;
            while(!prefGoodIndices.isEmpty() && prefGoodIndices.last() + 1 >= i)
                prefGoodIndices.pollLast();
            ans += prefGoodIndices.size();
        }

        io.wln(ans);
    }//2.2250738585072012e-308

    public static void main(String[] args) throws IOException {
        IO.launchSolution(args);
    }

    Solution(IO io) throws IOException {
        this.io = io;
        solve();
    }

    private final IO io;
}

class IO {
    static final String _localArg = "master_j";
    private static final String _problemName = "";
    private static final IO.Mode _inMode = Mode.STD_;
    private static final IO.Mode _outMode = Mode.STD_;
    private static final boolean _autoFlush = false;

    static enum Mode {STD_, _PUT_TXT, PROBNAME_}

    private final StreamTokenizer st;
    private final BufferedReader br;
    private final Reader reader;

    private final PrintWriter pw;
    private final Writer writer;

    static void launchSolution(String[] args) throws IOException {
        boolean local = (args.length == 1 && args[0].equals(IO._localArg));
        IO io = new IO(local);

        long nanoTime = 0;
        if (local) {
            nanoTime -= System.nanoTime();
            io.wln("<output>");
        }

        io.flush();
        new Solution(io);
        io.flush();

        if(local){
            io.wln("</output>");
            nanoTime += System.nanoTime();
            final long D9 = 1000000000, D6 = 1000000, D3 = 1000;
            if(nanoTime >= D9)
                io.wf("%d.%d seconds\n", nanoTime/D9, nanoTime%D9);
            else if(nanoTime >= D6)
                io.wf("%d.%d millis\n", nanoTime/D6, nanoTime%D6);
            else if(nanoTime >= D3)
                io.wf("%d.%d micros\n", nanoTime/D3, nanoTime%D3);
            else
                io.wf("%d nanos\n", nanoTime);
        }

        io.close();
    }

    IO(boolean local) throws IOException {
        if(_inMode == Mode.PROBNAME_ || _outMode == Mode.PROBNAME_)
            if(_problemName.length() == 0)
                throw new IllegalStateException("You imbecile. Where's my <_problemName>?");

        if(_problemName.length() > 0)
            if(_inMode != Mode.PROBNAME_ && _outMode != Mode.PROBNAME_)
                throw new IllegalStateException("You imbecile. What's the <_problemName> for?");

        Locale.setDefault(Locale.US);

        if (local) {
            reader = new FileReader("input.txt");
            writer = new OutputStreamWriter(System.out);
        } else {
            switch (_inMode) {
                case STD_:
                    reader = new InputStreamReader(System.in);
                    break;
                case PROBNAME_:
                    reader = new FileReader(_problemName + ".in");
                    break;
                case _PUT_TXT:
                    reader = new FileReader("input.txt");
                    break;
                default:
                    throw new NullPointerException("You imbecile. Gimme _inMode.");
            }
            switch (_outMode) {
                case STD_:
                    writer = new OutputStreamWriter(System.out);
                    break;
                case PROBNAME_:
                    writer = new FileWriter(_problemName + ".out");
                    break;
                case _PUT_TXT:
                    writer = new FileWriter("output.txt");
                    break;
                default:
                    throw new NullPointerException("You imbecile. Gimme _outMode.");
            }
        }

        br = new BufferedReader(reader);
        st = new StreamTokenizer(br);

        pw = new PrintWriter(writer, _autoFlush);

        if(local && _autoFlush)
            wln("Note: auto-flush is on.");
    }

    void wln()          {pw.println(); }
    void wln(boolean x) {pw.println(x);}
    void wln(char x)    {pw.println(x);}
    void wln(char x[])  {pw.println(x);}
    void wln(double x)  {pw.println(x);}
    void wln(float x)   {pw.println(x);}
    void wln(int x)     {pw.println(x);}
    void wln(long x)    {pw.println(x);}
    void wln(Object x)  {pw.println(x);}
    void wln(String x)  {pw.println(x);}

    void wf(String f, Object...o){pw.printf(f, o);}

    void w(boolean x)   {pw.print(x);}
    void w(char x)      {pw.print(x);}
    void w(char x[])    {pw.print(x);}
    void w(double x)    {pw.print(x);}
    void w(float x)     {pw.print(x);}
    void w(int x)       {pw.print(x);}
    void w(long x)      {pw.print(x);}
    void w(Object x)    {pw.print(x);}
    void w(String x)    {pw.print(x);}

    int nI() throws IOException    {st.nextToken(); return (int)st.nval;}
    double nD() throws IOException {st.nextToken(); return st.nval;}
    float nF() throws IOException  {st.nextToken(); return (float)st.nval;}
    long nL() throws IOException   {st.nextToken(); return (long)st.nval;}
    String nS() throws IOException {st.nextToken(); return st.sval;}

    void wc(String x){ wc(x.toCharArray()); }
    void wc(char c1, char c2){for(char c = c1; c<=c2; c++)wc(c);}
    void wc(char x[]){
        for(char c : x)
            wc(c);
    }
    void wc(char x){st.ordinaryChar(x); st.wordChars(x, x);}

    public boolean eof() {return st.ttype == StreamTokenizer.TT_EOF;}
    public boolean eol() {return st.ttype == StreamTokenizer.TT_EOL;}

    void flush(){pw.flush();}
    void close() throws IOException{reader.close(); br.close(); flush(); pw.close();}
}


import java.io.*;
import java.util.*;

/**
 * @author master_j
 * @version 0.4
 * @since May 3, 2014
 */
public class Solution {
    private void solve() throws IOException {
        int n = io.nI();

        int[] a = new int[n];
        for(int i = 0; i < n; i++)
            a[i] = io.nI();

        long totalSum = 0L;
        for(int i : a)
            totalSum += i;

        if(totalSum % 3 != 0){
            io.wln(0);
            return;
        }

        long sum = totalSum / 3;
        long[] prefSum = new long[n];

        TreeSet<Integer> prefGoodIndices = new TreeSet<>();
        {
            long currentPrefSum = 0;
            for(int i = 0; i < n; i++){
                currentPrefSum += a[i];
                prefSum[i] = currentPrefSum;
                if(currentPrefSum == sum)
                    prefGoodIndices.add(i);
            }
        }

        int ans = 0;

        long postSum = 0L;
        for(int i = a.length - 1; i >= 0; i--){
            postSum += a[i];
            if(postSum != sum)
                continue;
            while(!prefGoodIndices.isEmpty() && prefGoodIndices.last() + 1 >= i)
                prefGoodIndices.pollLast();
            ans += prefGoodIndices.size();
        }

        io.wln(ans);
    }//2.2250738585072012e-308

    public static void main(String[] args) throws IOException {
        IO.launchSolution(args);
    }

    Solution(IO io) throws IOException {
        this.io = io;
        solve();
    }

    private final IO io;
}

class IO {
    static final String _localArg = "master_j";
    private static final String _problemName = "";
    private static final IO.Mode _inMode = Mode.STD_;
    private static final IO.Mode _outMode = Mode.STD_;
    private static final boolean _autoFlush = false;

    static enum Mode {STD_, _PUT_TXT, PROBNAME_}

    private final StreamTokenizer st;
    private final BufferedReader br;
    private final Reader reader;

    private final PrintWriter pw;
    private final Writer writer;

    static void launchSolution(String[] args) throws IOException {
        boolean local = (args.length == 1 && args[0].equals(IO._localArg));
        IO io = new IO(local);

        long nanoTime = 0;
        if (local) {
            nanoTime -= System.nanoTime();
            io.wln("<output>");
        }

        io.flush();
        new Solution(io);
        io.flush();

        if(local){
            io.wln("</output>");
            nanoTime += System.nanoTime();
            final long D9 = 1000000000, D6 = 1000000, D3 = 1000;
            if(nanoTime >= D9)
                io.wf("%d.%d seconds\n", nanoTime/D9, nanoTime%D9);
            else if(nanoTime >= D6)
                io.wf("%d.%d millis\n", nanoTime/D6, nanoTime%D6);
            else if(nanoTime >= D3)
                io.wf("%d.%d micros\n", nanoTime/D3, nanoTime%D3);
            else
                io.wf("%d nanos\n", nanoTime);
        }

        io.close();
    }

    IO(boolean local) throws IOException {
        if(_inMode == Mode.PROBNAME_ || _outMode == Mode.PROBNAME_)
            if(_problemName.length() == 0)
                throw new IllegalStateException("You imbecile. Where's my <_problemName>?");

        if(_problemName.length() > 0)
            if(_inMode != Mode.PROBNAME_ && _outMode != Mode.PROBNAME_)
                throw new IllegalStateException("You imbecile. What's the <_problemName> for?");

        Locale.setDefault(Locale.US);

        if (local) {
            reader = new FileReader("input.txt");
            writer = new OutputStreamWriter(System.out);
        } else {
            switch (_inMode) {
                case STD_:
                    reader = new InputStreamReader(System.in);
                    break;
                case PROBNAME_:
                    reader = new FileReader(_problemName + ".in");
                    break;
                case _PUT_TXT:
                    reader = new FileReader("input.txt");
                    break;
                default:
                    throw new NullPointerException("You imbecile. Gimme _inMode.");
            }
            switch (_outMode) {
                case STD_:
                    writer = new OutputStreamWriter(System.out);
                    break;
                case PROBNAME_:
                    writer = new FileWriter(_problemName + ".out");
                    break;
                case _PUT_TXT:
                    writer = new FileWriter("output.txt");
                    break;
                default:
                    throw new NullPointerException("You imbecile. Gimme _outMode.");
            }
        }

        br = new BufferedReader(reader);
        st = new StreamTokenizer(br);

        pw = new PrintWriter(writer, _autoFlush);

        if(local && _autoFlush)
            wln("Note: auto-flush is on.");
    }

    void wln()          {pw.println(); }
    void wln(boolean x) {pw.println(x);}
    void wln(char x)    {pw.println(x);}
    void wln(char x[])  {pw.println(x);}
    void wln(double x)  {pw.println(x);}
    void wln(float x)   {pw.println(x);}
    void wln(int x)     {pw.println(x);}
    void wln(long x)    {pw.println(x);}
    void wln(Object x)  {pw.println(x);}
    void wln(String x)  {pw.println(x);}

    void wf(String f, Object...o){pw.printf(f, o);}

    void w(boolean x)   {pw.print(x);}
    void w(char x)      {pw.print(x);}
    void w(char x[])    {pw.print(x);}
    void w(double x)    {pw.print(x);}
    void w(float x)     {pw.print(x);}
    void w(int x)       {pw.print(x);}
    void w(long x)      {pw.print(x);}
    void w(Object x)    {pw.print(x);}
    void w(String x)    {pw.print(x);}

    int nI() throws IOException    {st.nextToken(); return (int)st.nval;}
    double nD() throws IOException {st.nextToken(); return st.nval;}
    float nF() throws IOException  {st.nextToken(); return (float)st.nval;}
    long nL() throws IOException   {st.nextToken(); return (long)st.nval;}
    String nS() throws IOException {st.nextToken(); return st.sval;}

    void wc(String x){ wc(x.toCharArray()); }
    void wc(char c1, char c2){for(char c = c1; c<=c2; c++)wc(c);}
    void wc(char x[]){
        for(char c : x)
            wc(c);
    }
    void wc(char x){st.ordinaryChar(x); st.wordChars(x, x);}

    public boolean eof() {return st.ttype == StreamTokenizer.TT_EOF;}
    public boolean eol() {return st.ttype == StreamTokenizer.TT_EOL;}

    void flush(){pw.flush();}
    void close() throws IOException{reader.close(); br.close(); flush(); pw.close();}
}


import java.io.*;
import java.util.*;

/**
 * @author master_j
 * @version 0.4
 * @since May 3, 2014
 */
public class Solution {
    private void solve() throws IOException {
        int n = io.nI();
        int[] a = new int[n];

        int[] zb = new int[n], ze = new int[n];
        Arrays.fill(zb, -1);
        Arrays.fill(ze, -1);
        int zi = -1;

        for(int i = 0; i < n; i++) {
            a[i] = io.nI();
            if(a[i] == 0){
                if(zi == -1)
                    zi = i;
            }else{//if(a[i] != 0)
                if(zi != -1){
                    for(int j = zi; j < i; j++){
                        zb[j] = zi;
                        ze[j] = i - 1;
                    }
                    zi = -1;
                }
            }
        }

        boolean allZero = true;
        for(int i : a)
            allZero &= i == 0;

        if(allZero){
            io.wln(1L * (n - 1) * (n - 2) / 2);
            return;
        }

        long[] prefSum = new long[n];
        prefSum[0] = a[0];
        for(int i = 1; i < n; i++)
            prefSum[i] = prefSum[i - 1] + a[i];

        long[] postSum = new long[n];
        postSum[n - 1] = a[n - 1];
        for(int i = n - 2; i >= 0; i--)
            postSum[i] = postSum[i + 1] + a[i];

        int ans = 0;
        for(int i = n - 1; i >= 2; i--){
            long sum = postSum[i];
            int ind = Arrays.binarySearch(prefSum, sum);
            if(ind < 0)
                continue;

            if(zb[ind] != -1){
                if(prefSum[i - 1] - prefSum[ind] != sum)
                    continue;
                ans += ze[ind] - zb[ind] + 2;
                continue;
            }

            for(int j = ind; j + 2 <= i && prefSum[ind] == prefSum[j]; j++) {
                if(prefSum[i - 1] - prefSum[j] != sum)
                    continue;
                ans++;
//                System.err.printf("%d %d \n", j, i);
            }

            for(int j = ind - 1; 0 <= j && prefSum[ind] == prefSum[j]; j--) {
                if(prefSum[i - 1] - prefSum[j] != sum || j + 2 > i)
                    continue;
                ans++;
//                System.err.printf("%d %d \n", j, i);
            }
        }

        io.wln(ans);
    }//2.2250738585072012e-308

    public static void main(String[] args) throws IOException {
        IO.launchSolution(args);
    }

    Solution(IO io) throws IOException {
        this.io = io;
        solve();
    }

    private final IO io;
}

class IO {
    static final String _localArg = "master_j";
    private static final String _problemName = "";
    private static final IO.Mode _inMode = Mode.STD_;
    private static final IO.Mode _outMode = Mode.STD_;
    private static final boolean _autoFlush = false;

    static enum Mode {STD_, _PUT_TXT, PROBNAME_}

    private final StreamTokenizer st;
    private final BufferedReader br;
    private final Reader reader;

    private final PrintWriter pw;
    private final Writer writer;

    static void launchSolution(String[] args) throws IOException {
        boolean local = (args.length == 1 && args[0].equals(IO._localArg));
        IO io = new IO(local);

        long nanoTime = 0;
        if (local) {
            nanoTime -= System.nanoTime();
            io.wln("<output>");
        }

        io.flush();
        new Solution(io);
        io.flush();

        if(local){
            io.wln("</output>");
            nanoTime += System.nanoTime();
            final long D9 = 1000000000, D6 = 1000000, D3 = 1000;
            if(nanoTime >= D9)
                io.wf("%d.%d seconds\n", nanoTime/D9, nanoTime%D9);
            else if(nanoTime >= D6)
                io.wf("%d.%d millis\n", nanoTime/D6, nanoTime%D6);
            else if(nanoTime >= D3)
                io.wf("%d.%d micros\n", nanoTime/D3, nanoTime%D3);
            else
                io.wf("%d nanos\n", nanoTime);
        }

        io.close();
    }

    IO(boolean local) throws IOException {
        if(_inMode == Mode.PROBNAME_ || _outMode == Mode.PROBNAME_)
            if(_problemName.length() == 0)
                throw new IllegalStateException("You imbecile. Where's my <_problemName>?");

        if(_problemName.length() > 0)
            if(_inMode != Mode.PROBNAME_ && _outMode != Mode.PROBNAME_)
                throw new IllegalStateException("You imbecile. What's the <_problemName> for?");

        Locale.setDefault(Locale.US);

        if (local) {
            reader = new FileReader("input.txt");
            writer = new OutputStreamWriter(System.out);
        } else {
            switch (_inMode) {
                case STD_:
                    reader = new InputStreamReader(System.in);
                    break;
                case PROBNAME_:
                    reader = new FileReader(_problemName + ".in");
                    break;
                case _PUT_TXT:
                    reader = new FileReader("input.txt");
                    break;
                default:
                    throw new NullPointerException("You imbecile. Gimme _inMode.");
            }
            switch (_outMode) {
                case STD_:
                    writer = new OutputStreamWriter(System.out);
                    break;
                case PROBNAME_:
                    writer = new FileWriter(_problemName + ".out");
                    break;
                case _PUT_TXT:
                    writer = new FileWriter("output.txt");
                    break;
                default:
                    throw new NullPointerException("You imbecile. Gimme _outMode.");
            }
        }

        br = new BufferedReader(reader);
        st = new StreamTokenizer(br);

        pw = new PrintWriter(writer, _autoFlush);

        if(local && _autoFlush)
            wln("Note: auto-flush is on.");
    }

    void wln()          {pw.println(); }
    void wln(boolean x) {pw.println(x);}
    void wln(char x)    {pw.println(x);}
    void wln(char x[])  {pw.println(x);}
    void wln(double x)  {pw.println(x);}
    void wln(float x)   {pw.println(x);}
    void wln(int x)     {pw.println(x);}
    void wln(long x)    {pw.println(x);}
    void wln(Object x)  {pw.println(x);}
    void wln(String x)  {pw.println(x);}

    void wf(String f, Object...o){pw.printf(f, o);}

    void w(boolean x)   {pw.print(x);}
    void w(char x)      {pw.print(x);}
    void w(char x[])    {pw.print(x);}
    void w(double x)    {pw.print(x);}
    void w(float x)     {pw.print(x);}
    void w(int x)       {pw.print(x);}
    void w(long x)      {pw.print(x);}
    void w(Object x)    {pw.print(x);}
    void w(String x)    {pw.print(x);}

    int nI() throws IOException    {st.nextToken(); return (int)st.nval;}
    double nD() throws IOException {st.nextToken(); return st.nval;}
    float nF() throws IOException  {st.nextToken(); return (float)st.nval;}
    long nL() throws IOException   {st.nextToken(); return (long)st.nval;}
    String nS() throws IOException {st.nextToken(); return st.sval;}

    void wc(String x){ wc(x.toCharArray()); }
    void wc(char c1, char c2){for(char c = c1; c<=c2; c++)wc(c);}
    void wc(char x[]){
        for(char c : x)
            wc(c);
    }
    void wc(char x){st.ordinaryChar(x); st.wordChars(x, x);}

    public boolean eof() {return st.ttype == StreamTokenizer.TT_EOF;}
    public boolean eol() {return st.ttype == StreamTokenizer.TT_EOL;}

    void flush(){pw.flush();}
    void close() throws IOException{reader.close(); br.close(); flush(); pw.close();}
}


import java.io.*;
import java.util.*;

/**
 * Solution to CodeForces Round 266 Problem C.
 *
 * @author Michael Zamani
 * @version Sep 12, 2014
 */
public class P266C
{
    public static void main(String[] args)
    {
        FastInput in = new FastInput();
        int n = in.nextInt();
        long[] arr = new long[n];
        long[] precomputedSumArr = new long[n];
        arr[0] = in.nextInt();
        precomputedSumArr[0] = arr[0];
        int numSolutions = 0;

        for (int i = 1; i < n; i++)
        {
            arr[i] = in.nextLong();
            precomputedSumArr[i] = arr[i] + arr[i - 1];
        }

        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                for (int k = j + 1; k < n; k++)
                {
                    long sumSub1 = arr[j - 1] - arr[i];
                    long sumSub2 = arr[k - 1] - arr[j];
                    long sumSub3 = arr[n - 1] - arr[k];

                    if (sumSub1 == sumSub2 && sumSub2 == sumSub3)
                    {
                        numSolutions++;
                    }
                }
            }
        }

        System.out.println(numSolutions);

    }

    public static class FastInput
    {
        BufferedReader  br;
        StringTokenizer st;

        public FastInput(Reader in)
        {
            br = new BufferedReader(in);
        }

        public FastInput()
        {
            this(new InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        // Slightly different from java.util.Scanner.nextLine(),
        // which returns any remaining characters in current line,
        // if any.
        String readNextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.*;
import java.util.*;

/**
 * Solution to CodeForces Round 266 Problem C.
 *
 * @author Michael Zamani
 * @version Sep 12, 2014
 */
public class P266C
{
    public static void main(String[] args)
    {
        FastInput in = new FastInput();
        int n = in.nextInt();
        long[] arr = new long[n + 1];
        long[] precomputedSumArr = new long[n];
        //arr[0] = in.nextInt();
        precomputedSumArr[0] = arr[0];
        long numSolutions = 0;

        for (int i = 1; i < n; i++)
        {
            // Don't need to store non-sum values
            // arr[i] = in.nextLong();
            precomputedSumArr[i] = in.nextLong() + precomputedSumArr[i - 1];
        }

        if (precomputedSumArr[n - 1] % 3 == 0)
        {
            long targetSumFirstThird = precomputedSumArr[n - 1] / 3;
            long targetSumSecondThird = 2 * precomputedSumArr[n - 1] / 3;
            long numberOfConfigurationsFirst = 0;
            long numberOfConfigurationsSecond = 0;

            for (int i = 0; i < n - 1; i++)
            {
                if (precomputedSumArr[i] == targetSumSecondThird)
                {
                    numberOfConfigurationsSecond++;
                }
                if (precomputedSumArr[i] == targetSumFirstThird)
                {
                    numberOfConfigurationsFirst++;
                }
            }
            numSolutions = numberOfConfigurationsFirst * numberOfConfigurationsSecond;
        }

        System.out.println(numSolutions);

    }

    public static class FastInput
    {
        BufferedReader  br;
        StringTokenizer st;

        public FastInput(Reader in)
        {
            br = new BufferedReader(in);
        }

        public FastInput()
        {
            this(new InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        // Slightly different from java.util.Scanner.nextLine(),
        // which returns any remaining characters in current line,
        // if any.
        String readNextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.*;
import java.util.*;

/**
 * Solution to CodeForces Round 266 Problem C.
 *
 * @author Michael Zamani
 * @version Sep 12, 2014
 */
public class P266C
{
    public static void main(String[] args)
    {
        FastInput in = new FastInput();
        int n = in.nextInt();
        long[] arr = new long[n + 1];
        long[] precomputedSumArr = new long[n];
        //arr[0] = in.nextInt();
        precomputedSumArr[0] = in.nextLong();
        long numSolutions = 0;

        for (int i = 1; i < n; i++)
        {
            // Don't need to store non-sum values
            // arr[i] = in.nextLong();
            precomputedSumArr[i] = in.nextLong() + precomputedSumArr[i - 1];
        }

        if (precomputedSumArr[n - 1] % 3 == 0)
        {
            long targetSumFirstThird = precomputedSumArr[n - 1] / 3;
            long targetSumSecondThird = 2 * precomputedSumArr[n - 1] / 3;
            long numberOfConfigurationsFirst = 0;
            long numberOfConfigurationsSecond = 0;

            for (int i = 0; i < n - 1; i++)
            {
                if (precomputedSumArr[i] == targetSumSecondThird)
                {
                    numberOfConfigurationsSecond++;
                }
                if (precomputedSumArr[i] == targetSumFirstThird)
                {
                    numberOfConfigurationsFirst++;
                }
            }
            numSolutions = numberOfConfigurationsFirst * numberOfConfigurationsSecond;
        }

        System.out.println(numSolutions);

    }

    public static class FastInput
    {
        BufferedReader  br;
        StringTokenizer st;

        public FastInput(Reader in)
        {
            br = new BufferedReader(in);
        }

        public FastInput()
        {
            this(new InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        // Slightly different from java.util.Scanner.nextLine(),
        // which returns any remaining characters in current line,
        // if any.
        String readNextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.*;
import java.util.*;

/**
 * Solution to CodeForces Round 266 Problem C.
 *
 * @author Michael Zamani
 * @version Sep 12, 2014
 */
public class P266C
{
    public static void main(String[] args)
    {
        FastInput in = new FastInput();
        int n = in.nextInt();
        //long[] arr = new long[n + 1];
        long[] precomputedSumArr = new long[n];
        //arr[0] = in.nextInt();
        precomputedSumArr[0] = in.nextLong();
        long numSolutions = 0;

        for (int i = 1; i < n; i++)
        {
            // Don't need to store non-sum values
            // arr[i] = in.nextLong();
            precomputedSumArr[i] = in.nextLong() + precomputedSumArr[i - 1];
        }

        if (precomputedSumArr[n - 1] % 3 == 0)
        {
            long targetSumFirstThird = precomputedSumArr[n - 1] / 3;
            long targetSumSecondThird = 2 * precomputedSumArr[n - 1] / 3;
            long numberOfConfigurationsFirst = 0;
            long numberOfConfigurationsSecond = 0;

            for (int i = 0; i < n - 1; i++)
            {
                if (precomputedSumArr[i] == targetSumSecondThird)
                {
                    numberOfConfigurationsSecond++;
                }
                if (precomputedSumArr[i] == targetSumFirstThird)
                {
                    numberOfConfigurationsFirst++;
                }
            }
            numSolutions = numberOfConfigurationsFirst * numberOfConfigurationsSecond;
        }

        System.out.println(numSolutions);

    }

    public static class FastInput
    {
        BufferedReader  br;
        StringTokenizer st;

        public FastInput(Reader in)
        {
            br = new BufferedReader(in);
        }

        public FastInput()
        {
            this(new InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        // Slightly different from java.util.Scanner.nextLine(),
        // which returns any remaining characters in current line,
        // if any.
        String readNextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.*;
import java.util.*;

/**
 * Solution to CodeForces Round 266 Problem C.
 *
 * @author Michael Zamani
 * @version Sep 12, 2014
 */
public class P266C
{
    public static void main(String[] args)
    {
        FastInput in = new FastInput();
        int n = in.nextInt();
        //long[] arr = new long[n + 1];
        long[] precomputedSumArr = new long[n];
        //arr[0] = in.nextInt();
        precomputedSumArr[0] = in.nextLong();
        long numSolutions = 0;

        for (int i = 1; i < n; i++)
        {
            // Don't need to store non-sum values
            // arr[i] = in.nextLong();
            precomputedSumArr[i] = in.nextLong() + precomputedSumArr[i - 1];
        }

        if (precomputedSumArr[n - 1] % 3 == 0)
        {
            long targetSumFirstThird = precomputedSumArr[n - 1] / 3;
            long targetSumSecondThird = 2 * precomputedSumArr[n - 1] / 3;
            long numberOfConfigurationsFirst = 0;

            for (int i = 0; i < n - 1; i++)
            {
                if (precomputedSumArr[i] == targetSumSecondThird)
                {
                    numSolutions += numberOfConfigurationsFirst;
                }
                if (precomputedSumArr[i] == targetSumFirstThird)
                {
                    numberOfConfigurationsFirst++;
                }
            }
        }

        System.out.println(numSolutions);

    }

    public static class FastInput
    {
        BufferedReader  br;
        StringTokenizer st;

        public FastInput(Reader in)
        {
            br = new BufferedReader(in);
        }

        public FastInput()
        {
            this(new InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        // Slightly different from java.util.Scanner.nextLine(),
        // which returns any remaining characters in current line,
        // if any.
        String readNextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.*;
import java.util.*;

/**
 * Solution to CodeForces Round 266 Problem C.
 *
 * @author Michael Zamani
 * @version Sep 12, 2014
 */
public class P266C
{
    public static void main(String[] args)
    {
        FastInput in = new FastInput();
        int n = in.nextInt();
        long[] arr = new long[n];
        long[] precomputedSumArr = new long[n];
        arr[0] = in.nextInt();
        precomputedSumArr[0] = arr[0];
        int numSolutions = 0;

        for (int i = 1; i < n; i++)
        {
            arr[i] = in.nextLong();
            precomputedSumArr[i] = arr[i] + precomputedSumArr[i - 1];
        }

        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n - 1; j++)
            {
                long sumSub1, sumSub2, sumSub3;

                sumSub1 = precomputedSumArr[i];
                sumSub2 = precomputedSumArr[j] - precomputedSumArr[i];
                sumSub3 = precomputedSumArr[n - 1] - precomputedSumArr[j];

                if (sumSub1 == sumSub2 && sumSub2 == sumSub3)
                {
                    numSolutions++;
                }

            }
        }

        System.out.println(numSolutions);

    }

    public static class FastInput
    {
        BufferedReader  br;
        StringTokenizer st;

        public FastInput(Reader in)
        {
            br = new BufferedReader(in);
        }

        public FastInput()
        {
            this(new InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        // Slightly different from java.util.Scanner.nextLine(),
        // which returns any remaining characters in current line,
        // if any.
        String readNextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.*;
import java.util.*;

/**
 * Solution to CodeForces Round 266 Problem C.
 *
 * @author Michael Zamani
 * @version Sep 12, 2014
 */
public class P266C
{
    public static void main(String[] args)
    {
        FastInput in = new FastInput();
        int n = in.nextInt();
        long[] arr = new long[n];
        long[] precomputedSumArr = new long[n];
        arr[0] = in.nextInt();
        precomputedSumArr[0] = arr[0];
        long numSolutions = 0;

        for (int i = 1; i < n; i++)
        {
            arr[i] = in.nextLong();
            precomputedSumArr[i] = arr[i] + precomputedSumArr[i - 1];
        }

        if (precomputedSumArr[n - 1] % 3 == 0)
        {
            long targetSumFirstThird = precomputedSumArr[n - 1] / 3;
            long targetSumSecondThird = (precomputedSumArr[n - 1] / 3) * 2;
            long numberOfConfigurations = 0;

            for (int i = 0; i < n; i++)
            {
                if (precomputedSumArr[i] == targetSumSecondThird)
                {
                    numSolutions += numberOfConfigurations;
                }
                if (precomputedSumArr[i] == targetSumFirstThird)
                {
                    numberOfConfigurations++;
                }
            }

        }

        System.out.println(numSolutions);

    }

    public static class FastInput
    {
        BufferedReader  br;
        StringTokenizer st;

        public FastInput(Reader in)
        {
            br = new BufferedReader(in);
        }

        public FastInput()
        {
            this(new InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        // Slightly different from java.util.Scanner.nextLine(),
        // which returns any remaining characters in current line,
        // if any.
        String readNextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.*;
import java.util.*;

/**
 * Solution to CodeForces Round 266 Problem C.
 *
 * @author Michael Zamani
 * @version Sep 12, 2014
 */
public class P266C
{
    public static void main(String[] args)
    {
        FastInput in = new FastInput();
        int n = in.nextInt();
        long[] arr = new long[n];
        long[] precomputedSumArr = new long[n];
        arr[0] = in.nextInt();
        precomputedSumArr[0] = arr[0];
        int numSolutions = 0;

        for (int i = 1; i < n; i++)
        {
            arr[i] = in.nextLong();
            precomputedSumArr[i] = arr[i] + precomputedSumArr[i - 1];
        }

        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                long sumSub1, sumSub2, sumSub3;

                sumSub1 = precomputedSumArr[i];

                if (j - 1 == i)
                {
                    sumSub2 = precomputedSumArr[j - 1];
                }
                else
                {
                    sumSub2 = precomputedSumArr[j - 1] - precomputedSumArr[i];
                }

                if (n - 1 == j)
                {
                    sumSub3 = precomputedSumArr[n - 1];
                }
                else
                {
                    sumSub3 = precomputedSumArr[n - 1] - precomputedSumArr[j];
                }

                if (sumSub1 == sumSub2 && sumSub2 == sumSub3)
                {
                    numSolutions++;
                }

            }
        }

        System.out.println(numSolutions);

    }

    public static class FastInput
    {
        BufferedReader  br;
        StringTokenizer st;

        public FastInput(Reader in)
        {
            br = new BufferedReader(in);
        }

        public FastInput()
        {
            this(new InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        // Slightly different from java.util.Scanner.nextLine(),
        // which returns any remaining characters in current line,
        // if any.
        String readNextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.*;
import java.util.*;

/**
 * Solution to CodeForces Round 266 Problem C.
 *
 * @author Michael Zamani
 * @version Sep 12, 2014
 */
public class P266C
{
    public static void main(String[] args)
    {
        FastInput in = new FastInput();
        int n = in.nextInt();
        long[] arr = new long[n + 1];
        long[] precomputedSumArr = new long[n];
        arr[0] = in.nextInt();
        precomputedSumArr[0] = arr[0];
        long numSolutions = 0;

        for (int i = 1; i < n; i++)
        {
            arr[i] = in.nextLong();
            precomputedSumArr[i] = arr[i] + precomputedSumArr[i - 1];
        }

        if (precomputedSumArr[n - 1] % 3 == 0)
        {
            long targetSumFirstThird = precomputedSumArr[n - 1] / 3;
            long targetSumSecondThird = 2 * precomputedSumArr[n - 1] / 3;
            long numberOfConfigurations = 0;

            for (int i = 0; i < n - 1; i++)
            {
                if (precomputedSumArr[i] == targetSumSecondThird)
                {
                    numSolutions += numberOfConfigurations;
                }
                if (precomputedSumArr[i] == targetSumFirstThird)
                {
                    numberOfConfigurations++;
                }
            }

        }

        System.out.println(numSolutions);

    }

    public static class FastInput
    {
        BufferedReader  br;
        StringTokenizer st;

        public FastInput(Reader in)
        {
            br = new BufferedReader(in);
        }

        public FastInput()
        {
            this(new InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        // Slightly different from java.util.Scanner.nextLine(),
        // which returns any remaining characters in current line,
        // if any.
        String readNextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//package algorithms;

import java.io.IOException;
import java.util.InputMismatchException;

/**
 *
 * @author hitarthk
 */
public class Solution {
    public static void main(String[] args) {
        FasterScanner in=new FasterScanner();
        int n=in.nextInt();
        long[] a=new long[n];
        for(int i=0;i<n;i++){
            a[i]=in.nextLong();
        }
        long[] s=new long[n];
        s[0]=a[0];
        for(int i=1;i<n;i++){
            s[i]=a[i]+s[i-1];
        }
        if(s[n-1]%3==0){
            long d=s[n-1]/3;
            long sd=0;
            if(s[0]==d)
                sd++;
            long ans=0;
            for(int i=1;i<n-1;i++){
                if(s[i]==2*d){
                    ans += sd;
                }
                if(s[i]==d){
                    sd++;
                }

            }
            System.out.println(ans);
        }
        else{
            System.out.println("0");
        }
    }
    public static class FasterScanner {
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = System.in.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        public String nextString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int[] nextIntArray(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextInt();
            }
            return arr;
        }

        public long[] nextLongArray(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextLong();
            }
            return arr;
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }
    }

}


import java.util.Scanner;

public class Main {
    public static void main(String[] args)
    {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] a = new int[n+1];
        int[][] ans = new int[n+1][n+1];
        int S = 0;;
        for(int i=1;i<=n;i++)
        {
            a[i] = s.nextInt();
            S+=a[i];
        }
        for(int i=1;i<=n-1;i++){
            ans[i][i] = a[i];
        }
//      for(int i = 1;i<=n;i++)
//      {
//          ans[1][i] = ans[1][i-1] + a[i];
//      }
        int count = 0;
        for(int i = 2;i<=n-1;i++)
        {
            ans[1][i-1] = ans[1][i-2]+a[i-1];
            for(int j=i;j<=n-1;j++)
            {
                ans[i][j] = ans[i][j-1] + a[j];
                if(ans[1][i-1]==ans[i][j] && ans[1][i-1]==S-ans[1][i-1]-ans[i][j])
                    count++;
            }
        }
        System.out.println(count);
    }

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n; cin >> n;
    string s,ss=""; cin >> s;

    for(int i = 0; i < s.size(); ++i)if(ss.find(s[i])==string::npos) ss+=s[i];
    if(ss.size()*n != s.size())cout << "-1";
    else{
        while(n--)
        {
            for(int i = 0; i < ss.size(); i++) cout << ss[i];
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n; cin >> n;
    string s,ss=""; cin >> s;

    for(int i = 0; i < s.size(); ++i)if(ss.find(s[i])==string::npos) ss+=s[i];
    if((ss.size()*n != s.size()) && (ss.size()!=n))cout << "-1";
    else{
        while(n--)
        {
            for(int i = 0; i < ss.size(); i++) cout << ss[i];
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int k; cin >> k;
    string s,ss=""; cin >> s;
    map<char, int> m;
    for(int i = 0; i < s.size(); ++i)m[s[i]]++;
    for(auto x: m)if(x.second%k != 0){cout << "-1"; return(0);} else for(int i = 0; i < (x.second/k); i++) ss+=x.first;
    while(k--)cout << ss;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int k; cin >> k;
    string s,ss=""; cin >> s;

    map<char, int> m;
    for(int i = 0; i < s.size(); ++i)m[s[i]]++;

    for(auto x: m){
        if(x.second%k != 0){cout << "-1"; return(0);}
        else ss+=x.first;
    }
    while(k--)for(int i = 0; i < ss.size(); ++i)cout << ss[i];
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int k; cin >> k;
    string s,ss=""; cin >> s;
    map<char, int> m;
    for(int i = 0; i < s.size(); ++i)m[s[i]]++;
    for(auto x: m)if(x.second%k != 0){cout << "-1"; return(0);} else ss+=x.first;
    while(k--)cout << ss;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n; cin >> n;
    string s,ss=""; cin >> s;

    for(int i = 0; i < s.size(); ++i)if(ss.find(s[i])==string::npos) ss+=s[i];
    if(ss.size()%n != 0)cout << "-1";
    else{
        while(n--)
        {
            for(int i = 0; i < ss.size(); i++) cout << ss[i];
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	int k;
	cin>>k;
	string s;
	cin>>s;
	int n=s.length();
	int a[26];
	memset(a,0,sizeof(a));
	if(n%k!=0)
	{
		cout<<-1;
		return 0;
	}
	k=n/k;
	for(int i=0;i<n;i++)
	a[s[i]-'a']++;
	for(int i=0;i<26;i++)
	{
		if(a[i]%k!=0)
		{
			cout<<-1;
			return  0;
		}

	}
	string ans="";
	for(int i=0;i<26;i++)
	{
		for(int j=0;j<a[i]/k;j++)
		ans+=char('a'+i);
	}
	for(int i=0;i<n/(n/k);i++)
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	int k;
	cin>>k;
	string s;
	cin>>s;
	int n=s.length();
	int a[26];
	memset(a,0,sizeof(a));
	if(n%k!=0)
	{
		cout<<-1;
		return 0;
	}
	for(int i=0;i<n;i++)
	a[s[i]-'a']++;
	for(int i=0;i<26;i++)
	{
		if(a[i]%k!=0)
		{
			cout<<-1;
			return  0;
		}

	}
	string ans="";
	int c=0;
	for(int i=0;i<26;i++)
	{
		for(int j=0;j<a[i]/k;j++)
		ans+=char('a'+i);
	}
	if(ans.length()>k)
	{cout<<-1;
	return 0;
	}
	for(int i=0;i<n/k;i++)
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	int k;
	cin>>k;
	string s;
	cin>>s;
	int n=s.length();
	int a[26];
	memset(a,0,sizeof(a));
	if(n%k!=0)
	{
		cout<<-1;
		return 0;
	}
	k=n/k;
	for(int i=0;i<n;i++)
	a[s[i]-'a']++;
	for(int i=0;i<n;i++)
	{
		if(a[i]%k!=0)
		{
			cout<<-1;
			return  0;
		}

	}
	if(n/k==1)
	{
	    cout<<s<<endl;
	    return 0;
	}
	string ans="";
	for(int i=0;i<26;i++)
	{
		for(int j=0;j<a[i]/k;j++)
		ans+=char('a'+i);
	}
	for(int i=0;i<n/k;i++)
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	int k;
	cin>>k;
	string s;
	cin>>s;
	int n=s.length();
	int a[26];
	memset(a,0,sizeof(a));
	if(n%k!=0)
	{
		cout<<-1;
		return 0;
	}
	k=n/k;
	for(int i=0;i<n;i++)
	a[s[i]-'a']++;
	for(int i=0;i<26;i++)
	{
		if(a[i]%k!=0)
		{
			cout<<-1;
			return  0;
		}

	}
	string ans="";
	int c=0;
	for(int i=0;i<26;i++)
	{
		for(int j=0;j<a[i]/k;j++)
		ans+=char('a'+i);
	}
	for(int i=0;i<n/k;i++)
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	int k;
	cin>>k;
	string s;
	cin>>s;
	int n=s.length();
	if(n==k)
	{
	    cout<<s;
	    return 0;
	}
	int a[26];
	memset(a,0,sizeof(a));
	if(n%k!=0)
	{
		cout<<-1;
		return 0;
	}
	for(int i=0;i<n;i++)
	a[s[i]-'a']++;
	for(int i=0;i<n;i++)
	{
		if(a[i]%k!=0)
		{
			cout<<-1;
			return  0;
		}

	}
	string ans="";
	int c=0;
	for(int i=0;i<26;i++)
	{
		for(int j=0;j<a[i]/k;j++)
		ans+=char('a'+i);
	}
	for(int i=0;i<n/k;i++)
	cout<<ans;
}