n, p, q = list(map(int, input().split())) a = list(map(int, input().split())) a.sort() cnt = 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): s = 10 ** a[i] + 9 ** a[j] + 7 ** a[k] + 5 ** a[l] if s % p == q: cnt += 1 print(cnt)