//#include #include using namespace std; //using namespace atcoder; using ll = long long; #define all(A) A.begin(),A.end() using vll = vector; #define rep(i, n) for (long long i = 0; i < (long long)(n); i++) using Graph = vector>; vector> BOX; //max C size 通りの全列挙 void next_combination(int size,int max, vector now_vector) { int now_size = now_vector.size(); if (now_size == size) { BOX.push_back(now_vector); } else { vector next = now_vector; if (now_size == 0) { for (int i = 0; i < max; i++) { next.push_back(i); next_combination(size, max, next); next.pop_back(); } } else { int LAST = next[now_size - 1]; for (int i = LAST + 1; i < max; i++) { next.push_back(i); next_combination(size, max, next); next.pop_back(); } } } return; } int main() { int N, K; scanf("%d %d", &N, &K); vector C(9); vector D; rep(i, 9) { cin >> C[i]; rep(j, C[i])D.push_back(i + 1); } int n=N/2; next_combination(n, N, {}); ll an = 0; set Poo; vll kkk(N,1); rep(i,N-1){ kkk[i+1]=kkk[i]*10; kkk[i+1]%=K; } rep(i, BOX.size()) { string AU; set S; rep(k, n) { S.insert(BOX[i][k]); AU.push_back(D[BOX[i][k]]); } if (Poo.count(AU))continue; Poo.insert(AU); string BU; rep(k, N) { if (!S.count(k))BU.push_back(D[k]); } ll x = 0; unordered_map M1; do { x=0; rep(u, n) { x += (AU[u]) * kkk[u]; } x %= K; M1[x]++; } while (next_permutation(all(AU))); do { x = 0; rep(u, N-n) { x += (BU[u]) * kkk[u+N/2]; } x%=K; if (x == 0)x += K; an += M1[K - x]; } while (next_permutation(all(BU))); } cout << an << endl; }