DC automatic polarity inverter
If the train power supply is a relatively-constant 12 Vdc supply, simply feed the two 12V terminals into a bridge rectifier.  The output of the bridge will be the same polarity no matter what the input polarity is.

If you are concerned about the voltage drop in the bridge, create your own bridge rectifier using 4- Schottky diodes.  Choose appropriate diodes for both voltage and current.