import sys from functools import lru_cache def main(): N, K = map(int, sys.stdin.readline().split()) c = list(map(int, sys.stdin.readline().split())) # Precompute the exponents for each position exponents = [] for j in range(N): exponents.append(pow(10, N-1 - j, K)) # Convert counts to a tuple for memoization counts = tuple(c) @lru_cache(maxsize=None) def backtrack(pos, current_mod, cnt): if pos == N: return 1 if current_mod == 0 else 0 res = 0 for d in range(9): if cnt[d] == 0: continue # Calculate the new counts after using this digit new_cnt = list(cnt) new_cnt[d] -= 1 new_cnt = tuple(new_cnt) # Calculate the new mod value contribution = (d + 1) * exponents[pos] new_mod = (current_mod + contribution) % K res += backtrack(pos + 1, new_mod, new_cnt) return res total = backtrack(0, 0, counts) print(total) if __name__ == "__main__": main()