for a particular selection for rows , students in each row can be arranged among themselves in 8! ways.
we can divide the 16 students in each class into two rows in (16 c 8) ways as there are 8 chairs in 1 row.
Once such a division is made for a row, note that set of students to be seated in the other row pertaining to this class is fixed.
so the number of ways 16 students of a class can sit is (16 c 8)(8!)(8!). (call this number p)
as each way of arrangement of students of one class can be combined with each way of arrangement of students of the other class, we get the total number of arrangements =
no of ways we can arrange two nonadjacent rows for each class * p^2
= (8)* p^2