from itertools import combinations def count_ways(N, P, Q, A): powers = [[pow(x, A[i], P) for x in [10, 9, 7, 5]] for i in range(N)] ways = 0 for a, b, c, d in combinations(range(N), 4): if (powers[a][0] + powers[b][1] + powers[c][2] + powers[d][3]) % 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))