Two trains narrowly escape collision