how many permutations of {1,2,...,9}

For $\{1,2,3,\dots,n\}$, the numbers are tabulated here, along with lots of information, formulas, references, links, and whatnot. For $n=9$, it says 148329.


Call a permutation of your form "good".

Consider the general problem, with $n$ elements. Let $A_n$ be the number of good permutations (so you are after $A_9$).

The last number, $n$, is special as it can precede anything. In particular, there are two ways to make a good permutation.

I. Take a good permutation on $n-1$ letters and place the $n$ in any slot (except the one following ${n-1}$). This gives $(n-1)A_{n-1}$ good permutations.

II. Take a permutation on $n-1$ letters that has a single bad pair and insert the $n$ in the middle of the bad pair. There are $n-2$ bad pairs amongst the $n-1$ letters and there are $A_{n-2}$ ways to arrange the $n-1$ letters so that the arrangement contains the specified bad pair and no other bad pairs. This gives $(n-2)A_{n-2}$ good permutations on the $n$ letters.

Thus we have the regression $$A_n=(n-1)A_{n-1}+(n-2)A_{n-2}$$ Easy to see that $A_1=1=A_2$ so the first few terms of our regression are $$\{1,1,3,11,53,309,2119,16687,148329\}$$ Of course, this matches the sequence identified by @GerryMyerson in another posted solution.