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

void executeTime() {
    cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " secs";
}

int main() {

    int n, x, y, w, b, z;
    cin >> n >> x >> y >> w >> b >> z;

    vector<int> v(n);
    for (auto &x : v) {
        cin >> x;
    }


    map<array<int, 4>, int> freq;

    freq[ {0, 0, 0, 0}] = 1;

    int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0;
    int ans = 0;

    for (int j = 0; j < n; j++) {
        if (v[j] == x) c1++;
        else if (v[j] == y) c2++;
        else if (v[j] == z) c3++;
        else if (v[j] == b) c4++;
        else c5++;

        int d1 = c1 - c2, d2 = c3 - c4, d3 = c4 - c5, d4 = c1 - c5;

        ans += freq[ {d1, d2, d3, d4}];
        freq[ {d1, d2, d3, d4}]++;
    }

    cout << ans << endl;

    executeTime();
    return 0;
}