#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long  answer = 0;
long long a[600000];
long long pref[600000];
long long sufic[600000];
long long leftanswers[600000];
long long rightanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	for (int i = N-1; i >= 0; --i)
	{
		if (i < N-1)
			sufic[i] = a[i] + sufic[i+1];
		else
			sufic[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	long long  cc = 1;
	long long  maxCount = 0;
	int first = 0;
	for (int i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] == parties)
		{
			if (first == 0)
			{
				first = i;
			}
		}
	}
	cc = 1;
	for (int i = N-1; i >= 0; --i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] >= 2*parties && pref[i] % 2*parties == 0)
		{
			rightanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (int i = first; i < N-1; ++i)
		{
			if (pref[i] == parties)
			{
				for (int k = i+1; k < N; ++k)
				{
					if (rightanswers[k] > 0 && pref[k] >= 2*parties && pref[k] % 2*parties == 0)
					{
						answer += rightanswers[k];
						break;
					}
				}
			}

		}
		cout << answer;
		return 0;
	}
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 1000000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long answer = 0;
long long a[600000];
long long pref[600000];
long long leftanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	int cc = 1;
	for (int i = 2; i < N; ++i)
	{
		if ( parties == 0 /*|| pref[i] % parties == 0*/)
		{
			leftanswers[i] = cc;
			cc++;
		}
	}
	if (parties == 0)
	{
		answer = 0;
		for (int i = 2; i < N; ++i)
		{
			if ( leftanswers[i] > 0 /*|| pref[i] % parties == 0*/)
			{
				answer += leftanswers[i];

			}
		}
		cout << answer;
		return 0;
	}
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 1000000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long answer = 0;
long long a[600000];
long long pref[600000];
long long leftanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	int cc = 1;
	int maxCount = 0;
	int firstt = -1;
	for (int i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 || parties != 0 && pref[i] % parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
			if (firstt == -1)
				firstt = i;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (int i = firstt; i < N; ++i)
		{
			if ( parties == 0 && leftanswers[i] > 0 || parties != 0 && pref[i] % 2*parties == 0)
			{
				answer += (maxCount - leftanswers[i]);

			}
		}
		if (answer == 0) {
			cout << 0;
			return 0;
		}
		cout << (parties == 0 ? answer : answer-1);
		return 0;
	}
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 1000000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long  answer = 0;
long long a[600000];
long long pref[600000];
long long sufic[600000];
long long leftanswers[600000];
long long rightanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	for (int i = N-1; i >= 0; --i)
	{
		if (i < N-1)
			sufic[i] = a[i] + sufic[i+1];
		else
			sufic[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	long long  cc = 1;
	long long  maxCount = 0;
	int first = 0;
	for (int i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] == parties)
		{
			if (first == 0)
			{
				first = i;
			}
		}
	}
	cc = 1;
	for (int i = N-1; i >= 0; --i)
	{
			rightanswers[i] = cc;
			maxCount = cc;
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] >= 2*parties && pref[i] % 2*parties == 0)
		{
			cc++;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (int i = first; i < N-1; ++i)
		{
			if (pref[i] == parties)
			{
				for (int k = i+1; k < N; ++k)
				{
					if (rightanswers[k] > 0 /*&& pref[k] >= 2*parties && pref[k] % 2*parties == 0*/)
					{
						answer += rightanswers[k];
						break;
					}
				}
			}

		}
		cout << answer;
		return 0;
	}
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 1000000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long  answer = 0;
long long a[600000];
long long pref[600000];
long long sufic[600000];
long long leftanswers[600000];
long long rightanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	for (int i = N-1; i >= 0; --i)
	{
		if (i < N-1)
			sufic[i] = a[i] + sufic[i+1];
		else
			sufic[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	long long  cc = 1;
	long long  maxCount = 0;

	for (int i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
	}
	cc = 1;
	for (int i = N-1; i >= 0; --i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			rightanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (int i = 1; i < N; ++i)
		{
			if (pref[i] == parties)
			{
				for (int k = i+1; k < N; ++k)
				{
					if (rightanswers[k] > 0)
					{
						answer += rightanswers[k];
						break;
					}
				}
			}

		}
		if (answer == 0) {
			cout << 0;
			return 0;
		}
		cout << (parties == 0 ? answer : answer-1);
		return 0;
	}
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 1000000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long  answer = 0;
long long a[600000];
long long pref[600000];
long long sufic[600000];
long long leftanswers[600000];
long long rightanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	for (int i = N-1; i >= 0; --i)
	{
		if (i < N-1)
			sufic[i] = a[i] + sufic[i+1];
		else
			sufic[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	long long  cc = 1;
	long long  maxCount = 0;
	long long first = 0;
	for (long long i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] == parties)
		{
			if (first == 0)
			{
				first = i;
			}
		}
	}
	cc = 0;
	long long partNear = first;
	for (long long i = N-2; i >= 0; --i)
	{
		if (pref[i] == parties)
		{
			partNear = i;
			rightanswers[partNear] = cc;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] >= 2*parties && pref[i] % 2*parties == 0)
		{
			//rightanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (long long i = 0; i < N-1; ++i)
		{
			if (pref[i] == parties)
			{
				//for (int k = i+1; k < N; ++k)
				{
					//if (rightanswers[k] > 0 && pref[k] >= 2*parties && pref[k] % 2*parties == 0)
					{
						answer += rightanswers[i];
						//break;
					}
				}
			}

		}
		//cout << answer;
		//return 0;
	}

		answer = 0;
	for (int i = 0; i < N-2; ++i)
	{
		long long part1 = pref[i];
		if(part1 == parties)
		{
			for (long long j = i+1; j < N-1; ++j)
			{
				long long part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					long long part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 451000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long  answer = 0;
long long a[600000];
long long pref[600000];
long long sufic[600000];
long long leftanswers[600000];
long long rightanswers[600000];
int N;
long long counter = 0;

void longSolve()
{
	long long parties = pref[N-1]/3;
	answer = 0;
	for (int i = 0; i < N-2; ++i)
	{
		long long part1 = pref[i];
		if(part1 == parties)
		{
			for (long long j = i+1; j < N-1; ++j)
			{
				long long part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					long long part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 451000000)
				{
					cout << answer;
					return ;
				}
			}
		}
	}
	cout << answer;
}

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}

	long long parties = pref[N-1] / 3;
	long long  cc = 0;
	for (long long i = N-2; i >= 0; --i)
	{
		if (pref[i] == parties && pref[N-1] == parties*3)
		{
			rightanswers[i] = cc;
		}
		if ( parties == 0 && pref[i] == 0 ||
		     parties != 0 && pref[i] == 2*parties)
		{
			cc++;
		}
	}

	answer = 0;
	for (long long i = 0; i < N-1; ++i)
	{
		if (pref[i] == parties)
		{
			answer += rightanswers[i];
		}
	}

	cout << answer;

	//longSolve();
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long answer = 0;
long long a[600000];
long long pref[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	const int parties = pref[N-1]/3;
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = ( i == 0 ? a[i] : pref[i] );
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 600000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long  answer = 0;
long long a[600000];
long long pref[600000];
long long sufic[600000];
long long leftanswers[600000];
long long rightanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	for (int i = N-1; i >= 0; --i)
	{
		if (i < N-1)
			sufic[i] = a[i] + sufic[i+1];
		else
			sufic[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	long long  cc = 1;
	long long  maxCount = 0;
	int first = 0;
	for (int i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] == parties)
		{
			if (first == 0)
			{
				first = i;
			}
		}
	}
	cc = 1;
	long long partNear = 1;
	for (int i = N-2; i >= 0; --i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] >= 2*parties && pref[i] % 2*parties == 0)
		{
			//rightanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
		if (pref[i] == parties)
		{
			partNear = i;
			rightanswers[partNear] = cc-1;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (int i = first; i < N-1; ++i)
		{
			if (pref[i] == parties)
			{
				//for (int k = i+1; k < N; ++k)
				{
					//if (rightanswers[k] > 0 && pref[k] >= 2*parties && pref[k] % 2*parties == 0)
					{
						answer += rightanswers[i];
						//break;
					}
				}
			}

		}
		cout << answer;
		return 0;
	}
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 1000000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long answer = 0;
long long a[600000];
long long pref[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	const int parties = pref[N-1]/3;
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 6000000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long  answer = 0;
long long a[600000];
long long pref[600000];
long long sufic[600000];
long long leftanswers[600000];
long long rightanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	for (int i = N-1; i >= 0; --i)
	{
		if (i < N-1)
			sufic[i] = a[i] + sufic[i+1];
		else
			sufic[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	long long  cc = 1;
	long long  maxCount = 0;
	int first = 0;
	for (int i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] == parties)
		{
			if (first == 0)
			{
				first = i;
			}
		}
	}
	cc = 1;
	for (int i = N-1; i >= 0; --i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] >= 2*parties && pref[i] % 2*parties == 0)
		{
			rightanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (int i = first; i < N-1; ++i)
		{
			if (pref[i] == parties)
			{
				for (int k = i+1; k < N; ++k)
				{
					if (rightanswers[k] > 0 && pref[i] >= 2*parties && pref[i] % 2*parties == 0)
					{
						answer += rightanswers[k];
						break;
					}
				}
			}

		}
		cout << answer;
		return 0;
	}
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 1000000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long answer = 0;
long long a[600000];
long long pref[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	const int parties = pref[N-1]/3;
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 600000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long  answer = 0;
long long a[600000];
long long pref[600000];
long long sufic[600000];
long long leftanswers[600000];
long long rightanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	for (int i = N-1; i >= 0; --i)
	{
		if (i < N-1)
			sufic[i] = a[i] + sufic[i+1];
		else
			sufic[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	long long  cc = 1;
	long long  maxCount = 0;
	int first = 0;
	for (int i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
			if (first == 0)
			{
				first = i;
			}
		}
	}
	cc = 1;
	for (int i = N-1; i >= 0; --i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			rightanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (int i = first; i < N; ++i)
		{
			if (pref[i] == parties)
			{
				for (int k = i+1; k < N; ++k)
				{
					if (rightanswers[k] > 0)
					{
						answer += rightanswers[k];
						break;
					}
				}
			}

		}
		if (answer == 0) {
			cout << 0;
			return 0;
		}
		cout << (parties == 0 ? answer : answer);
		return 0;
	}
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 1000000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long  answer = 0;
long long a[600000];
long long pref[600000];
long long sufic[600000];
long long leftanswers[600000];
long long rightanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	for (int i = N-1; i >= 0; --i)
	{
		if (i < N-1)
			sufic[i] = a[i] + sufic[i+1];
		else
			sufic[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	long long  cc = 1;
	long long  maxCount = 0;
	int first = 0;
	for (int i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] == parties)
		{
			if (first == 0)
			{
				first = i;
			}
		}
	}
	cc = 1;
	for (int i = N-1; i >= 0; --i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] >= 2*parties && pref[i] % 2*parties == 0)
		{
			rightanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (int i = first; i < N-1; ++i)
		{
			if (pref[i] == parties)
			{
				for (int k = i+1; k < N; ++k)
				{
					if (rightanswers[k] > 0)
					{
						answer += rightanswers[k];
						break;
					}
				}
			}

		}
		if (answer == 0) {
			cout << 0;
			return 0;
		}
		cout << (parties == 0 ? answer : answer);
		return 0;
	}
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 1000000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long  answer = 0;
long long a[600000];
long long pref[600000];
long long sufic[600000];
long long leftanswers[600000];
long long rightanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	for (int i = N-1; i >= 0; --i)
	{
		if (i < N-1)
			sufic[i] = a[i] + sufic[i+1];
		else
			sufic[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	long long  cc = 1;
	long long  maxCount = 0;

	for (int i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
	}
	cc = 1;
	for (int i = N-1; i >= 0; --i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			rightanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (int i = 1; i < N; ++i)
		{
			if (pref[i] == parties)
			{
				for (int k = i+1; k < N; ++k)
				{
					if (rightanswers[k] > 0)
					{
						answer += rightanswers[k];
						break;
					}
				}
			}

		}
		if (answer == 0) {
			cout << 0;
			return 0;
		}
		cout << (parties == 0 ? answer : answer);
		return 0;
	}
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 1000000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long  answer = 0;
long long a[600000];
long long pref[600000];
long long sufic[600000];
long long leftanswers[600000];
long long rightanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	for (int i = N-1; i >= 0; --i)
	{
		if (i < N-1)
			sufic[i] = a[i] + sufic[i+1];
		else
			sufic[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	long long  cc = 1;
	long long  maxCount = 0;
	long long first = 0;
	for (long long i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] == parties)
		{
			if (first == 0)
			{
				first = i;
			}
		}
	}
	cc = 0;
	long long partNear = first;
	for (long long i = N-2; i >= 0; --i)
	{
		if (pref[i] == parties)
		{
			partNear = i;
			rightanswers[partNear] = cc;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] >= 2*parties && pref[i] % 2*parties == 0)
		{
			//rightanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (long long i = 0; i < N-1; ++i)
		{
			if (pref[i] == parties)
			{
				//for (int k = i+1; k < N; ++k)
				{
					//if (rightanswers[k] > 0 && pref[k] >= 2*parties && pref[k] % 2*parties == 0)
					{
						answer += rightanswers[i];
						//break;
					}
				}
			}

		}
		//cout << answer;
		//return 0;
	}

		answer = 0;
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 600000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long  answer = 0;
long long a[600000];
long long pref[600000];
long long sufic[600000];
long long leftanswers[600000];
long long rightanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	for (int i = N-1; i >= 0; --i)
	{
		if (i < N-1)
			sufic[i] = a[i] + sufic[i+1];
		else
			sufic[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	long long  cc = 1;
	long long  maxCount = 0;
	int first = 0;
	for (int i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] == parties)
		{
			if (first == 0)
			{
				first = i;
			}
		}
	}
	cc = 1;
	for (int i = N-1; i >= 0; --i)
	{
			rightanswers[i] = cc;
			maxCount = cc;
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] >= 2*parties && pref[i] % 2*parties == 0)
		{
			cc++;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (int i = first; i < N-1; ++i)
		{
			if (pref[i] == parties)
			{
				for (int k = i+1; k < N; ++k)
				{
					if (rightanswers[k] > 0 && pref[k] >= 2*parties && pref[k] % 2*parties == 0)
					{
						answer += rightanswers[k];
						break;
					}
				}
			}

		}
		cout << answer;
		return 0;
	}
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 1000000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long  answer = 0;
long long a[600000];
long long pref[600000];
long long sufic[600000];
long long leftanswers[600000];
long long rightanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	for (int i = N-1; i >= 0; --i)
	{
		if (i < N-1)
			sufic[i] = a[i] + sufic[i+1];
		else
			sufic[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	long long  cc = 1;
	long long  maxCount = 0;
	long long first = 0;
	for (long long i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] == parties)
		{
			if (first == 0)
			{
				first = i;
			}
		}
	}
	cc = 0;
	long long partNear = first;
	for (long long i = N-2; i >= 0; --i)
	{
		if (pref[i] == parties)
		{
			partNear = i;
			rightanswers[partNear] = cc;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] >= 2*parties && pref[i] % 2*parties == 0)
		{
			//rightanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (long long i = first; i < N-1; ++i)
		{
			if (pref[i] == parties)
			{
				//for (int k = i+1; k < N; ++k)
				{
					//if (rightanswers[k] > 0 && pref[k] >= 2*parties && pref[k] % 2*parties == 0)
					{
						answer += rightanswers[i];
						//break;
					}
				}
			}

		}
		cout << answer;
		return 0;
	}

	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 1000000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long  answer = 0;
long long a[600000];
long long pref[600000];
long long sufic[600000];
long long leftanswers[600000];
long long rightanswers[600000];
int N;
long long counter = 0;

void longSolve()
{
	long long parties = pref[N-1]/3;
	answer = 0;
	for (int i = 0; i < N-2; ++i)
	{
		long long part1 = pref[i];
		if(part1 == parties)
		{
			for (long long j = i+1; j < N-1; ++j)
			{
				long long part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					long long part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 451000000)
				{
					cout << answer;
					return ;
				}
			}
		}
	}
	cout << answer;
}

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}

	long long parties = pref[N-1] / 3;
	long long  cc = 0;
	for (long long i = N-2; i >= 0; --i)
	{
		if (pref[i] == parties)
		{
			rightanswers[i] = cc;
		}
		if ( parties == 0 && pref[i] == 0 ||
		     parties != 0 && pref[i] == 2*parties)
		{
			cc++;
		}
	}

	answer = 0;
	for (long long i = 0; i < N-1; ++i)
	{
		if (pref[i] == parties)
		{
			answer += rightanswers[i];
		}
	}

	cout << answer;

	//longSolve();
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long answer = 0;
long long a[600000];
long long pref[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	const int parties = pref[N-1]/3;
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = ( i == 0 ? a[i] : pref[i] );
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 6000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long  answer = 0;
long long a[600000];
long long pref[600000];
long long sufic[600000];
long long leftanswers[600000];
long long rightanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	for (int i = N-1; i >= 0; --i)
	{
		if (i < N-1)
			sufic[i] = a[i] + sufic[i+1];
		else
			sufic[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	long long  cc = 1;
	long long  maxCount = 0;
	long long first = 0;
	for (long long i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] == parties)
		{
			if (first == 0)
			{
				first = i;
			}
		}
	}
	cc = 0;
	long long partNear = first;
	for (long long i = N-2; i >= 0; --i)
	{
		if (pref[i] == parties)
		{
			partNear = i;
			rightanswers[partNear] = cc;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] >= 2*parties && pref[i] % 2*parties == 0)
		{
			//rightanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (long long i = 0; i < N-1; ++i)
		{
			if (pref[i] == parties)
			{
				//for (int k = i+1; k < N; ++k)
				{
					//if (rightanswers[k] > 0 && pref[k] >= 2*parties && pref[k] % 2*parties == 0)
					{
						answer += rightanswers[i];
						//break;
					}
				}
			}

		}
		//cout << answer;
		//return 0;
	}

		answer = 0;
	for (int i = 0; i < N-2; ++i)
	{
		long long part1 = pref[i];
		if(part1 == parties)
		{
			for (long long j = i+1; j < N-1; ++j)
			{
				long long part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					long long part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 520000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long answer = 0;
long long a[600000];
long long pref[600000];
long long leftanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	int cc = 1;
	int maxCount = 0;
	int firstt = -1;
	for (int i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 || parties != 0 && pref[i] % parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
			if (firstt == -1)
				firstt = i;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (int i = firstt; i < N; ++i)
		{
			if (parties != 1)
			{
				if ( parties == 0 && leftanswers[i] > 0 || parties != 0 && pref[i] % 2*parties == 0)
				{
					answer += (maxCount - leftanswers[i]);

				}
			}
			else
			{
				if (pref[i] == 1) {
					answer += (maxCount - leftanswers[i]);
				}
			}
		}
		if (answer == 0) {
			cout << 0;
			return 0;
		}
		cout << (parties == 0 ? answer : answer-1);
		return 0;
	}
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 1000000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long answer = 0;
long long a[600000];
long long pref[600000];
long long leftanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	int cc = 1;
	int maxCount = 0;
	int firstt = -1;
	for (int i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 || parties != 0 && pref[i] % parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
			if (firstt == -1)
				firstt = i;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (int i = firstt; i < N; ++i)
		{
			if ( parties == 0 && leftanswers[i] > 0 || parties != 0 && pref[i] % 2*parties == 0)
			{
				answer += (maxCount - leftanswers[i]);

			}
		}

		cout << parties == 0 ? answer : answer-1;
		return 0;
	}
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 1000000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long  answer = 0;
long long a[600000];
long long pref[600000];
long long sufic[600000];
long long leftanswers[600000];
long long rightanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	for (int i = N-1; i >= 0; --i)
	{
		if (i < N-1)
			sufic[i] = a[i] + sufic[i+1];
		else
			sufic[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	long long  cc = 1;
	long long  maxCount = 0;
	long long first = 0;
	for (long long i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] == parties)
		{
			if (first == 0)
			{
				first = i;
			}
		}
	}
	cc = 0;
	long long partNear = first;
	for (long long i = N-2; i >= 0; --i)
	{
		if (pref[i] == parties)
		{
			partNear = i;
			rightanswers[partNear] = cc;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] >= 2*parties && pref[i] % 2*parties == 0)
		{
			//rightanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (long long i = 0; i < N-1; ++i)
		{
			if (pref[i] == parties)
			{
				//for (int k = i+1; k < N; ++k)
				{
					//if (rightanswers[k] > 0 && pref[k] >= 2*parties && pref[k] % 2*parties == 0)
					{
						answer += rightanswers[i];
						//break;
					}
				}
			}

		}
		//cout << answer;
		//return 0;
	}

		answer = 0;
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 450000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long  answer = 0;
long long a[600000];
long long pref[600000];
long long sufic[600000];
long long leftanswers[600000];
long long rightanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	for (int i = N-1; i >= 0; --i)
	{
		if (i < N-1)
			sufic[i] = a[i] + sufic[i+1];
		else
			sufic[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	long long  cc = 1;
	long long  maxCount = 0;
	long long first = 0;
	for (long long i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] == parties)
		{
			if (first == 0)
			{
				first = i;
			}
		}
	}
	cc = 0;
	long long partNear = first;
	for (long long i = N-2; i >= 0; --i)
	{
		if (pref[i] == parties)
		{
			partNear = i;
			rightanswers[partNear] = cc;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] >= 2*parties && pref[i] % 2*parties == 0)
		{
			//rightanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (long long i = 0; i < N-1; ++i)
		{
			if (pref[i] == parties)
			{
				//for (int k = i+1; k < N; ++k)
				{
					//if (rightanswers[k] > 0 && pref[k] >= 2*parties && pref[k] % 2*parties == 0)
					{
						answer += rightanswers[i];
						//break;
					}
				}
			}

		}
		cout << answer;
		return 0;
	}

	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 1000000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

long long  answer = 0;
long long a[600000];
long long pref[600000];
long long sufic[600000];
long long leftanswers[600000];
long long rightanswers[600000];
int N;
long long counter = 0;

int main(void)
{
	ios_base::sync_with_stdio(0);
	//freopen("ancestor.in", "rt", stdin);
	//freopen("ancestor.out", "wt", stdout);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		if (i > 0)
			pref[i] = a[i] + pref[i-1];
		else
			pref[i] = a[i];
	}
	for (int i = N-1; i >= 0; --i)
	{
		if (i < N-1)
			sufic[i] = a[i] + sufic[i+1];
		else
			sufic[i] = a[i];
	}
	long long parties = pref[N-1]/3;
	long long  cc = 1;
	long long  maxCount = 0;
	long long first = 0;
	for (long long i = 1; i < N; ++i)
	{
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] % 2*parties == 0)
		{
			leftanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] == parties)
		{
			if (first == 0)
			{
				first = i;
			}
		}
	}
	cc = 0;
	long long partNear = first;
	for (long long i = N-2; i >= 0; --i)
	{
		if (pref[i] == parties)
		{
			partNear = i;
			rightanswers[partNear] = cc;
		}
		if ( parties == 0 && pref[i] == 0 ||
			parties == 1 && pref[i] == 1 ||
			parties != 0 && pref[i] >= 2*parties && pref[i] % 2*parties == 0)
		{
			//rightanswers[i] = cc;
			maxCount = cc;
			cc++;
		}
	}
	//if (parties == 0)
	{
		answer = 0;
		for (long long i = 0; i < N-1; ++i)
		{
			if (pref[i] == parties)
			{
				//for (int k = i+1; k < N; ++k)
				{
					//if (rightanswers[k] > 0 && pref[k] >= 2*parties && pref[k] % 2*parties == 0)
					{
						answer += rightanswers[i];
						//break;
					}
				}
			}

		}
		//cout << answer;
		//return 0;
	}

		answer = 0;
	for (int i = 0; i < N-2; ++i)
	{
		int part1 = pref[i];
		if(part1 == parties)
		{
			for (int j = i+1; j < N-1; ++j)
			{
				int part2 = ( pref[j] - pref[i] );
				if(part2 == parties)
				{
					int part3 = pref[N-1] - pref[j];
					if(part3 == parties)
					{
						answer++;
					}
				}
				counter++;
				if (counter > 700000000)
				{
					cout << answer;
					return 0;
				}
			}
		}
	}
	cout << answer;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll n,a[500005];
int main(){
	ll  i,j,k,nb,c,x,sum=0,s=0,sumright=0,sumleft=0,nbright=0,nbleft=0,posright=0,posleft=n,posfinal=1;
	cin>>n;
	for(i=1;i<=n;i++){
		cin>>a[i];
		s+=a[i];
	}
	if(s%3){
		cout<<"0";
		return  0;
	}

    for(i=1;i<n;i++){
    	sumleft+=a[i];
        if(sumleft==2*(s/3))sum+=nbleft;
		if(sumleft==s/3)nbleft++;
	}
	cout<<sum;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll n,a[500005];
/***************************************/
ll solve(ll i,ll j,ll s,ll sum){
	if(i==j+1)return 0;
	if(s+a[i]==sum)return 1+solve(i+1,j,s+a[i],sum)+solve(i+1,j,0,sum);
	else return solve(i+1,j,s+a[i],sum)+solve(i+1,j,0,sum);
}
/****************************************/
int main(){
	ll  i,j,k,nb,c,x,sum,s=0,sumright=0,sumleft=0,nbright=0,nbleft=0,posright=0,posleft=n,posfinal=1;
	cin>>n;
	for(i=1;i<=n;i++){
		cin>>a[i];
		s+=a[i];
	}
	if(s%3){
		cout<<"0";
		return  0;
	}
	for(i=1;i<=n;i++){
		sumleft+=a[i];
		if(sumleft==s/3)break;
	}
	if(i==n+1){
		cout<<"0";return 0;
	}
	posleft=i;
	for(i=n;i>=1;i--){
		sumright+=a[i];
		if(sumright==s/3)break;
	}
	if(i==0){
		cout<<"0";return 0;
	}
	posright=i;
	//cout<<posleft<<" "<<posright<<endl;
	cout<<solve(posleft+1,posright-1,0,s/3);

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll n,a[500005];

int main(){
	ll  i,j,k,nb,c,x,sum,s=0,sumright=0,sumleft=0,nbright=0,nbleft=0,posright=0,posleft=n,posfinal=1;
	cin>>n;
	for(i=1;i<=n;i++){
		cin>>a[i];
		s+=a[i];
	}
	if(s%3){
		cout<<"0";
		return  0;
	}
	for(i=1;i<=n;i++){
		sumleft+=a[i];
		if(sumleft==s/3)break;
	}
	if(i==n+1){
		cout<<"0";return 0;
	}
	posleft=i;
	for(i=n;i>=1;i--){
		sumright+=a[i];
		if(sumright==s/3)break;
	}
	if(i==0){
		cout<<"0";return 0;
	}
	posright=i;
	sumright=0;sumleft=0;
	for(i=posleft+1;i<posright;i++){
		sumleft+=a[i];
		if(sumleft==0){
			posfinal=i;
		}

	}
	for(i=posright-1;i>posleft&&i>posfinal;i--){
		sumright+=a[i];
		if(!sumright)nbright++;
	}
	nbright++;nbleft++;;
	cout<<nbright<<" "<<nbleft<<endl;
	cout<<nbright*nbleft;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll n,a[500005];
int main(){
	ll  i,j,k,nb,c,x,sum=0,s=0,sumright=0,sumleft=0,nbright=0,nbleft=0,posright=0,posleft=n,posfinal=1;
	cin>>n;
	for(i=1;i<=n;i++){
		cin>>a[i];
		s+=a[i];
	}
	if(s%3){
		cout<<"0";
		return  0;
	}
	sumleft+=a[1];
    for(i=1;i<n;i++){
    	if(sumleft==s/3)nbleft++;
        if(sumleft==2*(s/3))sum+=nbleft;
    	sumleft+=a[i];
	}
	cout<<sum;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll n,a[500005];

int main(){
	ll  i,j,k,nb,c,x,sum,s=0,sumright=0,sumleft=0,nbright=0,nbleft=0,posright=0,posleft=n,posfinal;
	cin>>n;
	for(i=1;i<=n;i++){
		cin>>a[i];
		s+=a[i];
	}
	if(s%3){
		cout<<"0";
		return  0;
	}
	for(i=1;i<=n;i++){
		sumleft+=a[i];
		if(sumleft==s/3)break;
	}
	if(i==n+1){
		cout<<"0";return 0;
	}
	posleft=i;
	for(i=n;i>=1;i--){
		sumright+=a[i];
		if(sumright==s/3)break;
	}
	if(i==0){
		cout<<"0";return 0;
	}
	posright=i;
	sumright=0;sumleft=0;
	for(i=posleft+1;i<posright;i++){
		sumleft+=a[i];
		if(sumleft==0){
			posfinal=i;
		}

	}
	for(i=posright-1;i>posleft&&i>posfinal;i--){
		sumright+=a[i];
		if(!sumright)nbright++;
	}
	nbright++;nbleft++;
	cout<<nbright*nbleft;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll n,a[500005];

int main(){
	ll  i,j,k,nb,c,x,sum,s=0,sumright=0,sumleft=0,nbright=0,nbleft=0,posright=0,posleft=n,posfinal=1;
	cin>>n;
	for(i=1;i<=n;i++){
		cin>>a[i];
		s+=a[i];
	}
	if(s%3){
		cout<<"0";
		return  0;
	}
	for(i=1;i<=n;i++){
		sumleft+=a[i];
		if(sumleft==s/3)break;
	}
	if(i==n+1){
		cout<<"0";return 0;
	}
	posleft=i;
	for(i=n;i>=1;i--){
		sumright+=a[i];
		if(sumright==s/3)break;
	}
	if(i==0){
		cout<<"0";return 0;
	}
	posright=i;
	sumright=0;sumleft=0;
	for(i=posleft+1;i<posright;i++){
		sumleft+=a[i];
		if(sumleft==0){
			posfinal=i;
		}

	}
	for(i=posright-1;i>posleft&&i>posfinal;i--){
		sumright+=a[i];
		if(!sumright)nbright++;
	}
	nbright++;nbleft++;;
	//cout<<nbright<<" "<<nbleft<<endl;
	cout<<nbright*nbleft;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll n,a[500005];
int main(){
	ll  i,j,k,nb,c,x,sum=0,s=0,sumright=0,sumleft=0,nbright=0,nbleft=0,posright=0,posleft=n,posfinal=1;
	cin>>n;
	for(i=1;i<=n;i++){
		cin>>a[i];
		s+=a[i];
	}
	if(s%3){
		cout<<"0";
		return  0;
	}
	sumleft+=a[1];
    for(i=1;i<n;i++){
    	if(sumleft==s/3)nbleft++;
    	else if(sumleft==2*(s/3))sum+=nbleft;
    	sumleft+=a[i];
	}
	cout<<sum;

}


#include	<bits/stdc++.h>
using 		namespace 	std;
typedef long long int ll;
#define 	fast 		ios_base::sync_with_stdio(false);cin.tie(NULL);
#define 	fr(i,l,r) 	for(ll i=(l);i<(r);i++)
#define 	test(t) 	ll t;cin>>t; while(t--)
#define 	debug(x) 	cout<<" x = "<<x<<" \n";
#define 	inn 		freopen("input.txt", "r", stdin)
#define 	outt 		freopen("output.txt", "w", stdout)
#define 	vl			vector<ll>
#define 	endl		"\n";
#define 	vvl 		vector<vector<ll> >
#define 	all(c) 		c.begin(),c.end()
#define 	pb			push_back
#define 	mp			make_pair
#define 	pl			pair<ll,ll>
#define 	vpl 		vector<pl >
#define 	MAX			200001
#define 	LONG_MAX1	9223372036854775806
#define		print(v)	for(auto i:v)cout<<i<<" ";cout<<endl;

int main(){
	int n;
	cin>>n;
	vector<int> nums(n);
	int sum=0;
	int res=0;
	fr(i,0,n) {cin>>nums[i]; sum+=nums[i];}
	if(sum%3!=0){
		cout<<"0"<<endl;
	}
	else{
		sum=sum/3;
		vector<int> count(n);
		int t=0;
		for(int i=n-1; i>=0; i--){
			t+=nums[i];
			if(sum==t){
				count[i]=1;
			}
		}
		for(int i=n-2; i>=0; i--)
			count[i]+=count[i+1];
		t=0;
		for(int i=0; i<n-2; i++){
			t+=nums[i];
			if(t==sum){
				res+=count[i+2];
			}
		}
		cout<<res<<endl;
	}
	return 0;
}


#include	<bits/stdc++.h>
using 		namespace 	std;
typedef long long int ll;
#define 	fast 		ios_base::sync_with_stdio(false);cin.tie(NULL);
#define 	fr(i,l,r) 	for(ll i=(l);i<(r);i++)
#define 	test(t) 	ll t;cin>>t; while(t--)
#define 	debug(x) 	cout<<" x = "<<x<<" \n";
#define 	inn 		freopen("input.txt", "r", stdin)
#define 	outt 		freopen("output.txt", "w", stdout)
#define 	vl			vector<ll>
#define 	endl		"\n";
#define 	vvl 		vector<vector<ll> >
#define 	all(c) 		c.begin(),c.end()
#define 	pb			push_back
#define 	mp			make_pair
#define 	pl			pair<ll,ll>
#define 	vpl 		vector<pl >
#define 	LONG_MAX1	9223372036854775806
#define		print(v)	for(auto i:v)cout<<i<<" ";cout<<endl;

#define MAX 100005

int main() {
	ll n;
	cin>>n;
	ll sum=0;
	vl nums(n);
	fr(i,0,n){
		cin>>nums[i];
		sum+=nums[i];
	}
	if(sum%3!=0){
		cout<<"0"<<endl;
		return 0;
	}
	sum/=3;
	ll f=0,s=0;
	ll t=0;
	fr(i,0,n){
		t+=nums[i];
		if(t==sum)
			f++;
		if(t==2*sum)
			s++;
	}
	cout<<f*s<<endl;

}


#include	<bits/stdc++.h>
using 		namespace 	std;
typedef long long int ll;
#define 	fast 		ios_base::sync_with_stdio(false);cin.tie(NULL);
#define 	fr(i,l,r) 	for(ll i=(l);i<(r);i++)
#define 	test(t) 	ll t;cin>>t; while(t--)
#define 	debug(x) 	cout<<" x = "<<x<<" \n";
#define 	inn 		freopen("input.txt", "r", stdin)
#define 	outt 		freopen("output.txt", "w", stdout)
#define 	vl			vector<ll>
#define 	endl		"\n";
#define 	vvl 		vector<vector<ll> >
#define 	all(c) 		c.begin(),c.end()
#define 	pb			push_back
#define 	mp			make_pair
#define 	pl			pair<ll,ll>
#define 	vpl 		vector<pl >
#define 	LONG_MAX1	9223372036854775806
#define		print(v)	for(auto i:v)cout<<i<<" ";cout<<endl;

#define MAX 100005

int main() {
  int n;
  scanf("%d", &n);
  ll sum=0;
  vl nums(n);
  for (int i = 0; i < n; i++) {
    cin>>nums[i];
    sum+=nums[i];
  }
  long long ans = 0;
  if (sum% 3 == 0) {
    long long u = sum / 3, v = 2 * sum / 3;
    long long cnt = 0,t=0;

    for (int i = 0; i < n; i++) {
    	t+=nums[i];
      if (t == v) {
        ans += cnt;
      }
      if (t == u) {
        cnt++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


#include	<bits/stdc++.h>
using 		namespace 	std;
typedef long long int ll;
#define 	fast 		ios_base::sync_with_stdio(false);cin.tie(NULL);
#define 	fr(i,l,r) 	for(ll i=(l);i<(r);i++)
#define 	test(t) 	ll t;cin>>t; while(t--)
#define 	debug(x) 	cout<<" x = "<<x<<" \n";
#define 	inn 		freopen("input.txt", "r", stdin)
#define 	outt 		freopen("output.txt", "w", stdout)
#define 	vl			vector<ll>
#define 	endl		"\n";
#define 	vvl 		vector<vector<ll> >
#define 	all(c) 		c.begin(),c.end()
#define 	pb			push_back
#define 	mp			make_pair
#define 	pl			pair<ll,ll>
#define 	vpl 		vector<pl >
#define 	LONG_MAX1	9223372036854775806
#define		print(v)	for(auto i:v)cout<<i<<" ";cout<<endl;

#define MAX 100005

long long s[1234567];

int main() {
  int n;
  scanf("%d", &n);
  s[0] = 0;
  for (int i = 0; i < n; i++) {
    int foo;
    scanf("%d", &foo);
    s[i + 1] = s[i] + foo;
  }
  long long ans = 0;
  if (s[n] % 3 == 0) {
    long long u = s[n] / 3, v = 2 * s[n] / 3;
    long long cnt = 0;
    for (int i = 1; i <=n; i++) {
      if (s[i] == v) {
        ans += cnt;
      }
      if (s[i] == u) {
        cnt++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


#include	<bits/stdc++.h>
using 		namespace 	std;
typedef long long int ll;
#define 	fast 		ios_base::sync_with_stdio(false);cin.tie(NULL);
#define 	fr(i,l,r) 	for(ll i=(l);i<(r);i++)
#define 	test(t) 	ll t;cin>>t; while(t--)
#define 	debug(x) 	cout<<" x = "<<x<<" \n";
#define 	inn 		freopen("input.txt", "r", stdin)
#define 	outt 		freopen("output.txt", "w", stdout)
#define 	vl			vector<ll>
#define 	endl		"\n";
#define 	vvl 		vector<vector<ll> >
#define 	all(c) 		c.begin(),c.end()
#define 	pb			push_back
#define 	mp			make_pair
#define 	pl			pair<ll,ll>
#define 	vpl 		vector<pl >
#define 	LONG_MAX1	9223372036854775806
#define		print(v)	for(auto i:v)cout<<i<<" ";cout<<endl;

#define MAX 100005

long long s[1234567];

int main() {
  int n;
  scanf("%d", &n);
  s[0] = 0;
  for (int i = 0; i < n; i++) {
    int foo;
    scanf("%d", &foo);
    s[i + 1] = s[i] + foo;
  }
  long long ans = 0;
  long long cnt = 0;
  if (s[n] % 3 == 0) {
    long long u = s[n] / 3, v = 2 * s[n] / 3;
    for (int i = 1; i <n; i++) {
      if (s[i] == v) {
        ans += cnt;
      }
      if (s[i] == u) {
        cnt++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


#include	<bits/stdc++.h>
using 		namespace 	std;
typedef long long int ll;
#define 	fast 		ios_base::sync_with_stdio(false);cin.tie(NULL);
#define 	fr(i,l,r) 	for(ll i=(l);i<(r);i++)
#define 	test(t) 	ll t;cin>>t; while(t--)
#define 	debug(x) 	cout<<" x = "<<x<<" \n";
#define 	inn 		freopen("input.txt", "r", stdin)
#define 	outt 		freopen("output.txt", "w", stdout)
#define 	vl			vector<ll>
#define 	endl		"\n";
#define 	vvl 		vector<vector<ll> >
#define 	all(c) 		c.begin(),c.end()
#define 	pb			push_back
#define 	mp			make_pair
#define 	pl			pair<ll,ll>
#define 	vpl 		vector<pl >
#define 	MAX			200001
#define 	LONG_MAX1	9223372036854775806
#define		print(v)	for(auto i:v)cout<<i<<" ";cout<<endl;

int main(){
	ll n;
	cin>>n;
	vector<ll> nums(n);
	ll sum=0;
	ll res=0;
	fr(i,0,n) {cin>>nums[i]; sum+=nums[i];}
	if(sum%3!=0){
		cout<<"0"<<endl;
	}
	else{
		sum=sum/3;
		vector<ll> count(n);
		ll t=0;
		for(ll i=n-1; i>=0; i--){
			t+=nums[i];
			if(sum==t){
				count[i]=1;
			}
		}
		for(ll i=n-2; i>=0; i--)
			count[i]+=count[i+1];
		t=0;
		for(ll i=0; i<n-2; i++){
			t+=nums[i];
			if(t==sum){
				res+=count[i+2];
			}
		}
		cout<<res<<endl;
	}
	return 0;
}


#include	<bits/stdc++.h>
using 		namespace 	std;
typedef long long int ll;
#define 	fast 		ios_base::sync_with_stdio(false);cin.tie(NULL);
#define 	fr(i,l,r) 	for(ll i=(l);i<(r);i++)
#define 	test(t) 	ll t;cin>>t; while(t--)
#define 	debug(x) 	cout<<" x = "<<x<<" \n";
#define 	inn 		freopen("input.txt", "r", stdin)
#define 	outt 		freopen("output.txt", "w", stdout)
#define 	vl			vector<ll>
#define 	endl		"\n";
#define 	vvl 		vector<vector<ll> >
#define 	all(c) 		c.begin(),c.end()
#define 	pb			push_back
#define 	mp			make_pair
#define 	pl			pair<ll,ll>
#define 	vpl 		vector<pl >
#define 	LONG_MAX1	9223372036854775806
#define		print(v)	for(auto i:v)cout<<i<<" ";cout<<endl;

#define MAX 100005

int main() {
	ll n;
	cin>>n;
	ll sum=0;
	vl nums(n);
	fr(i,0,n){
		cin>>nums[i];
		sum+=nums[i];
	}
	if(sum%3!=0){
		cout<<"0"<<endl;
		return 0;
	}
	sum/=3;
	ll f=0,s=0;
	ll t=0;
	fr(i,0,n){
		t+=nums[i];
		if(t==sum)
			f++;
		if(t==2*sum)
			s+=f;
	}
	cout<<s<<endl;
}


#include	<bits/stdc++.h>
using 		namespace 	std;
typedef long long int ll;
#define 	fast 		ios_base::sync_with_stdio(false);cin.tie(NULL);
#define 	fr(i,l,r) 	for(ll i=(l);i<(r);i++)
#define 	test(t) 	ll t;cin>>t; while(t--)
#define 	debug(x) 	cout<<" x = "<<x<<" \n";
#define 	inn 		freopen("input.txt", "r", stdin)
#define 	outt 		freopen("output.txt", "w", stdout)
#define 	vl			vector<ll>
#define 	endl		"\n";
#define 	vvl 		vector<vector<ll> >
#define 	all(c) 		c.begin(),c.end()
#define 	pb			push_back
#define 	mp			make_pair
#define 	pl			pair<ll,ll>
#define 	vpl 		vector<pl >
#define 	LONG_MAX1	9223372036854775806
#define		print(v)	for(auto i:v)cout<<i<<" ";cout<<endl;

#define MAX 100005

long long s[1234567];

int main() {
  int n;
  scanf("%d", &n);
  s[0] = 0;
  for (int i = 0; i < n; i++) {
    int foo;
    scanf("%d", &foo);
    s[i + 1] = s[i] + foo;
  }
  long long ans = 0;
  long long cnt = 0;
  if (s[n] % 3 == 0) {
    long long u = s[n] / 3, v = 2 * s[n] / 3;

    for (int i = 1; i < n; i++) {
      if (s[i] == v) {
        ans++;
      }
      if (s[i] == u) {
        cnt++;
      }
    }
  }
  cout << ans*cnt << endl;
  return 0;
}


#include	<bits/stdc++.h>
using 		namespace 	std;
typedef long long int ll;
#define 	fast 		ios_base::sync_with_stdio(false);cin.tie(NULL);
#define 	fr(i,l,r) 	for(ll i=(l);i<(r);i++)
#define 	test(t) 	ll t;cin>>t; while(t--)
#define 	debug(x) 	cout<<" x = "<<x<<" \n";
#define 	inn 		freopen("input.txt", "r", stdin)
#define 	outt 		freopen("output.txt", "w", stdout)
#define 	vl			vector<ll>
#define 	endl		"\n";
#define 	vvl 		vector<vector<ll> >
#define 	all(c) 		c.begin(),c.end()
#define 	pb			push_back
#define 	mp			make_pair
#define 	pl			pair<ll,ll>
#define 	vpl 		vector<pl >
#define 	LONG_MAX1	9223372036854775806
#define		print(v)	for(auto i:v)cout<<i<<" ";cout<<endl;

#define MAX 100005

ll dp[MAX];
ll countt[MAX];

int main(){
    ll n;
    cin>>n;
    fr(i,0,n){
    	ll a;
    	cin>>a;
    	countt[a]++;
    }
    dp[0]=0;
    dp[1]=1;
    fr(i,2,MAX){
    	dp[i]=max(dp[i-1],dp[i-2]+i*countt[i]);
    }
    cout<<dp[MAX-1];
}


#include	<bits/stdc++.h>
using 		namespace 	std;
typedef long long int ll;
#define 	fast 		ios_base::sync_with_stdio(false);cin.tie(NULL);
#define 	fr(i,l,r) 	for(ll i=(l);i<(r);i++)
#define 	test(t) 	ll t;cin>>t; while(t--)
#define 	debug(x) 	cout<<" x = "<<x<<" \n";
#define 	inn 		freopen("input.txt", "r", stdin)
#define 	outt 		freopen("output.txt", "w", stdout)
#define 	vl			vector<ll>
#define 	endl		"\n";
#define 	vvl 		vector<vector<ll> >
#define 	all(c) 		c.begin(),c.end()
#define 	pb			push_back
#define 	mp			make_pair
#define 	pl			pair<ll,ll>
#define 	vpl 		vector<pl >
#define 	LONG_MAX1	9223372036854775806
#define		print(v)	for(auto i:v)cout<<i<<" ";cout<<endl;

#define MAX 100005

int main() {
  int n;
  scanf("%d", &n);
  ll sum=0;
  vl nums(n);
  for (int i = 0; i < n; i++) {
    cin>>nums[i];
    sum+=nums[i];
  }
  long long ans = 0;
  if (sum% 3 == 0) {
    long long u = sum / 3, v = 2 * sum / 3;
    long long cnt = 0,t=0;

    for (int i = 0; i < n; i++) {
    	t+=s[i];
      if (t == v) {
        ans += cnt;
      }
      if (t == u) {
        cnt++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


#include	<bits/stdc++.h>
using 		namespace 	std;
typedef long long int ll;
#define 	fast 		ios_base::sync_with_stdio(false);cin.tie(NULL);
#define 	fr(i,l,r) 	for(ll i=(l);i<(r);i++)
#define 	test(t) 	ll t;cin>>t; while(t--)
#define 	debug(x) 	cout<<" x = "<<x<<" \n";
#define 	inn 		freopen("input.txt", "r", stdin)
#define 	outt 		freopen("output.txt", "w", stdout)
#define 	vl			vector<ll>
#define 	endl		"\n";
#define 	vvl 		vector<vector<ll> >
#define 	all(c) 		c.begin(),c.end()
#define 	pb			push_back
#define 	mp			make_pair
#define 	pl			pair<ll,ll>
#define 	vpl 		vector<pl >
#define 	LONG_MAX1	9223372036854775806
#define		print(v)	for(auto i:v)cout<<i<<" ";cout<<endl;

#define MAX 100005

long long s[1234567];

int main() {
  int n;
  scanf("%d", &n);
  s[0] = 0;
  for (int i = 0; i < n; i++) {
    int foo;
    scanf("%d", &foo);
    s[i + 1] = s[i] + foo;
  }
  long long ans = 0;
  long long cnt = 0;
  if (s[n] % 3 == 0) {
    long long u = s[n] / 3, v = 2 * s[n] / 3;
    for (int i = 1; i <n; i++) {
      if (s[i] == v) {
        ans ++;
      }
      if (s[i] == u) {
        cnt++;
      }
    }
  }
  cout << ans*cnt << endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <functional>   // std::minus
#include <numeric>      // std::accumulate

using namespace std;

const bool brute_force = false;

int sum(vector<int>&v, int i, int j) {
  if (brute_force) return accumulate(v.begin() + i, v.begin() + j + 1, 0, plus<int>());
  // v is prefix.
  else {
    if (i == 0) return v[j];
    else return v[j] - v[i - 1];
  }
}

vector<int> compute_prefix(vector<int>&v) {
  int n = v.size();
  vector<int>dp(n);
  dp[0] = v[0];
  for (int i = 1; i < n; ++i) {
    dp[i] = dp[i - 1] + v[i];
  }
  return dp;
}

int main() {
  //cout << sum({1,2,3}, 0, 2) << endl;
  // cout << sum({1,3,6,6,9}, 1, 3) << endl;
  int n;
  cin >> n;
  vector<int>v(n, 0);
  for (int i = 0; i < n; ++i) cin >> v[i];
  vector<int>prefix = compute_prefix(v);

  // for (auto x: prefix) cout << x << " ";
  // cout << "\n";

  int nways = 0;
  int i = 0, j = n - 1;
  for (int k = i + 1; k <= j - 1; ++k) {
    int first = sum(brute_force ? v : prefix, i, k - 1);
    if (first > sum(prefix, k, j)) continue; // not possible solution further.
    for (int l = k + 1; l <= j; ++l) {
      int middle = sum(brute_force ? v : prefix, k, l - 1);
      int last = sum(brute_force ? v : prefix, l, j);
      //cout << "(" << k << ", " << l << ") " << first << " " << middle << " " << last << endl;
      if (first == middle && middle == last) {
        nways++;
      }
    }
  }
  cout << nways << endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <functional>   // std::minus
#include <numeric>      // std::accumulate

using namespace std;

const bool brute_force = false;

int sum(vector<long long>&v, int i, int j) {
  if (brute_force) return accumulate(v.begin() + i, v.begin() + j + 1, 0, plus<long long>());
  // v is prefix.
  else {
    if (i == 0) return v[j];
    else return v[j] - v[i - 1];
  }
}

vector<long long> compute_prefix(vector<long long>&v) {
  int n = v.size();
  vector<long long>dp(n);
  dp[0] = v[0];
  for (int i = 1; i < n; ++i) {
    dp[i] = dp[i - 1] + v[i];
  }
  return dp;
}

int n2solution(vector<long long>&v) {
  vector<long long>prefix = compute_prefix(v);
  int n = v.size();
  // for (auto x: prefix) cout << x << " ";
  // cout << "\n";
  int nways = 0;
  int i = 0, j = n - 1;
  for (int k = i + 1; k <= j - 1; ++k) {
    int first = sum(brute_force ? v : prefix, i, k - 1);
    if (first > sum(prefix, k, j)) continue; // not possible solution further.
    for (int l = k + 1; l <= j; ++l) {
      int middle = sum(brute_force ? v : prefix, k, l - 1);
      int last = sum(brute_force ? v : prefix, l, j);
      //cout << "(" << k << ", " << l << ") " << first << " " << middle << " " << last << endl;
      if (first == middle && middle == last) {
        nways++;
      }
    }
  }
  return nways;
}

long long nsolution(vector<long long>&v) {
  long long nways = 0;
  long long S = 0;
  for (const auto& x: v) S += x;
  if (S % 3) return 0;
  long long a = S / 3, b = 2 * S / 3;
  long long cur = 0;
  vector<long long>prefix = compute_prefix(v);
  for (int i = 0; i < v.size() - 1; ++i) {
    int x = prefix[i];
    if (x == b) nways += cur;
    if (x == a) cur++;
  }
  return nways;
}

int main() {
  //cout << sum({1,2,3}, 0, 2) << endl;
  // cout << sum({1,3,6,6,9}, 1, 3) << endl;
  int n;
  cin >> n;
  vector<long long>v(n, 0);
  for (int i = 0; i < n; ++i) cin >> v[i];
  long long nways = nsolution(v);
  cout << nways << endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <functional>   // std::minus
#include <numeric>      // std::accumulate

using namespace std;

int sum(vector<int>v, int i, int j) {
  return accumulate(v.begin() + i, v.begin() + j + 1, 0, plus<int>());
}

int main() {
  // cout << sum({1,2,3}, 0, 2) << endl;
  // cout << sum({1,2,3}, 0, 1) << endl;
  int n;
  cin >> n;
  vector<int>v(n, 0);
  for (int i = 0; i < n; ++i) cin >> v[i];
  int nways = 0;
  int i = 0, j = n - 1;
  for (int k = i + 1; k <= j - 1; ++k) {
    for (int l = k + 1; l <= j; ++l) {
      int first = sum(v, i, k - 1);
      int middle = sum(v, k, l - 1);
      int last = sum(v, l, j);
      //cout << first << " " << middle << " " << last << endl;
      if (first == middle && middle == last) {
        nways++;
      }
    }
  }
  cout << nways << endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <functional>   // std::minus
#include <numeric>      // std::accumulate

using namespace std;

const bool brute_force = false;

int sum(vector<long long>&v, int i, int j) {
  if (brute_force) return accumulate(v.begin() + i, v.begin() + j + 1, 0, plus<long long>());
  // v is prefix.
  else {
    if (i == 0) return v[j];
    else return v[j] - v[i - 1];
  }
}

vector<long long> compute_prefix(vector<long long>&v) {
  int n = v.size();
  vector<long long>dp(n);
  dp[0] = v[0];
  for (int i = 1; i < n; ++i) {
    dp[i] = dp[i - 1] + v[i];
  }
  return dp;
}

int n2solution(vector<long long>&v) {
  vector<long long>prefix = compute_prefix(v);
  int n = v.size();
  // for (auto x: prefix) cout << x << " ";
  // cout << "\n";
  int nways = 0;
  int i = 0, j = n - 1;
  for (int k = i + 1; k <= j - 1; ++k) {
    int first = sum(brute_force ? v : prefix, i, k - 1);
    if (first > sum(prefix, k, j)) continue; // not possible solution further.
    for (int l = k + 1; l <= j; ++l) {
      int middle = sum(brute_force ? v : prefix, k, l - 1);
      int last = sum(brute_force ? v : prefix, l, j);
      //cout << "(" << k << ", " << l << ") " << first << " " << middle << " " << last << endl;
      if (first == middle && middle == last) {
        nways++;
      }
    }
  }
  return nways;
}

long long nsolution(vector<long long>&v) {
  long long nways = 0;
  long long S = 0;
  for (const auto& x: v) S += x;
  if (S % 3) return 0;
  long long a = S / 3, b = 2 * S / 3;
  long long cur = 0;
  vector<long long>prefix = compute_prefix(v);
  for (int i = 0; i < v.size() - 1; ++i) {
    long long x = prefix[i];
    if (x == b) nways += cur;
    if (x == a) cur++;
  }
  return nways;
}

int main() {
  //cout << sum({1,2,3}, 0, 2) << endl;
  // cout << sum({1,3,6,6,9}, 1, 3) << endl;
  int n;
  cin >> n;
  vector<long long>v(n, 0);
  for (int i = 0; i < n; ++i) cin >> v[i];
  long long nways = nsolution(v);
  cout << nways << endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <functional>   // std::minus
#include <numeric>      // std::accumulate

using namespace std;

const bool brute_force = false;

int sum(vector<long long>&v, int i, int j) {
  if (brute_force) return accumulate(v.begin() + i, v.begin() + j + 1, 0, plus<long long>());
  // v is prefix.
  else {
    if (i == 0) return v[j];
    else return v[j] - v[i - 1];
  }
}

vector<long long> compute_prefix(vector<long long>&v) {
  int n = v.size();
  vector<long long>dp(n);
  dp[0] = v[0];
  for (int i = 1; i < n; ++i) {
    dp[i] = dp[i - 1] + v[i];
  }
  return dp;
}

int n2solution(vector<long long>&v) {
  vector<long long>prefix = compute_prefix(v);
  int n = v.size();
  // for (auto x: prefix) cout << x << " ";
  // cout << "\n";
  int nways = 0;
  int i = 0, j = n - 1;
  for (int k = i + 1; k <= j - 1; ++k) {
    int first = sum(brute_force ? v : prefix, i, k - 1);
    if (first > sum(prefix, k, j)) continue; // not possible solution further.
    for (int l = k + 1; l <= j; ++l) {
      int middle = sum(brute_force ? v : prefix, k, l - 1);
      int last = sum(brute_force ? v : prefix, l, j);
      //cout << "(" << k << ", " << l << ") " << first << " " << middle << " " << last << endl;
      if (first == middle && middle == last) {
        nways++;
      }
    }
  }
  return nways;
}

long long nsolution(vector<long long>&v) {
  long long nways = 0;
  long long S = 0;
  for (const auto& x: v) S += x;
  if (S % 3) return 0;
  long long a = S / 3, b = 2 * S / 3;
  long long cur = 0;
  vector<long long>prefix = compute_prefix(v);
  for (int i = 0; i < v.size() - 1; ++i) {
    int x = prefix[i];
    if (x == b) nways += cur;
    if (x == a) cur++;
  }
  return nways;
}

int main() {
  //cout << sum({1,2,3}, 0, 2) << endl;
  // cout << sum({1,3,6,6,9}, 1, 3) << endl;
  int n;
  cin >> n;
  vector<int>v(n, 0);
  for (int i = 0; i < n; ++i) cin >> v[i];
  long long nways = nsolution(v);
  cout << nways << endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <functional>   // std::minus
#include <numeric>      // std::accumulate

using namespace std;

int sum(vector<int>v, int i, int j) {
  return accumulate(v.begin() + i, v.begin() + j + 1, 0, plus<int>());
}

int main() {
  // cout << sum({1,2,3}, 0, 2) << endl;
  // cout << sum({1,2,3}, 0, 1) << endl;
  int n;
  cin >> n;
  vector<int>v(n, 0);
  for (int i = 0; i < n; ++i) cin >> v[i];
  int nways = 0;
  int i = 0, j = n - 1;
  for (int k = i + 1; k < j - 1; ++k) {
    for (int l = k + 1; l <= j; ++l) {
      int first = sum(v, i, k - 1);
      int middle = sum(v, k, l - 1);
      int last = sum(v, l, j);
      cout << first << " " << middle << " " << last << endl;
      if (first == middle && middle == last) {
        nways++;
      }
    }
  }
  cout << nways << endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <functional>   // std::minus
#include <numeric>      // std::accumulate

using namespace std;

int sum(vector<int>v, int i, int j) {
  return accumulate(v.begin() + i, v.begin() + j + 1, 0, plus<int>());
}

int main() {
  // cout << sum({1,2,3}, 0, 2) << endl;
  // cout << sum({1,2,3}, 0, 1) << endl;
  int n;
  cin >> n;
  vector<int>v(n, 0);
  for (int i = 0; i < n; ++i) cin >> v[i];
  int nways = 0;
  int i = 0, j = n - 1;
  for (int k = i + 1; k < j - 1; ++k) {
    for (int l = k + 1; l <= j; ++l) {
      int first = sum(v, i, k - 1);
      int middle = sum(v, k, l - 1);
      int last = sum(v, l, j);
      //cout << first << " " << middle << " " << last << endl;
      if (first == middle && middle == last) {
        nways++;
      }
    }
  }
  cout << nways << endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <functional>   // std::minus
#include <numeric>      // std::accumulate

using namespace std;

const bool brute_force = false;

int sum(vector<int>&v, int i, int j) {
  if (brute_force) return accumulate(v.begin() + i, v.begin() + j + 1, 0, plus<int>());
  // v is prefix.
  else {
    if (i == 0) return v[j];
    else return v[j] - v[i - 1];
  }
}

vector<int> compute_prefix(vector<int>&v) {
  int n = v.size();
  vector<int>dp(n);
  dp[0] = v[0];
  for (int i = 1; i < n; ++i) {
    dp[i] = dp[i - 1] + v[i];
  }
  return dp;
}

int main() {
  //cout << sum({1,2,3}, 0, 2) << endl;
  // cout << sum({1,3,6,6,9}, 1, 3) << endl;
  int n;
  cin >> n;
  vector<int>v(n, 0);
  for (int i = 0; i < n; ++i) cin >> v[i];
  vector<int>prefix = compute_prefix(v);

  // for (auto x: prefix) cout << x << " ";
  // cout << "\n";

  int nways = 0;
  int i = 0, j = n - 1;
  for (int k = i + 1; k <= j - 1; ++k) {
    int first = sum(brute_force ? v : prefix, i, k - 1);
    if (first >= sum(prefix, k, j)) continue; // not possible solution further.
    for (int l = k + 1; l <= j; ++l) {
      int middle = sum(brute_force ? v : prefix, k, l - 1);
      int last = sum(brute_force ? v : prefix, l, j);
      //cout << "(" << k << ", " << l << ") " << first << " " << middle << " " << last << endl;
      if (first == middle && middle == last) {
        nways++;
      }
    }
  }
  cout << nways << endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <functional>   // std::minus
#include <numeric>      // std::accumulate

using namespace std;

const bool brute_force = false;

int sum(vector<int>&v, int i, int j) {
  if (brute_force) return accumulate(v.begin() + i, v.begin() + j + 1, 0, plus<int>());
  // v is prefix.
  else {
    if (i == 0) return v[j];
    else return v[j] - v[i - 1];
  }
}

vector<int> compute_prefix(vector<int>&v) {
  int n = v.size();
  vector<int>dp(n);
  dp[0] = v[0];
  for (int i = 1; i < n; ++i) {
    dp[i] = dp[i - 1] + v[i];
  }
  return dp;
}

int n2solution(vector<int>&v) {
  vector<int>prefix = compute_prefix(v);
  int n = v.size();
  // for (auto x: prefix) cout << x << " ";
  // cout << "\n";
  int nways = 0;
  int i = 0, j = n - 1;
  for (int k = i + 1; k <= j - 1; ++k) {
    int first = sum(brute_force ? v : prefix, i, k - 1);
    if (first > sum(prefix, k, j)) continue; // not possible solution further.
    for (int l = k + 1; l <= j; ++l) {
      int middle = sum(brute_force ? v : prefix, k, l - 1);
      int last = sum(brute_force ? v : prefix, l, j);
      //cout << "(" << k << ", " << l << ") " << first << " " << middle << " " << last << endl;
      if (first == middle && middle == last) {
        nways++;
      }
    }
  }
  return nways;
}

int nsolution(vector<int>&v) {
  int nways = 0;
  int S = 0;
  for (const auto& x: v) S += x;
  if (S % 3) return 0;
  int a = S / 3, b = 2 * S / 3;
  int cur = 0;
  vector<int>prefix = compute_prefix(v);
  for (const auto& x: prefix) {
    if (x == b) nways += cur;
    if (x == a) cur++;
  }
  return nways;
}

int main() {
  //cout << sum({1,2,3}, 0, 2) << endl;
  // cout << sum({1,3,6,6,9}, 1, 3) << endl;
  int n;
  cin >> n;
  vector<int>v(n, 0);
  for (int i = 0; i < n; ++i) cin >> v[i];
  int nways = nsolution(v);
  cout << nways << endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <functional>   // std::minus
#include <numeric>      // std::accumulate

using namespace std;

const bool brute_force = false;

int sum(vector<int>&v, int i, int j) {
  if (brute_force) return accumulate(v.begin() + i, v.begin() + j + 1, 0, plus<int>());
  // v is prefix.
  else {
    if (i == 0) return v[j];
    else return v[j] - v[i - 1];
  }
}

vector<int> compute_prefix(vector<int>&v) {
  int n = v.size();
  vector<int>dp(n);
  dp[0] = v[0];
  for (int i = 1; i < n; ++i) {
    dp[i] = dp[i - 1] + v[i];
  }
  return dp;
}

int main() {
  //cout << sum({1,2,3}, 0, 2) << endl;
  // cout << sum({1,3,6,6,9}, 1, 3) << endl;
  int n;
  cin >> n;
  vector<int>v(n, 0);
  for (int i = 0; i < n; ++i) cin >> v[i];
  vector<int>prefix = compute_prefix(v);

  // for (auto x: prefix) cout << x << " ";
  // cout << "\n";

  int nways = 0;
  int i = 0, j = n - 1;
  for (int k = i + 1; k <= j - 1; ++k) {
    for (int l = k + 1; l <= j; ++l) {
      int first = sum(brute_force ? v : prefix, i, k - 1);
      int middle = sum(brute_force ? v : prefix, k, l - 1);
      int last = sum(brute_force ? v : prefix, l, j);
      //cout << "(" << k << ", " << l << ") " << first << " " << middle << " " << last << endl;
      if (first == middle && middle == last) {
        nways++;
      }
    }
  }
  cout << nways << endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <functional>   // std::minus
#include <numeric>      // std::accumulate

using namespace std;

const bool brute_force = false;

int sum(vector<int>&v, int i, int j) {
  if (brute_force) return accumulate(v.begin() + i, v.begin() + j + 1, 0, plus<int>());
  // v is prefix.
  else {
    if (i == 0) return v[j];
    else return v[j] - v[i - 1];
  }
}

vector<int> compute_prefix(vector<int>&v) {
  int n = v.size();
  vector<int>dp(n);
  dp[0] = v[0];
  for (int i = 1; i < n; ++i) {
    dp[i] = dp[i - 1] + v[i];
  }
  return dp;
}

int n2solution(vector<int>&v) {
  vector<int>prefix = compute_prefix(v);
  int n = v.size();
  // for (auto x: prefix) cout << x << " ";
  // cout << "\n";
  int nways = 0;
  int i = 0, j = n - 1;
  for (int k = i + 1; k <= j - 1; ++k) {
    int first = sum(brute_force ? v : prefix, i, k - 1);
    if (first > sum(prefix, k, j)) continue; // not possible solution further.
    for (int l = k + 1; l <= j; ++l) {
      int middle = sum(brute_force ? v : prefix, k, l - 1);
      int last = sum(brute_force ? v : prefix, l, j);
      //cout << "(" << k << ", " << l << ") " << first << " " << middle << " " << last << endl;
      if (first == middle && middle == last) {
        nways++;
      }
    }
  }
  return nways;
}

long long nsolution(vector<int>&v) {
  long long nways = 0;
  int S = 0;
  for (const auto& x: v) S += x;
  if (S % 3) return 0;
  int a = S / 3, b = 2 * S / 3;
  long long cur = 0;
  vector<int>prefix = compute_prefix(v);
  for (int i = 0; i < v.size() - 1; ++i) {
    int x = prefix[i];
    if (x == b) nways += cur;
    if (x == a) cur++;
  }
  return nways;
}

int main() {
  //cout << sum({1,2,3}, 0, 2) << endl;
  // cout << sum({1,3,6,6,9}, 1, 3) << endl;
  int n;
  cin >> n;
  vector<int>v(n, 0);
  for (int i = 0; i < n; ++i) cin >> v[i];
  long long nways = nsolution(v);
  cout << nways << endl;
  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAX 1000000000

int a[100000];

int min(int x,int y){return x<y?x:y;
}

int main()
{
	int ans,t;
	int n,i,j,flag;
	while(scanf("%d%d",&n,&t)!=EOF)
	{
		int k=MAX;
		flag=ans=0;
		for(i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			k=min(k,a[i]);
		}
		for(i=1;i<=n;i++)
		{
			int t1=a[i]-k;
			if(t1%t!=0)
			{
				flag=1;
				break;
			}
			else
			{
				ans+=t1/t;
			}
		}
		if(flag==1)
		printf("-1\n");
		else
		cout<<ans;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	ll n,m;
	cin>>n>>m;
	ll a[n];
	ll i;
	for(i=0;i<=n-1;i++)
	{
		cin>>a[i];

	}
	sort(a,a+n);
	ll h=0;
	if(a[0]==a[n-1])
	{
		h=1;
	}
	ll gcd=a[0];
	for(i=1;i<=n-1;i++)
	{
		gcd=__gcd(gcd,a[i]);
	}
	if(gcd%m!=0 && h==0)
	{
		cout<<-1;
	}
	else
	{
		ll min=*min_element(a,a+n);
		ll sum=0;
		for(i=0;i<=n-1;i++)
		{
			sum=sum+(abs(a[i]-min))/m;
		}
		cout<<sum;
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	ll n,m;
	cin>>n>>m;
	ll a[n];
	ll i;
	for(i=0;i<=n-1;i++)
	{
		cin>>a[i];

	}
	sort(a,a+n);
	ll h=0;
	if(a[0]==a[n-1])
	{
		h=1;
	}
	ll gcd=a[0];
	for(i=1;i<=n-1;i++)
	{
		gcd=__gcd(gcd,a[i]);
	}
	if(gcd!=m && h==0)
	{
		cout<<-1;
	}
	else
	{
		ll min=*min_element(a,a+n);
		ll sum=0;
		for(i=0;i<=n-1;i++)
		{
			sum=sum+(abs(a[i]-min))/m;
		}
		cout<<sum;
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	ll n,m;
	cin>>n>>m;
	ll i;
	ll a[n];
	for(i=0;i<=n-1;i++)
	{
		cin>>a[i];
	}
	ll min=*min_element(a,a+n);
	ll sum=0;
	for(i=0;i<=n-1;i++)
	{
		ll x=a[i]-min;
		if(x%m==0)
		{
			sum=sum+x/m;
		}
		else
		{
			cout<<-1;
			return(0);
		}
	}
	cout<<sum;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	ll n,m;
	cin>>n>>m;
	ll a[n];
	ll i;
	for(i=0;i<=n-1;i++)
	{
		cin>>a[i];
	}
	ll gcd=a[0];
	for(i=1;i<=n-1;i++)
	{
		gcd=__gcd(gcd,a[i]);
	}

	if(gcd!=m)
	{
		cout<<-1;
	}
	else
	{
		ll min=*min_element(a,a+n);
		ll sum=0;
		for(i=0;i<=n-1;i++)
		{
			sum=sum+(abs(a[i]-min))/m;
		}
		cout<<sum;
	}
}


#include<iostream>
#include<bits/stdc++.h>
#define maxn 100010
using namespace std;
int a[maxn],n,k;
long long cnt=0,tem=0x7fffffff;
long long sum=0;
int main(){
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		tem=min(tem,a[i]);
		sum+=a[i];
	}
	int res;
	if(k==1){
		cout<<sum-(unsigned long long)n*tem;
		return 0;
	}
	for(int i=1;i<=n;i++){
		res=abs(a[i]-tem);
		if((res%k)!=0){
	    	cout<<"-1";
			return 0;
		}
		cnt+=(res/k);
	}
	cout<<cnt;
	return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#define maxn 100010
using namespace std;
int a[maxn],n,k;
long long cnt=0;
int tem=0x7fffffff;
long long sum=0;
int main(){
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		tem=min(tem,a[i]);
		sum+=a[i];
	}
	int res;
	if(k==1){
		cout<<sum-(unsigned long long)n*tem;
		return 0;
	}
	for(int i=1;i<=n;i++){
		res=abs(a[i]-tem);
		if((res%k)!=0){
	    	cout<<"-1";
			return 0;
		}
		cnt+=(res/k);
	}
	cout<<cnt;
	return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#define maxn 100010
using namespace std;
int a[maxn],n,k,cnt=0,tem=0x7fffffff;
long long sum=0;
int main(){
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		tem=min(tem,a[i]);
		sum+=a[i];
	}
	int res;
	if(k==1){
		cout<<sum-n*tem;
		return 0;
	}
	for(int i=1;i<=n;i++){
		res=abs(a[i]-tem);
		if((res%k)!=0){
	    	cout<<"-1";
			return 0;
		}
		cnt+=(res/k);
	}
	cout<<cnt;
	return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#define maxn 100010
using namespace std;
int a[maxn],n,k,cnt=0,tem=0x7fffffff;
long long sum=0;
int main(){
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		tem=min(tem,a[i]);
		sum+=a[i];
	}
	int res;
	if(k==1){
		cout<<sum-(unsigned long long)n*tem;
		return 0;
	}
	for(int i=1;i<=n;i++){
		res=abs(a[i]-tem);
		if((res%k)!=0){
	    	cout<<"-1";
			return 0;
		}
		cnt+=(res/k);
	}
	cout<<cnt;
	return 0;
}


//	Chront 24-4-17
//	A man of noble birth went to a distant country to have himself appointed king and then to return.

#include <bits/stdc++.h>
#define INF 1000000001

int main(){
	long long int n, k;
	scanf("%lld %lld",&n,&k);
	long long int req=0;
	long long int arr[n];
	long long int min=INF;
	for(int i=0; i<n; i++){
		scanf("%lld",&arr[i]);
		if(arr[i]<min)	min=arr[i];
	}
	for(int i=0; i<n; i++){
		arr[i]-=min;
		if(arr[i]%k!=0){
			printf("-1");
			return 0;
		}
		req+=arr[i]/k;
	}
	printf("%lld",req);
	return 0;
}


#include <iostream>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include<stdio.h>
#include<windows.h>
using namespace std;

int main()
{

    long long x1,x2,x3,x4=0;
	x3=2000000000;
	long a1[100001];
    cin>>x1>>x2;

    for(int i=1; i<=x1;i++)
	{
		cin>>a1[i];
		if (a1[i]<x3) x3=a1[i];
	}

    for(int i=1; i<=x1;i++)
	{
		if ((a1[i] - x3) % x2 != 0)
		{
			cout << -1;
			return 0;
		}

		x4 = x4 + (a1[i] - x3) / x2;
	}

cout << x4;

    getch();

    return 0;
}


#include <iostream>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include<stdio.h>
#include<windows.h>
using namespace std;

int main()
{

    long long x1,x2,x3,x4=0;
	x3=2000000000;
	long a1[100001];
    cin>>x1>>x2;

    for(int i=1; i<=x1;i++)
	{
		cin>>a1[i];
		if (a1[i]<x3) x3=a1[i];
	}

    for(int i=1; i<=x1;i++)
	{
		if ((a1[i] - x3) % x2 != 0)
		{
			cout << -1;
			return 0;
		}

		x4 = x4 + (a1[i] - x3) / x2;
	}

cout << x4;

   // getch();

    return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
int n,k;
ll a[maxn];
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        for(int i=0; i<n; i++)
            scanf("%lld",&a[i]);
        sort(a,a+n);
        ll sum=0;
        int flag=0;
        for(int i=0; i<n; i++)
        {
            if((a[i]-a[0])%k)
                flag=1;
            sum+=(a[i]-a[0])/k;
        }
        if(flag)
            printf("-1\n");
        else
            printf("%lld\n",sum);
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
int n,k;
ll a[maxn];
ll gcd(ll x,ll y)
{
    return (y==0)?x:gcd(y,x%y);
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        scanf("%lld",&a[0]);
        ll g=a[0];
        for(int i=1;i<n;i++)
        {
            scanf("%lld",&a[i]);
            g=gcd(g,a[i]);
        }
        sort(a,a+n);
        //printf("%lld\n",g);
        if((g==1&&k!=1)||(g%k!=0&&n!=1))
        {
            printf("-1\n");
            continue;
        }
        ll sum=0;
        for(int i=1;i<n;i++)
        {
            sum+=(a[i]-a[0])/k;
        }
        printf("%lld\n",sum);
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
int n,k;
ll a[maxn];
ll gcd(ll x,ll y)
{
    return (y==0)?x:gcd(y,x%y);
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        scanf("%lld",&a[0]);
        ll g=a[0];
        for(int i=1; i<n; i++)
        {
            scanf("%lld",&a[i]);
            g=gcd(g,a[i]);
        }
        sort(a,a+n);
        //printf("%lld\n",gcd(g,k));
        if(g!=k)
        {
            if((gcd(g,k)==1&&n!=1))
            {
                printf("-1\n");
                continue;
            }
        }
        ll sum=0;
        for(int i=1; i<n; i++)
        {
            sum+=(a[i]-a[0])/k;
        }
        printf("%lld\n",sum);
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
int n,k;
ll a[maxn];
ll gcd(ll x,ll y)
{
    return (y==0)?x:gcd(y,x%y);
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        scanf("%lld",&a[0]);
        ll g=a[0];
        for(int i=1;i<n;i++)
        {
            scanf("%lld",&a[i]);
            g=gcd(g,a[i]);
        }
        sort(a,a+n);
        //printf("%lld\n",g);
        if((g==1&&k!=1)||g%k!=0)
        {
            printf("-1\n");
            continue;
        }
        ll sum=0;
        for(int i=1;i<n;i++)
        {
            sum+=(a[i]-a[0])/k;
        }
        printf("%lld\n",sum);
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define PI 3.14159265358979323846

// cout <<
// cin  >>

//*************************************************//

int main(){
	long long int n, k, i, less=1e9, aux, sum=0;
	vector<long long int> prices;
	scanf(" %I64d %I64d", &n, &k);
	for(i=0;i<n;i++){
		scanf(" %I64d", &aux);
		less = min(less, aux);
		prices.push_back(aux);
	};
	for(i=0;i<n;i++){
		if((prices[i]-less)%k!=0){
			printf("-1\n");
			return 0;
		}
		else {
			sum += (prices[i]-less)/k;
		};
	};
	printf("%I64d\n", sum);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define PI 3.14159265358979323846

// cout <<
// cin  >>

//*************************************************//

int main(){
	int n, k, i, less=1e9, aux;
	long long int sum=0;
	vector<int> prices;
	scanf(" %d %d", &n, &k);
	for(i=0;i<n;i++){
		scanf(" %d", &aux);
		less = min(less, aux);
		prices.push_back(aux);
	};
	for(i=0;i<n;i++){
		if((prices[i]-less)%k){
			printf("-1\n");
			return 0;
		}
		else {
			sum += (prices[i]-less)/k;
		};
	};
	printf("%Ld\n", sum);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define PI 3.14159265358979323846

// cout <<
// cin  >>

//*************************************************//

int main(){
	long long int n, k, i, less=1e9, aux;
	vector<long long int> prices;
	scanf(" %I64d %I64d", &n, &k);
	for(i=0;i<n;i++){
		scanf(" %I64d", &aux);
		less = min(less, aux);
		prices.push_back(aux);
	};
	for(i=0;i<n;i++){
		if((prices[i]-less)%k!=0){
			printf("-1\n");
			return 0;
		}
		else {
			sum += (prices[i]-less)/k;
		};
	};
	printf("%I64d\n", sum);
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
  int n,k;
  cin>>n>>k;
  ll b[n],v=0;
  for(int g=0;g<n;g++)
  cin>>b[g];
  sort(b,b+a);
  for(int g=1;g<n;g++)
  {
      ll x=b[g]-b[0];
      if(x%k>0)
      {
          cout<<-1;
          return 0;
      }
      v+=x/k;
  }
 cout<<v;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
  int n,k;
  cin>>n>>k;
  ll b[n],v=0;
  for(int g=0;g<n;g++)
  cin>>b[g];
  sort(b,b+a)
  for(int g=1;g<n;g++)
  {
      ll x=b[g]-b[0];
      if(x%k>0)
      {
          cout<<-1;
          return 0;
      }
      v+=x/k;
  }
 cout<<v;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
  int n,k;
  cin>>n>>k;
  ll b[n],v=0;
  for(int g=0;g<n;g++)
  cin>>b[g];
  sort(b,b+n);
  for(int g=1;g<n;g++)
  {
      ll x=b[g]-b[0];
      if(x%k>0)
      {
          cout<<-1;
          return 0;
      }
      v+=x/k;
  }
 cout<<v;
}


#include"bits/stdc++.h"
using namespace std;
int main()
{
    long long n,k,c=0,i,f=0;
    cin>>n>>k;
    long long a[n];
    for(i=0; i<n; i++)
        cin>>a[i];
    sort(a,a+n);
    for(i=1; i<n; i++)
    {
        if((a[i]-a[0])%k)
        {
          f=1;
            break;
        }
        else
            c+=a[i]-a[0];
    }
    if(f)
        cout<<"-1"<<endl;
    else
    cout<<c/k<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,k,r=1,s=0,x;
    cin>>n>>k;
    double p=0,t=0;
    long long int a[n];
    for(long long int i=0; i<n; i++)

    {
        cin>>a[i];
        p+=a[i];
    }
    t=p/k;

    for(long long int i=0; i<n-1; i++)
    {

        if(a[i]-a[i+1]==k)
        {

            s=t-a[i+1];
           break;
        }
if(s==0)
    s=min(s,a[i+1]-a[i]);
        else r++;

    }
    x=t-k;
 //sort(a,a+n);
    if(r==n)
        for(long long int i=1; i<n; i++)
        {

            s+=a[i]-a[0];
            if(s<0)
                s=0;

        }

    cout<<min(s,x)<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,k,r=1,s=0,x,f=1;
    cin>>n>>k;
    double p=0,t=0;
    long long int a[n];
    for(long long int i=0; i<n; i++)

    {
        cin>>a[i];
        p+=a[i];
    }
    t=p/k;

    for(long long int i=0; i<n-1; i++)
    {

        if(a[i]-a[i+1]==k)
        {

            s=t-a[i+1];

            break;
        }

        else r++;

    }
    x=t-k;
//
//sort(a,a+n);
    if(r==n)
        for(long long int i=1; i<n; i++)
        {

            s+=a[i]-a[0];
            if(s<0)
               {
                 s=0;
                 f++;
               }

        }
        if(f==k&&s==0) s=-1;
    cout<<min(s,x)<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,k,r=1,s=0,x;
    cin>>n>>k;
    double p=0,t=0;
    long long int a[n];
    for(long long int i=0; i<n; i++)

    {
        cin>>a[i];
        p+=a[i];
    }
    t=p/k;

    for(long long int i=0; i<n-1; i++)
    {

        if(a[i]-a[i+1]==k)
        {

            s=t-a[i+1];

            break;
        }

        else r++;

    }
    x=t-k;
//sort(a,a+n);
    if(r==n)
        for(long long int i=1; i<n; i++)
        {

            s+=a[i]-a[0];
            if(s<0)
               s=0;}

 for(long long int i=0; i<n-1; i++)
    if(s==0)
        s=min(s,a[i+1]-a[i]);

    cout<<min(s,x)<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,k,s=0,p=0;
    cin>>n>>k;
    long long int a[n];
    for(long long int i=0;i<n;i++)
        cin>>a[i];
        p=a[0];
      for(long long int i=1;i<n;i++)
      {
          s+=a[i]-a[0];

      }
      cout<<s<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,k,s=0,p=0;
    cin>>n>>k;
    long long int a[n];
    for(long long int i=0; i<n; i++)
        cin>>a[i];
    p=a[0];
    for(long long int i=1; i<n; i++)
    {
        s+=a[i]-a[0];
        p=s;
        if(s<0)
            s=0;

    }
    cout<<min(s,p)<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,k,r=1,s=0,x;
    cin>>n>>k;
    double p=0,t=0;
    long long int a[n];
    for(long long int i=0; i<n; i++)

    {
        cin>>a[i];
        p+=a[i];
    }
    t=p/k;

    for(long long int i=0; i<n-1; i++)
    {

        if(a[i]-a[i+1]==k)
        {

            s=t-a[i+1];
           break;
        }

        else r++;

    }
    x=t-k;
 //sort(a,a+n);
    if(r==n)
        for(long long int i=1; i<n; i++)
        {

            s+=a[i]-a[0];
            if(s<0)
                s=0;

        }

if(r==n&&s==0)
    s=-1;
    cout<<min(s,x)<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,k,r=1,y=0;
    cin>>n>>k;
    double p=0,t=0,x,s=0;
    long long int a[n];
    for(long long int i=0; i<n; i++)

    {
        cin>>a[i];
        p+=a[i];
    }
    t=p/k;

    for(long long int i=0; i<n-1; i++)
    {

        if(a[i+1]-a[i]==k)
        {
            y++;
            s=t-a[i];
            break;
        }
        if(y==0)s=-1;
        else r++;

    }
    x=t-k;
    // sort(a,a+n);
    if(r==n)
        for(long long int i=1; i<n; i++)
        {

            s+=a[i]-a[0];
            if(s<0)
                s=0;

        }

    cout<<min(s,x)<<endl;
}


//   Author:Arturia
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<ctime>
#include<cstdlib>
#include<string>
#include<queue>
#include<cmath>
#include<set>
#include<map>
#include<bitset>
#include<vector>
#define rep(x,a,b) for (int x=int(a);x<=(int)(b);x++)
#define drp(x,a,b) for (int x=int(a);x>=(int)(b);x--)
#define cross(x,a) for (int x=hd[a];~x;x=nx[x])
#define ll long long
#define inf (1<<30)
#define pii pair<int,int>
#define pdd pair<double,double>
#define mk(a,b) make_pair(a,b)
#define fs first
#define sc second
#define pb push_back
#define VI vector<int>
#define VS vector<string>
using namespace std;
inline ll rd(){
	ll x=0;int ch=getchar(),f=1;
	while (!isdigit(ch)&&(ch!='-')&&(ch!=EOF)) ch=getchar();
	if (ch=='-'){f=-1;ch=getchar();}
	while (isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}
inline void rt(ll x){
	if (x<0) putchar('-'),x=-x;
	if (x>=10) rt(x/10),putchar(x%10+'0');
		else putchar(x+'0');
}
const int maxn=100005;
int n,k,mn=inf,a[maxn];
ll ans;
int main(){
	n=rd(),k=rd();
	rep(i,1,n) a[i]=rd(),mn=min(mn,a[i]);
	rep(i,1,n) if ((a[i]-mn)%k) return puts("-1"),0;else ans+=(a[i]-mn)/k;
	rt(ans),putchar('\n');
}


//   Author:Arturia
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<ctime>
#include<cstdlib>
#include<string>
#include<queue>
#include<cmath>
#include<set>
#include<map>
#include<bitset>
#include<vector>
#define rep(x,a,b) for (int x=int(a);x<=(int)(b);x++)
#define drp(x,a,b) for (int x=int(a);x>=(int)(b);x--)
#define cross(x,a) for (int x=hd[a];~x;x=nx[x])
#define ll long long
#define inf (1<<29)
#define pii pair<int,int>
#define pdd pair<double,double>
#define mk(a,b) make_pair(a,b)
#define fs first
#define sc second
#define pb push_back
#define VI vector<int>
#define VS vector<string>
using namespace std;
inline ll rd(){
	ll x=0;int ch=getchar(),f=1;
	while (!isdigit(ch)&&(ch!='-')&&(ch!=EOF)) ch=getchar();
	if (ch=='-'){f=-1;ch=getchar();}
	while (isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}
inline void rt(ll x){
	if (x<0) putchar('-'),x=-x;
	if (x>=10) rt(x/10),putchar(x%10+'0');
		else putchar(x+'0');
}
const int maxn=100005;
int n,k,mn=inf,a[maxn];
ll ans;
int main(){
	n=rd(),k=rd();
	rep(i,1,n) a[i]=rd(),mn=min(mn,a[i]);
	rep(i,1,n) if ((a[i]-mn)%k) return puts("-1"),0;else ans+=(a[i]-mn)/k;
	rt(ans),putchar('\n');
}


#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <limits.h>
#include <string.h>
#include <math.h>

long long min(long long a, long long b){
    if (a < b) return a;
    return b;
}

int nod(int a, int b)
{
    while(a*b)
        a >= b ? a %= b : b %= a;
    return a + b;
}

int main(){
    int N, K;
    scanf("%i %i",&N,&K);
    int *mas = new int[N];
    int min = 1000000005;
    for (int i = 0; i < N; i++){
        scanf("%i",&mas[i]);
        if (mas[i] < min) min = mas[i];
    }

    long long sum = 0;
    for (int i = 0; i < N; i++){
        if (nod(mas[i], min) % K != 0){ printf("-1"); return 0;}
        sum += (mas[i] - min) / K;
    }
    printf("%I64d",sum);
}