Probability of getting a certain number of heads in coin toss problem with differently biased coins with different numbers There are $K$ bags of coins, each bag contains $m_k$ ($k=1,\dots,K$) coins, and thus we have $M:=\sum_{k=1}^K m_k$ coins in total.
Coins in the $k$-th bag satisfies $P(\text{H})=p_k$, $P(\text{T})=1-p_k$ (here, H and T stands for head and tail).
The question is, what is the probability of getting $N$ heads when we toss these $M$ coins?

Collection of my thoughts:
We have $k=1,\dots,K$ of bags, and there are in total $\frac{(K+N)!}{K!N!}$ ways of getting $N$ heads from different bags.
For $k$-th bag, suppose we have $n_k$ heads. Then, there are $\frac{(K-1+N-n_k)!}{(K-1)!(N-n_k)!}$ ways of getting $N-n_k$ from other bags.
For $k$-th bag, the probability of getting $n_k$ heads is${m_k \choose n_k} p_k^{n_k}(1-p_k)^{m_k-n_k}$.
etc., but I am not convinced or I cannot get it right.
 A: Your thoughts are along the right lines, particularly this:

For the $k$-th bag, the probability of getting $n_k$ heads is ${m_k \choose n_k} p_k^{n_k}(1-p_k)^{m_k-n_k}$.

The only remaining thing is: what can the values of $n_k$ be, so that the total number of heads is $N$? Well, the only constraint is that $n_1 + n_2 + \dots + n_K$ (which is the total number of heads) must be equal to $N$. If you fix any such tuple $(n_1, \dots, n_K)$, then the probability from each bag is ${m_k \choose n_k} p_k^{n_k}(1-p_k)^{m_k-n_k}$, and the total probability is their product as the bags are independent. In other words, the answer is:
$$\sum_{\substack{(n_1, \dots, n_K) \\ n_1 + \dots + n_K = N}} \prod_{k=1}^K {m_k \choose n_k} p_k^{n_k}(1-p_k)^{m_k-n_k}$$

There is another way to express this: the probability generating function for a particular coin in the $k$th bag turning heads is $(p_kz + 1-p_k)$. As all coins are independent, the probability-generating function for all your coins is the product of this expression over all $M$ coins, namely
$$\prod_{k=1}^{K} (p_kz + 1-p_k)^{m_k}$$
and the probability you want is the coefficient of $z^N$ in the above.
