N, P, Q = map(int, input().split()) A = list(map(int, input().split())) A.sort() ans = 0 for i in range(N - 3): for j in range(i + 1, N - 2): for k in range(j + 1, N - 1): for l in range(k + 1, N): a = pow(10, A[i]) + pow(9, A[j]) + pow(7, A[k]) + pow(5, A[l]) if a % P == Q: ans += 1 print(ans)