#include using namespace std; #include using namespace atcoder; using ll = long long; #define rep(i, n) for(int i = 0; i < n; i++) int main() { int N, p, q; cin >> N >> p >> q; vector a(N); rep(i, N) cin >> a[i]; int ans = 0; rep(i, N) { for(int j = i + 1; j < N; j++) { for(int k = j + 1; k < N; k++) { for(int l = k + 1; l < N; l++) { vector v; v.push_back(a[i]); v.push_back(a[j]); v.push_back(a[k]); v.push_back(a[l]); sort(v.begin(), v.end()); ll s = pow_mod(10, v[0], p) + pow_mod(9, v[1], p) + pow_mod(7, v[2], p) + pow_mod(5, v[3], p); if(s % p == q) { ans++; } } } } } cout << ans << endl; }