import java.util.*; import java.io.*; class Main { public static void main(String[] args) { var sc = new ContestScanner(); int n = sc.nextInt(); long k = sc.nextLong(); var ka = new long[16]; ka[0] = 1; for (int i = 1; i < 16; i++) ka[i] = i * ka[i - 1]; var a = new int[n]; int cn = 0; for (int i = 1; i <= 9; i++) { int nc = sc.nextInt(); for (int j = 0; j < nc; j++) a[cn++] = i; } if (n < 7) { var r = new HashSet(); for (var i : new Permutation(n)) { long s = 0; for (int j : i) { s *= 10; s += a[j]; } if (s % k == 0) r.add(s); } System.out.println(r.size()); return; } int nn = n >> 1, mm = n - nn; int ma = 1 << n; long m10 = mp(10, mm, k); var aa = new HashMap(); var bb = new HashMap(); var xx = new HashSet(); var aaa = new int[nn]; var bbb = new int[mm]; long ans = 0; var tt = new int[9]; for (int bi = (1 << nn) - 1; bi < ma; bi = n(bi)) { aa.clear(); bb.clear(); int ii = bi, cc = 0, ccc = 0; var ss = new StringBuilder(); for (int i = 0; i < n; i++) { if ((ii & 1) == 1) { aaa[cc] = a[i]; ss.append(String.valueOf(aaa[cc])); cc++; } else { bbb[ccc] = a[i]; ccc++; } ii >>= 1; } var rr = ss.toString(); if (xx.contains(rr)) continue; xx.add(rr); for (var i : new Permutation(nn)) { long s = 0; for (int j : i) { s *= 10; s += aaa[j]; } s *= m10; s %= k; if (aa.containsKey(s)) aa.put(s, aa.get(s) + 1L); else aa.put(s, 1L); } for (var i : new Permutation(mm)) { long s = 0; for (int j : i) { s *= 10; s += bbb[j]; } s = -s; s %= k; if (s < 0) s += k; if (bb.containsKey(s)) bb.put(s, bb.get(s) + 1L); else bb.put(s, 1L); } var r = new A(); aa.forEach((i, j) -> { if (bb.containsKey(i)) r.a(j * bb.get(i)); }); long tr = r.a; Arrays.fill(tt, 0); for (int i : aaa) tt[i - 1]++; for (int i : tt) tr /= ka[i]; Arrays.fill(tt, 0); for (int i : bbb) tt[i - 1]++; for (int i : tt) tr /= ka[i]; ans += tr; } System.out.println(ans); } private static long mp(long a, long b, long m) { if (b == 0) return 1; if ((b & 1) == 1) return (a * mp(a, b - 1, m)) % m; return mp((a * a) % m, b >> 1, m); } private static int n(int s) { int x = s & -s, y = s + x; return (((s & ~y) / x) >> 1) | y; } } class A { long a = 0L; public void a(long x) { a += x; } public void d(long x) { a /= x; } public long g() { return a; } @Override public String toString() { return String.valueOf(a); } } class Permutation implements java.util.Iterator, Iterable { private int[] next; public Permutation(int n) { next = java.util.stream.IntStream.range(0, n).toArray(); } @Override public boolean hasNext() { return next != null; } @Override public int[] next() { int[] r = next.clone(); next = nextPermutation(next); return r; } @Override public java.util.Iterator iterator() { return this; } public static int[] nextPermutation(int[] a) { if (a == null || a.length < 2) return null; int p = 0; for (int i = a.length - 2; i >= 0; i--) { if (a[i] >= a[i + 1]) continue; p = i; break; } int q = 0; for (int i = a.length - 1; i > p; i--) { if (a[i] <= a[p]) continue; q = i; break; } if (p == 0 && q == 0) return null; int temp = a[p]; a[p] = a[q]; a[q] = temp; int l = p, r = a.length; while (++l < --r) { temp = a[l]; a[l] = a[r]; a[r] = temp; } return a; } } class ContestScanner { private final InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; public ContestScanner(InputStream in) { this.in = in; } public ContestScanner() { this(System.in); } private boolean hasNextByte() { if (ptr < buflen) return true; ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } return buflen > 0; } private int readByte() { return hasNextByte() ? buffer[ptr++] : -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public void nextThrow(int n) { for (int i = 0; i < n; i++) this.next(); } public void nextThrow() { this.nextThrow(1); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) throw new NumberFormatException(); while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) return minus ? -n : n; else throw new NumberFormatException(); b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public boolean[] nextBoolean(char True) { String s = this.next(); int n = s.length(); boolean[] array = new boolean[n]; for (int i = 0; i < n; i++) array[i] = s.charAt(i) == True; return array; } public long[] nextLongArray(int length) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public long[] nextLongArray(int length, long[] a) { long[] array = new long[length + a.length]; for (int i = 0; i < length; i++) array[i] = this.nextLong(); for (int i = length; i < array.length; i++) array[i] = a[i - length]; return array; } public int[] nextIntArray(int length) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = this.nextInt(); return array; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public int[] nextIntArray(int length, int[] array) { int n = length + array.length; int[] a = new int[n]; for (int i = 0; i < length; i++) a[i] = this.nextInt(); for (int i = length; i < n; i++) a[i] = array[i - length]; return a; } public Integer[] nextIntegerArray(int length, java.util.function.IntUnaryOperator map) { Integer[] array = new Integer[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public Integer[] nextIntegerArray(int length) { Integer[] array = new Integer[length]; for (int i = 0; i < length; i++) array[i] = this.nextInt(); return array; } public double[] nextDoubleArray(int length) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public String[] nextArray(int length) { String[] array = new String[length]; for (int i = 0; i < length; i++) array[i] = this.next(); return array; } public long[][] nextLongMatrix(int height, int width) { long[][] mat = new long[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) mat[h][w] = this.nextLong(); return mat; } public int[][] nextIntMatrix(int height, int width) { int[][] mat = new int[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) mat[h][w] = this.nextInt(); return mat; } public int[][] nextIntMatrix(int height, int width, java.util.function.IntUnaryOperator map) { int[][] mat = new int[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) mat[h][w] = map.applyAsInt(this.nextInt()); return mat; } public double[][] nextDoubleMatrix(int height, int width) { double[][] mat = new double[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) mat[h][w] = this.nextDouble(); return mat; } public boolean[][] nextBooleanMatrix(int height, int width, char True) { boolean[][] mat = new boolean[height][width]; for (int h = 0; h < height; h++) { String s = this.next(); for (int w = 0; w < width; w++) mat[h][w] = s.charAt(w) == True; } return mat; } public char[][] nextCharMatrix(int height, int width) { char[][] mat = new char[height][width]; for (int h = 0; h < height; h++) { String s = this.next(); for (int w = 0; w < width; w++) mat[h][w] = s.charAt(w); } return mat; } public char[][] nextCharMatrix(int height, int width, int h, int w, char c) { char[][] mat = new char[height + 2 * h][width + 2 * w]; for (int i = 0; i < height; i++) { String s = this.next(); for (int j = 0; j < width; j++) mat[i + h][j + w] = s.charAt(j); } for (int i = 0; i < h; i++) for (int j = 0; j < 2 * w + width; j++) mat[i][j] = c; for (int i = h + height; i < 2 * h + height; i++) for (int j = 0; j < 2 * w + width; j++) mat[i][j] = c; for (int i = h; i < h + height; i++) { for (int j = 0; j < w; j++) mat[i][j] = c; for (int j = w + width; j < 2 * w + width; j++) mat[i][j] = c; } return mat; } public boolean[][] nextBooleanMatrix(int height, int width, int h, int w, char c) { boolean[][] mat = new boolean[height + 2 * h][width + 2 * w]; for (int i = 0; i < height; i++) { String s = this.next(); for (int j = 0; j < width; j++) mat[i + h][j + w] = s.charAt(j) == c; } return mat; } }