N, P, Q = map(int, input().split()) A = list(map(int, input().split())) power10 = [pow(10, a, P) for a in A] power9 = [pow(9, a, P) for a in A] power7 = [pow(7, a, P) for a in A] power5 = [pow(5, a, P) for a in A] count = 0 for i in range(N-3): for j in range(i+1, N-2): for k in range(j+1, N-1): for l in range(k+1, N): if (power10[i] + power9[j] + power7[k] + power5[l]) % P == Q: count += 1 print(count)