How to solve ICL1904

How to solve this Combinatorics Problem from international coding league?

Link to problem: https://www.codechef.com/ICL2019/problems/ICL1904/