Only 2 test cases passed.. What's the issue?

many problem in your code

  1. don’t sort the array other order will change and you get wrong ans

  2. t should be consider at the end after calculating ans
    just return this
    return ((ans%mod)*time)%mod;

  3. your canPaint() has also some issues

check this code i have explained each step here

Modified Code

Yupp thanks I removed the sort function and it workedd

now can i mark this doubt as resolved??