import sys from functools import lru_cache def main(): sys.setrecursionlimit(1 << 25) N, K = map(int, sys.stdin.readline().split()) c = list(map(int, sys.stdin.readline().split())) # Precompute 10^i mod K for i in 0..N-1 e = [1] * N for i in range(1, N): e[i] = (e[i-1] * 10) % K # We'll represent the counts as a tuple initial_counts = tuple(c) @lru_cache(maxsize=None) def dp(pos, rem, counts): if pos == N: return 1 if rem == 0 else 0 total = 0 for d in range(9): if counts[d] == 0: continue new_counts = list(counts) new_counts[d] -= 1 new_counts = tuple(new_counts) contribution = (d + 1) * e[pos] # since d starts from 0 to 8, digits are 1-9 new_rem = (rem + contribution) % K total += dp(pos + 1, new_rem, new_counts) return total result = dp(0, 0, initial_counts) print(result) if __name__ == '__main__': main()