from itertools import combinations N, P, Q = map(int, input().split()) A = list(map(int, input().split())) A.sort() ans = 0 for a, b, c, d in combinations(A, 4): x = pow(10, a, P) x += pow(9, b, P) x += pow(7, c, P) x += pow(5, d, P) if x % P == Q: ans += 1 print(ans)