public static long numberByPermutation(int[] p) {
        int n = p.length;
        long[] fact = new long[n];
        fact[0] = 1;
        for (int i = 1; i < n; i++) {
            fact[i] = i * fact[i - 1];
        }
        long res = 0;
        for (int i = 0; i < n; i++) {
            int a = p[i];
            for (int j = 0; j < i; j++) {
                if (p[j] < p[i]) {
                    --a;
                }
            }
            res += a * fact[n - 1 - i];
        }
        return res;
    }