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

int countOfSubarray(vector<int>& a , int x , int y , int z , int w , int d){
	int n = a.size();
	map<tuple<int,int,int,int>,int>freq;
	int ans = 0;
	freq[{0,0,0,0}] = 1;
	int cx = 0 , cy = 0 , cz=0 , cw = 0, cd = 0;
	for(int i=0;i<n;i++){
		if(a[i]==x)cx++;
		if(a[i]==y)cy++;
		if(a[i]==z)cz++;
		if(a[i]==w)cw++;
		if(a[i]==d)cd++;
		
		int diff1 = cx-cy;
		int diff2 = cy-cz;
		int diff3 = cz-cw;
		int diff4 = cw-cd;
		ans += freq[{diff1,diff2,diff3,diff4}];
		freq[{diff1,diff2,diff3,diff4}]++;
	}
	return ans;
}

int main() {
	int n;
	cin>>n;
	int x,y,z,w,d;
	cin>>x>>y>>z>>w>>d;
	vector<int>a(n);
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	cout<<countOfSubarray(a,x,y,z,w,d);
	return 0;
}
