Discovery and inversion of the viscoelastic wave equation in inhomogeneous media