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)): for b in range(c): for a in range(b): ans += D[(Q-pow(7,A[c],P)-pow(9,A[b],P)-pow(10,A[a],P))%P] D[pow(5,A[c],P)]+=1 print(ans)