from itertools import combinations N, P, Q = map(int, input().split()) A = list(map(int, input().split())) M = max(A)+1 pow_10, pow_9, pow_7, pow_5 = [1]*M, [1]*M, [1]*M, [1]*M for i in range(M-1): pow_10[i+1] = pow_10[i]*10 % P pow_9[i+1] = pow_9[i]*9 % P pow_7[i+1] = pow_7[i]*7 % P pow_5[i+1] = pow_5[i]*5 % P ans = 0 for a, b, c, d in combinations(A, 4): if (pow_10[a] + pow_9[b] + pow_7[c] + pow_5[d]) % P == Q: ans += 1 print(ans)