#K58127. Lexicographic Rank of Permutations

    ID: 30574 Type: Default 1000ms 256MiB

Lexicographic Rank of Permutations

Lexicographic Rank of Permutations

Given a string S, compute its lexicographic rank among all its permutations sorted in alphabetical order. The rank of the smallest permutation is defined as 1. The computation uses the formula:

\[ \text{Rank}(S) = 1 + \sum_{i=0}^{n-1} \left( \text{Count}(i) \times \frac{n!}{(n-i)!} \right) \]

where n is the length of the string, and \( \text{Count}(i) \) is the number of characters to the right of the i-th character that are smaller than S[i]. The factorial function is defined as:

\[ n! = 1 \times 2 \times \cdots \times n \]

This problem also supports strings containing digits or other characters, using standard lexicographical order.

inputFormat

The input consists of a single line containing the string S. The string may include alphabets, digits, or other characters.

outputFormat

Output a single integer that represents the lexicographic rank of S among all its permutations in ascending order.

## sample
abc
1

</p>