code: def permutation_rank(p):
      
      n = len(p)
      fact = 1                                 # compute (n-1) factorial
      for i in range(2, n):
          fact *= i
      r = 0                                    # compute rank of p
      digits = list(range(n))                  # all yet unused digits
      for i in range(n-1):                     # for all digits except last one
          q = digits.index(p[i])
          r += fact * q
          del digits[q]                        # remove this digit p[i]
          fact //= (n - 1 - i)                 # weight of next digit
      return r
docs: Given a permutation of {0,..,n-1} find its rank according to lexicographical order
  
         :param p: list of length n containing all integers from 0 to n-1
         :returns: rank between 0 and n! -1
         :beware: computation with big numbers
         :complexity: `O(n^2)`