Editorial
Let . The value must appear inside the interval, and the other interval values must be chosen from through .
There are choices for these values, orders inside the interval, and orders outside it. Thus the answer is
Precomputing factorials and inverse factorials gives total time complexity .
Solution written by GPT5