import collections N,P,Q = map(int, input().split()) A = list(map(int, input().split())) A.sort() ans = 0 D = collections.defaultdict(int) for c in reversed(range(N)): cc = pow(7,A[c],P) for b in range(c): bb = pow(9,A[b],P) for a in range(b): ans += D[(Q-cc-bb-pow(10,A[a],P))%P] D[pow(5,A[c],P)]+=1 print(ans)