P2524 Uim's Valentine's Gift · Part II

Description

Prequel: see Luogu P2525. Uim successfully delivered gifts to $N$ girls in some order and maintained their harmony. Now Uim wants to know the lexicographic rank of the final order he chose among all gift-giving orders for $N$ girls. The order can be viewed as a permutation of $1,2,\cdots,N$.

Input Format

The first line contains an integer $N$. The second line contains an integer $X$, representing the given permutation.

Output Format

Output a single integer, indicating its position in lexicographic order.

Explanation/Hint

$1\le N \le 9$. Please note that the permutation in the input has no spaces. Translated by ChatGPT 5