How can I calculate the total number of possible anagrams for a set of letters? How can I calculate the total number of possible anagrams for a set of letters?

For example:

"Math" : 24 possible combinations.

math
maht
mtah
mtha
mhat
mhta
amth
amht
atmh
athm
ahmt
ahtm
tmah
tmha
tamh
tahm
thma
tham
hmat
hmta
hamt
hatm
htma
htam
Total: 24


I generated this by actually generating each combination, one by one, but I want to know the formula for calculating the total possible number of combinations of any group of letters.
Suppose you have word with $n_1$ letters $a_1$, $n_2$ letters $a_2$, $\cdots$, $n_k$ letters $a_k$.

First give each letter unique pair: you have first letter  $a_1$, it will be $(a_1,1)$, second $a_1$ will be $(a_1,2)$ ,$\cdots$ ,$n_1$th letter $a_1$ will be $(a_1,n_1)$, first letter $a_2$ will be $(a_2,1)$ etc...

Now you have $(n_1+n_2+\cdots+n_k)!$ possible combinations of pairs.

Next it's easy to get letters combinations from pair combination: note that for each $s$ there is $s!$ combinations of pair $(a_s,i)$, but only one combinations of letters, so finally you have:

$$\frac{(n_1+n_2+\cdots+n_k)!}{n_1!n_2!\cdots n_k!}$$

Possible combinations of letters.
$n!$, that is factorial of $n$, where $n$ is the number of digits.

If $n=4$, then $4! = 1\times 2\times 3\times 4$

$n! = 1\times 2\times 3\dots\times n$