import sys input = sys.stdin.readline sys.setrecursionlimit(10**8) N, P, Q = map(int, input().split()) A = list(map(int, input().split())) A.sort() ans = 0 modP = [[0] * P] for i in range(N): modP.append([]) for p in range(P): modP[-1].append(modP[-2][p] + (1 if (A[i] % P) == p else 0)) for i in range(N - 3): for j in range(i + 1, N - 2): for k in range(j + 1, N - 1): a, b, c = A[i], A[j], A[k] cnt = (pow(10, a, P) + pow(9, b, P) + pow(7, c, P)) % P tmp = (Q - cnt) % P ans += modP[-1][tmp] - modP[k][tmp] print(ans)