Suppose I'm computing the partition function of some system
\begin{equation} \sum_{x_1,...,\ x_N}e^{-H(x_1,...,\ x_N)} \end{equation}
Here the sum is over every possible value of each variable $x_i$. I want to make a change of variables $\{x_1,...,\ x_N \}\rightarrow \{y_1,...,\ y_M \}$ where the $y_i$ are functions of the previous variables and $N$ and $M$ can be different. In general, how do I know if I have to add a degeneracy factor $g(y_1,...,\ y_M)$
\begin{equation} \sum_{x_1,...,\ x_N}e^{-H(x_1,...,\ x_N)}= \sum_{y_1,...,\ y_M}g(y_1,...,\ y_M)e^{- H(y_1,...,\ y_M)} \end{equation}
Is there a formal way to know if $\sum_{x_1,...,\ x_N}=\sum_{y_1,...,\ y_M}$ or if I have to add the degeneracy $g$?
My guess is that if the change of variables is inversible then you don't need the degeneracy $g$ but I wouldn't know how to justify this either.