from collections import defaultdict def main(): import sys input = sys.stdin.read().split() idx = 0 N = int(input[idx]) idx += 1 K = int(input[idx]) idx += 1 c = list(map(int, input[idx:idx+9])) idx += 9 original_counts = tuple(c) dp = defaultdict(int) initial_remainder = 0 dp[(initial_remainder, original_counts)] = 1 for _ in range(N): new_dp = defaultdict(int) for (rem, counts), cnt in dp.items(): for d in range(1, 10): if counts[d-1] == 0: continue new_counts_list = list(counts) new_counts_list[d-1] -= 1 new_counts = tuple(new_counts_list) new_rem = (rem * 10 + d) % K new_dp[(new_rem, new_counts)] += cnt dp = new_dp target_counts = tuple([0]*9) answer = dp.get((0, target_counts), 0) print(answer) if __name__ == "__main__": main()