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

int main() {
	int n;
	cin >> n;
	vector<int> array(n, 0);
	for (int i = 0; i < n; i++)
		cin >> array[i];
	long long answer = 0;
	map<vector<int>, long long> mp;
	mp[{0, 0, 0, 0}] = 1;
	int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0;
	for (int j = 0; j < n; j++) {
		if (array[j] == 1)
			c1++;
		else if (array[j] == 2)
			c2++;
		else if (array[j] == 3)
			c3++;
		else if (array[j] == 4)
			c4++;
		else if (array[j] == 5)
			c5++;

		vector<int> diff = {c2 - c1, c3 - c2, c4 - c3, c5 - c4};
		if(mp.find(diff)!=mp.end())
			answer += mp[diff];
		mp[diff]++;
	}
	cout << answer;
	return 0;
}