I'm not sure I fully understand how XMR guarantees fungibility of its token, but I think a big part of it is the construction of ring signatures to prove transactions are zero sum, with a pool of participating signers that all have statistically valid plausible deniability. Start here maybe https://web.getmonero.org/resources/moneropedia/clsag.html?