code: private int rankPermutationLexicographically(int[] permutation) {
          int rank = 0;
          int n = permutation.length;
          int[] counter = new int[n + 1];
          for (int i = 1; i < permutation.length; i++) {
              counter[i] = permutation[i - 1] + 1;
          }
          for (int j = 1; j <= n; j++) {
              rank += (counter[j] - 1) * factorial(n - j);
              for (int i = j + 1; i <= n; i++) {
                  if (counter[i] > counter[j]) {
                      counter[i]--;
                  }
              }
          }
          return rank + 1;
      }
docs: Convert a permutation (in the form of an int array) into a 'rank' - which
  is just a single number that is the order of the permutation in a lexico-
  graphically ordered list.
  
  @param permutation the permutation to use
  @return the rank as a number