import sys from collections import defaultdict def main(): N, K = map(int, sys.stdin.readline().split()) c = list(map(int, sys.stdin.readline().split())) counts = tuple(c) # Precompute the weights for each position p weight = [] for p in range(N): exponent = N - 1 - p w = pow(10, exponent, K) weight.append(w) # Initialize DP current_dp = defaultdict(int) current_dp[(counts, 0)] = 1 for p in range(N): next_dp = defaultdict(int) for (current_counts, mod), ways in current_dp.items(): current_counts_list = list(current_counts) for d in range(1, 10): if current_counts_list[d - 1] == 0: continue # Create new counts new_counts = list(current_counts_list) new_counts[d - 1] -= 1 new_counts_tuple = tuple(new_counts) # Compute new mod contribution = d * weight[p] new_mod = (mod + contribution) % K # Update next_dp key = (new_counts_tuple, new_mod) next_dp[key] += ways current_dp = next_dp # The final state is all counts zero and mod 0 final_counts = (0, 0, 0, 0, 0, 0, 0, 0, 0) answer = current_dp.get((final_counts, 0), 0) print(answer) if __name__ == "__main__": main()