I am trying to find out a closed-form formula (or a generating function at least) for the number of permutations $\sigma$ that satisfy $$ S = \sum_{i = 1}^{n} i\sigma(i)$$ for a given value of $S$. We can find a maximum and minimum possible sum for a particular $n$ and I have observed that the intermediate sums (between max and min) will always exist for $n>3$. The frequency of the occurrence of each sum is approximately a bell-shaped curve although it is not unimodal.

I have tried various approaches to solve this problem including using a partitions formulation but I have not been able to derive much from it. Any clue as to how to proceed with this problem? Has a similar problem been solved elsewhere?