/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class key {
    int a, b, c, d;

    public key(int a, int b, int c, int d) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

    @Override
    public int hashCode() {
        return Objects.hash(a, b, c, d);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        key k = (key) obj;
        return a == k.a && b == k.b && c == k.c && d == k.d;
    }
}
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int d = sc.nextInt();
        int e = sc.nextInt();

        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        HashMap<key, Integer> mp = new HashMap<>();
        mp.put(new key(0, 0, 0, 0), 1);

        int ax = 0, bx = 0, cx = 0, dx = 0, ex = 0;
        long count = 0;

        for (int i = 0; i < n; i++) {
            if (arr[i] == a)
                ax++;
            else if (arr[i] == b)
                bx++;
            else if (arr[i] == c)
                cx++;
            else if (arr[i] == d)
                dx++;
            else if (arr[i] == e)
                ex++;

            int d1 = bx - ax;
            int d2 = cx - bx;
            int d3 = dx - cx;
            int d4 = ex - dx;

            key k = new key(d1, d2, d3, d4);

            count += mp.getOrDefault(k, 0);
            mp.put(k, mp.getOrDefault(k, 0) + 1);
        }

        System.out.println(count);
        sc.close();
	}
}