Analytic solution of chemical master equations involving gene switching. I: Representation theory and diagrammatic approach to exact solution