Modular anomaly equation, heat kernel and S-duality in $ \mathcal{N}=2 $ theories