If I have interpreted the question correctly then the method is as follows.
First permute the two specific elements at the ends in $2!$ ways and the rest in $(n-2)!$ways . Now we need to subtract this case from all the permutations that can be done using all $n$ elements which is $n! $
Hence the answer to the question can be given as $$n! -(n-2)!2!=(n-2)!(n-2)(n+1)$$