Generalization of the Change of Variables Formula with Applications to Residual Flows