The SU(r) Vafa-Witten partition function, which virtually counts Higgs pairs on a projective surface S, was mathematically defined by Tanaka-Thomas. On the Langlands dual side, the first-named author recently introduced virtual counts of Higgs pairs on µ r -gerbes. In this paper, we instead use Yoshioka's moduli spaces of twisted sheaves. Using Chern character twisted by rational B-field, we give a new mathematical definition of the SU(r)/Z r Vafa-Witten partition function when r is prime. Our definition uses the period-index theorem of de Jong.S-duality, a concept from physics, predicts that the SU(r) and SU(r)/Z r partitions functions are related by a modular transformation. We turn this into a mathematical conjecture, which we prove for all K3 surfaces and prime numbers r.c 1 +rγ (q). SU(r)/Z r partition function. In order to mathematically understand the S-duality transformation (1), we need to define the right-hand side. In the physics literature [VW, LL], one can find the following formula (4) Z SU(r)/Zr c 1 (q) := w∈H 2 (S,µr) e 2πi r (w•c 1 ) Z w (q), 2 The equivariant parameter drops out because of symmetry.