N, P, Q = map(int, input().split()) C = [0] * P A = list(map(int, input().split())) A.sort() ans = 0 for c in range(N - 1, -1, -1): for b in range(c - 1, -1, -1): for a in range(b - 1, -1, -1): now = pow(10, A[a], P) + pow(9, A[b], P) + pow(7, A[c], P) now %= P ans += C[(Q-now) % P] C[pow(5, A[c], P)] += 1 print(ans)