Loop in Stabilize Mode Causes FC to Lose Orientation and Inverted Flight?

While flying my Ritewing Z2.5 in stabilize mode I attempted a loop. The autopilot took over at 90 degrees pitch up and completely overrode my inputs. The plane performed a radical maneuver to rite itself, then proceeded to enter a hard left turn to a steep nose down pitch. I switched to RTL and the plane ended up hitting the ground at full throttle while inverted at a shallow angle. I had plenty of altitude for the plane to recover after switching to RTL. Is it possible the flight controller lost orientation and began flying inverted. The plane contacted ground at a shallow angle like it was trying to climb out while inverted. My flight controller is a matek f405.

a log of that flight would be really useful to tell what happened.