from functools import lru_cache N, P, Q = map(int, input().split()) A = sorted(map(int, input().split())) li = [[None]*(A[-1]+1) for _ in range(11)] def power(x, y): if li[x][y] == None: li[x][y] = pow(x, y, P) return li[x][y] ans = 0 for i in range(N): a = A[i] for j in range(i+1, N): b = A[j] for k in range(j+1, N): c = A[k] for l in range(k+1, N): d = A[l] if (power(10, a) + power(9, b) + power(7, c) + power(5, d)) % P == Q: ans += 1 print(ans)