a(n) = { if(n <= 2, return(11-n)); if(n%2 == 1, return(9*10^(n-1)); ); my(res = 0, nover2 = n/2, nover2p1 = n/2 + 1, nover2m1 = n/2 - 1); forvec(x = vector(n-1, i, [0, 9]), if(x[nover2m1] == x[nover2], res-=qperms(x); res+=qperms(concat(0,x)); ) , 1 ); forvec(x = vector(n, i, [1, 9]), if(x[nover2] == x[nover2p1], res+=qperms(x); ); , 1 ); res } addhelp(qperms, "number of permutations elements in vector v.") qperms(v) = {my(r=1,t); for(i=1,#v-1,if(v[i]==v[i+1],t++,r*=binomial(i,t+1);t=0));r*=binomial(#v,t+1)} /* from qperms I removed a v = vecsort(v) since input comes in sorted from function a */