Solution (source code)

= Solution

Assume the fixed margins are compatible, so the finite state space is nonempty. At state $X$, draw an index table $Y$ from the stipulated uniform proposal law. If $X+Y$ has any negative entry, reject and stay at $X$; otherwise accept and set the next state to $X+Y$. The margins stay fixed because the proposal has zero row and column sums.

The proposal set is invariant under $Y\mapsto-Y$. Hence proposals between two feasible distinct states are symmetric. The target density is constant, so the Metropolis–Hastings acceptance probability for a feasible proposal is one. The explicit rejection of infeasible proposals is essential: fixed margins by themselves do not guarantee nonnegative entries.