from itertools import combinations def count_ways(N, P, Q, A): ways = 0 for a, b, c, d in combinations(range(N), 4): if (10**A[a] + 9**A[b] + 7**A[c] + 5**A[d]) % P == Q: ways += 1 return ways N, P, Q = map(int, input().split()) A = list(map(int, input().split())) print(count_ways(N, P, Q, A))