#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define all(a) (a).begin(),(a).end()

ll modpow(ll a, ll n, ll m) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % m;
        a = a * a % m;
        n >>= 1;
    }
    return res;
}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    
    int N;
    ll P, Q;
    cin >> N >> P >> Q;
    vector<ll> t(N);
    rep(i, 0, N) cin >> t[i];
    sort(all(t));
    vector<ll> A(N), B(N), C(N), D(N);
    rep(i, 0, N) {
        A[i] = modpow(10, t[i], P);
        B[i] = modpow(9, t[i], P);
        C[i] = modpow(7, t[i], P);
        D[i] = modpow(5, t[i], P);
    }
    
    ll ans = 0;
    rep(a, 0, N - 3) rep(b, a + 1, N - 2) rep(c, b + 1, N - 1) rep(d, c + 1, N) {
        if ((A[a] + B[b] + C[c] + D[d]) % P == Q) ++ans;
    }
    
    cout << ans << '\n';
}