n, p, q = map(int, input().split()) a = list(map(int, input().split())) a.sort() ans = 0 for i in range(n): for j in range(i+1, n): for h in range(j+1, n): for k in range(h+1, n): tmp = pow(10, a[i], p) + pow(9, a[j], p) + pow(7, a[h], p) + pow(5, a[k], p) tmp %= p if tmp == q: ans += 1 print(ans)