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

vector<long long> v = {10LL, 9LL, 7LL, 5LL};
int siz = 2000010;

long long modpow(long long a, long long b, long long mod) {
    long long cur = a, ans = 1;
    for (int i = 0; i < 60; i++) {
        if (b & (1LL << i)) {
            ans = (ans * cur) % mod;
        }
        cur = (cur * cur) % mod;
    }
    return ans;
}

int main() {
    int n;
    long long p, q;
    cin >> n >> p >> q;
    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<vector<long long>> mods(4, vector<long long>(siz, 1LL));
    for (int i = 0; i < 4; i++) {
        for (int j = 1; j < siz; j++) {
            mods[i][j] = mods[i][j - 1] * v[i] % p;
        }
    }
    vector<char> c;
    for (int i = 0; i < 4; i++) {
        c.push_back('o');
    }
    for (int i = 0; i < n - 4; i++) {
        c.push_back('x');
    }
    sort(a.begin(), a.end());
    int ans = 0;
    do {
        long long res = 0LL;
        int w = -1, x = -1, y = -1, z = -1;
        for (int i = 0; i < n; i++) {
            if (c[i] == 'o') {
                if (w == -1) {
                    w = i;
                } else if (x == -1) {
                    x = i;
                } else if (y == -1) {
                    y = i;
                } else {
                    z = i;
                }
            }
        }
        res += (((mods[0][a[w]] + mods[1][a[x]]) % p + mods[2][a[y]]) % p + mods[3][a[z]]) % p;
        if (res == q) {
            ans++;
        }
    } while (next_permutation(c.begin(), c.end()));
    cout << ans << endl;
}