This function returns the required number of ways where idx is the current index and diff is number of boxes having different color from box on its left
int solve ( int idx , int diff , int N , int M , int K ) {