n, p, q = map(int, input().split()) count = 0 A = tuple(map(int, input().split())) for i in A: a = pow(10, i, p) for j in A: if j >= i: break b = pow(9, j, p) for k in A: if k >= j: break c = pow(7, j, p) for l in A: if l >= k: break d = pow(5, l, p) if (a + b + c + d) % p == q: count += 1 print(count)