** How to specify that, for example, that mathematics would never be the last lesson, or would be last only once a week
**

You can set, for example, for mathematics subject, that lessons 8 and 9 are with a question mark, and then set that “Max. on question marked” is “1”. Then program will put math lessons on the 8th or 9th lesson only once a week.

Another method:

In this case 3 mathematics lessons per week will be until 5-th period. If there are 4 lessons per week for 7A, 7B and 7C classes, only one math lesson will be later that 5-th lesson.