We conjecture a structure formula for the SUprq Vafa-Witten partition function for surfaces with holomorphic 2-form. The conjecture is based on S-duality and a structure formula for the vertical contribution previously derived by the third-named author using Gholampour-Thomas's theory of virtual degeneracy loci.For ranks r " 2, 3, conjectural expressions for the partition function in terms of the theta functions of A r´1 , A _ r´1 and Seiberg-Witten invariants were known. We conjecture new expressions for r " 4, 5 in terms of the theta functions of A r´1 , A _ r´1 , Seiberg-Witten invariants, and continued fractions studied by Ramanujan. The vertical part of our conjectures is proved for low virtual dimensions by calculations on nested Hilbert schemes.The horizontal part of our conjectures give predictions for virtual Euler characteristics of Gieseker-Maruyama moduli spaces of stable sheaves. In this case, our formulae are sums of universal functions with coefficients in Galois extensions of Q. The universal functions, corresponding to different quantum vacua, are permuted under the action of the Galois group.For r " 6, 7 we also find relations with Hauptmoduln of Γ 0 prq. We present K-theoretic refinements for r " 2, 3, 4 involving weak Jacobi forms. c vd pT vir M q " p´1q vd e vir pMq.Keeping S, H, r, c 1 fixed and varying c 2 (still assuming there are no strictly H-semistable objects), we define 3 the SUprq Vafa-Witten partition function by H,c 1 pqq " r ´1Z prq S,H,c 1 pqq `r´1 Z p1 r q S,H,c 1 pqq.3 The factor in front of the sum is required for modularity later.