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