a(n) = {my(walker = 0, nf = n!, res = 0); maxIndex = n; while(walker < nf, maxIndex = n; perm = numtoperm(n, walker); if(isperm(perm), res++; ); walker += (n - maxIndex)! ); res } isperm(v) = {my(tst=vector(#v), qseen = vector(#tst+1)); for(i=1, #v, tst[i]=(i+v[i]*(-1)^v[i])%(#v+1); if(qseen[tst[i] + 1] == 0, qseen[tst[i] + 1] = 1; , maxIndex = i; return(0); ) ); s=Set(tst); if(#s==#v && s[1] == 0 && haswalk(v), return(1) ); 0 } haswalk(v) = {v=concat([0],Vec(v)); my(i=(#v+1)\2+1,t=1); while(v[i]!=0, i=(i+(v[i]*(-1)^v[i]))%(#v); i=(i-1)%#v+1; t++ ); t==#v }