Note we need to count permutations such that
We formulate a dp solution. Assume we are iterating over positions and while iterating, we can keep positions empty or fill them with some non-larger value and we can put current value in already iterated positions or leave them unassigned for now. Thus we can define to mean number of prefixes with positions unfilled (and thus numbers unassigned) and confirmed sum. Then when at position , five things can happen
We can put at position
We can put at an earlier unfilled position and leave position unfilled
We can put at an earlier unfilled position and put some earlier unassigned value at position
We can leave unassigned and position unfilled
We can leave unassigned and put an earlier unassigned value at position B
By considering this cases we can come up with transitions. Note contribution to sum are confirmed in case 1 or when a position is left unfilled for future or when a value is left unassigned for future.