faster n2multiidx and multiidx2n