How do PID control loop in a servo motor reverse motor direction?
You misunderstand the implementation of a PID controller.



The PID controller wants to make its Error \$e(t)\$ equal to 0.
Your implementation determines which way the actuator goes.

Take setpoint 40 while actual 0 degree. The Error is 40.
Take kP to be 5, then U will be 40*5 = 200.
Your process, the motor controller, then outputs 200, in positive direction.

But setpoint -40 while actual 0 degree. The Error is -40.
When kP is 5, U will be -40*5 = -200.
Your motor controller then outputs -200, in negative direction.

If you invert this, the PID will output 200, but error will increase. Creating a loop until something clips or breaks.