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
function solve ( $ idx , $ diff , $ N , $ M , $ K ) { global $ dp , $ MOD ;