We want to arrange on a shelf $k$ mathematics books (distinct), $m$ physics books, and $n$ chemistry books. In how many ways can this arrangement be done:

1. if the books must be grouped by subjects
2. if only the mathematics books must be grouped.
1. First choose the order of the subjects (3! choices), then for each subject the order of the books ($k!m!n!$ choices). This makes a total of: $3!k!m!n!$ ways to arrange the books.
2. We can first only consider the physics and chemistry books. There are $(n+m)$ of them. Therefore, there are $(n+m)!$ ways to order them. There are then $(n+m+1)$ positions to insert the group of mathematics books (all the way to the left, all the way to the right, and between two consecutive books). Finally, we need to choose an order for the mathematics books ($k!$ choices). Conclusion, there are $(n+m+1)!k!$ ways to arrange the books.