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