It may have bended possibly due to overwatering, so it has softened. And depending too on the time of the year it happens, plants tend to follow the sun and that also influences direction of growth. I do not know of the watering frequency your plant had, or if its media is porous enough to avoid staying too damp than needed. But then it got a chance to dry up and get more sun. It may not go totally right side up again, but it will hopefully slowly acclimate and stop leaning any further.
Oftentimes, I would stake the cacti to help it keep upright and do quarter turns to even out light distribution, and be cautious of watering.
But I leave my cacti outdoors, so they are at Nature's mercy.
One batch of cacti that I have, I gave up trying to make it upright, so it now looks like a leaning windswept cacti. But it managed to make a new growth along its body, and that kept upright.
Used to be upright like this:
Have been leaning/bending:
I took some cuttings before as back up, so far the cuttings have grown better, kept upright and kept its natural green colors:
This one is leaning/bending since it is too slender and tall, so I just used the foot of one of the tables, tied it there to help stake it, so far it is holding up well: