from itertools import combinations def count_ways(N, P, Q, A): ways = 0 powers = [10**a % P for a in A] for a, b, c, d in combinations(range(N), 4): if (powers[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))