If three of the history books are together
They can occupy positions 123 and be arranged in 3! ways.....the other history book can occupy positions 5, 6 or7
And the other three math books can be arranged in 3! ways
So 3! * 3 * 3! = 108 ways
They can occupy positions 234 and be arranged in 3! ways.....the oher history book can occupy positions 6 or 7
And the three math books can be arranged in 3! ways
So 3! * 2 * 3! = 72 ways
And occupying positions 345 or 456 also produce 2 ( 72) arrangements =144
And occupying 567 also produces another 108 arrangements
So if three of the history books are together we have 2(108) +3(72) = 432 arrangements
If all 4 of the history books are together they can occupy positions
1 - 4
2 - 5
3 - 6
4 - 7
For each of these, they can be arranged in 4! ways and the other 3 math books can be arranged in 3! ways
So....if all are together, they can be arranged in 4 * 4! * 3! = 3456 ways
So....the total number of arrangements = 432 + 3456 = 3888 total arrangements