If a defendant has served any portion of the defendant's sentence under a commitment based upon a judgment that is subsequently declared invalid or that is modified during the term of imprisonment, the time served must be credited against any subsequent sentence received upon a new commitment for the same criminal act or acts.
§ 46-18-402, MCA