You have N objects, some are identical. Label them (1,1) through (N,m) where the first component is just the kth object and the second component is the jth class. |j| = size of the jth class.
There are clearly N! ways to order these N objects. The problem is that some the same so some of these orderings will be identical and so we have to remove them.
Say you have Red_(1,1) Blue_(2,2) Green_(3,3) Red_(4,1)
This arrangement is the same as Red_(4,1) Blue_(2,2) Green_(3,3) Red_(1,1)
If we ignore the class info then we have Red_1 Blue_2 Green_3 Red_4 and Red_4 Blue_2 Green_3 Red_1 and these are different than the fully distinguishable case. Hence we "double counted".
If we ignore the distinguishing indexing then we Red_1 Blue_2 Green_3 Red_1 or just Red Blue Green Red.
Now if we index them we actually have $N = \sum a_i$ total distinguishable objects and so N! possible combinations. But now we have to remove "extras" due to collapsing to classes. In the above example, Red_(1,1) and Red_(4,1) are different and but they are also the same. How are they the same? We can swap them. There are 2! ways to swap. So we must remove 2!. If we had 3 reds(a_1 = 3) then we could form 3! different distinct strings or remove 3! to collapse them.
The way I think about this removable is that we collapse down all the same class objects to one object but that object exists in "multiple places" due to the original arrangement order. E.g., what I showed above. E.g., say we have RBGRRGRBBRRG but we also have RBGRRGRBBRRG, RBGRRGRBBRRG, RBGRRGRBBRRG, RBGRRGRBBRRG, RBGRRGRBBRRG, RBGRRGRBBRRG, RBGRRGRBBRRG, RBGRRGRBBRRG, etc. Yes, these are all the same! NO THEY ARE NOT ALL THE SAME! YES THEY ARE! If you just think of "how many ways can I fill the R's with my a_R marbles, well, there are a_R! ways. Same goes for each of the other colors. But when we combine them we have to multiply to get all the possible combinations. If you are just dealing with R's then you can ignore the B and G's for now so that RBGRRGRBBRRG -> RRRRRR. So we have 6 R's that we can fill in with our 6 red marbles. If we ordered our marbles then that would be 6! ways to arrange them. RBGRRGRBBRRG -> BBB and so we can arrange our 3 blue marbles 3! ways and RBGRRGRBBRRG -> GGG also has 3!. Combined though this means we have 6!*3!*3! distinguishing combinations(and to go the opposite way we have to remove these).
This is an easier problem than if we are given, say, a pattern like RGRRGB and have 10 reds, 34 greens, and 8 blue marbles and asked how many distinguished combinations can we form. Well, only slightly more difficult. We have 3 spots for our red marbles and have 10 so we have 10C3, then 24C2 and 8C1. Then multiply together to get all combinations. But that was just for RGRRGB, we then might have RRGGBR and all other arrangements for those sequences(which is a sort of separate problem).
So you end up with $$\frac{(\sum a_i)!}{\prod a_i!}$$
Note that if they are all distinguishable then you just have $N!$. Imagine you have 2 classes of x and y elements. This means you have a total of $(x+y)!$ ways to order them if you treat the elements as distinct. Once you have those combinations you have over counted by x! and y! ways(for a total of x!*y!).