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