public class Euler172 {
    static int LENGTH = 18, MAX_REP = 3;
    static long[] fact;
    static int[] counts = new int[10];
    static long total = 0;

    static void dfs(int digit, int remaining, long denom) {
        if (digit == 10) {
            if (remaining != 0)
                return;
            long all = fact[LENGTH] / denom;
            long lz = 0;
            if (counts[0] > 0) {
                long d2 = (denom / fact[counts[0]]) * fact[counts[0] - 1];
                lz = fact[LENGTH - 1] / d2;
            }
            total += all - lz;
            return;
        }
        int mx = Math.min(MAX_REP, remaining);
        for (int c = 0; c <= mx; c++) {
            counts[digit] = c;
            dfs(digit + 1, remaining - c, denom * fact[c]);
        }
    }

    public static void main(String[] args) {
        fact = new long[LENGTH + 1];
        fact[0] = 1;
        for (int i = 1; i <= LENGTH; i++)
            fact[i] = fact[i - 1] * i;
        dfs(0, LENGTH, 1);
        System.out.println(total);
    }
}
