sum of all the numbers that may be formed with permutations of n given digits

n distinct digits are given (from 0 to 9), find sum of all n digit numbers that can be formed using given digits . assuming that numbers formed with leading 0 are also allowed.

example
Input: 3 2 1
Output: 1332
Explanation
Numbers Formed: 123 , 132 , 312 , 213, 231 , 321
123 + 132 + 312 + 213 + 231 + 321 = 1332=ans

note that

Total numbers that can be formed using n digits is total number of permutations of n digits, viz factorial(n). Now, since the number formed is a n-digit number, each digit will appear factorial(n)/n times at each position from least significant digit to most significant digit. Therefore, sum of digits at a position = (sum of all the digits) * (factorial(n)/n).

example
Considering the example digits as 1 2 3

factorial(3)/3 = 2;

Sum of digits at least significant digit = (1 + 2 + 3) * 2 = 12

Similarly sum of digits at tens position, hundreds place is 12.
(This sum will contribute as 12 * 100)

Similarly sum of digits at tens position , thousands place position is 12.
( this sum will be 12 * 1000)

sum of all numbers = 12 + (10 * 12) + (100 * 12) = 1332;

upvote if it helped