Let be the expectation when the current run has length and Mog acts next, and define similarly. With and ,
The term with is replaced by zero.
Process from down to . Every expectation is an affine expression in and . At , the two expressions form a linear system. Solving it gives the answer in time and extra space.