private int sum(int[] digits, int len, int rem, int max) {
        
        if (rem == 0) {
            
            long f = FACTORIAL[LENGTH];
            int n = 0;
            
            for (int i = 0; i < digits.length; ++i) {
                n += digits[i] * i;
                f /= FACTORIAL[digits[i]];
            }
            
            return (int)((111111111 * (((f * n) / LENGTH) % LIMIT)) % LIMIT);
        }
        
        if (len-- == 0) return 0;
        
        int sum = 0;
        
        for (int i = max; i > 0; --i) {
            
            final int diff = rem - SQUARE[i];
            
            if (diff < 0) continue;
            if (len * SQUARE[i] < diff) break;
            --digits[0];
            ++digits[i];
            if ((sum += sum(digits, len, diff, i)) >= LIMIT) sum -= LIMIT;
            --digits[i];
            ++digits[0];
        }
        
        return sum;
    }