[reference to question] (http://codeforces.com/contest/1391/problem/C)

Consider any permutation p_i, i = 1 to n. Construct an un-directed Graph with edges of the form (i, p[i]). Note that you may construct self-loops also or ignore them. If there is any cycle of length greater than 1(excluding self loops) then we say there exists a cycle in permutation. You may also refer to this: https://en.wikipedia.org/wiki/Cyclic_permutation

Now second question : Cyclic permutation of length βnβ. Note that I am assuming that permutation is build using elements 1 to βnβ. In that case you need to arrange all elements in a circle. Number of ways of doing so is (n-1)!.

The question you are referring is a bit different since the graph is built in a different way. In this case, we find number of permutations such that there is no cycle. Also observe that permutation should first increase and then decrease so that there is no cycle(try to prove that there exists a cycle if this is not the case).

Now we need to place n at some position. Letβs Place it at i, on the left of n there are (i-1) positions left to be occupied which can be filled out of any (n-1) elements, Therefore we have the sum:

Just subtract this from n!.

thanks for your help