import sys from functools import lru_cache def main(): sys.setrecursionlimit(1 << 25) n, k = map(int, sys.stdin.readline().split()) counts = list(map(int, sys.stdin.readline().split())) # Precompute the digits that are available and their counts digits = [(i+1, counts[i]) for i in range(9) if counts[i] > 0] # Convert counts into a tuple for easier handling initial_counts = tuple(counts) @lru_cache(maxsize=None) def dp(current_counts, rem): # Check if all counts are zero (base case) if all(c == 0 for c in current_counts): return 1 if rem == 0 else 0 total = 0 for i in range(9): if current_counts[i] == 0: continue # Create new counts by decrementing the ith digit new_counts = list(current_counts) new_counts[i] -= 1 new_counts_tuple = tuple(new_counts) # Compute the new remainder new_rem = (rem * 10 + (i + 1)) % k total += dp(new_counts_tuple, new_rem) return total result = dp(initial_counts, 0) print(result) if __name__ == "__main__": main()