The locomotive came to a stop in trees close to the line