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