n, p, q = map(int, input().split()) A = list(map(int, input().split())) A.sort() R = [0 for _ in range(p)] for i in range(3, n): R[pow(5, A[i], p)] += 1 ans = 0 for i in range(n): res1 = pow(10, A[i], p) for j in range(i + 1, n): res2 = (res1 + pow(9, A[j], p)) % p for k in range(j + 1, n): res3 = (res2 + pow(7, A[k], p)) % p r = (q - res3) % p ans += R[r] if k + 1 < n: R[pow(5, A[k + 1], p)] -= 1 for k in range(j + 3, n): R[pow(5, A[k], p)] += 1 for k in range(i + 4, n): R[pow(5, A[k], p)] += 1 print(ans)