Two men have been jailed after nearly derailing a 95mph train by piling concrete posts and pallets on the line, British Transport Police said.
Richard Hamilton, 21, and Ashley Leedham, 18, moved materials left by contractors into a 4ft-high pile on the line in Dosthill, Tamworth, Staffordshire in March.
The pile was hit by a train travelling at 95mph with 50 passengers on board, nearly causing it to derail. British Transport Police (BTP) said it was "sheer luck" nobody was injured.
A spokesman said Hamilton and Leedham, both from Kingsbury, Tamworth, were jailed for four-and-a-half and four years respectively at Warwick Crown Court.
The pair previously admitted malicious obstruction of the railway with intent to endanger safety. The incident happened at about 11.30pm on March 18 when the driver of the 11.09pm Birmingham New Street to Nottingham service hit debris on the line, the spokesman said.
The driver managed to stop the train after hitting the objects, which included concrete posts, wooden pallets and bags of cement. It eventually came to a standstill just under half a mile away without derailing, but he told police afterwards he feared for his life as the train lifted from the tracks.
"I thought my time was up. I thought the train had come off the rails as it was juddering violently," he told officers. "I then remember seeing a road bridge going over the railway lines and I remember shouting to myself, 'Oh no'. I thought the train was going to hit the bridge and that I was going to die. Images of my children, my family and people close to me flashed before me and I thought I would never see them again."
None of the 50 passengers on board the CrossCountry service were injured, but the driver was left extremely shaken, BTP said.
Chief Inspector Allan Gregory said: "It is a matter of sheer luck that no-one was seriously injured in this incident. The reality is that we could have been left dealing with mass casualties or even, in a worst case scenario, fatalities."
Network Rail route director Jo Kaye added: "This was a ludicrous thing to do and could easily have resulted in passengers or the train crew being killed. It is good to know that the judicial system has recognised the seriousness of the case in the sentences they have given. I hope this acts as a lesson to anyone else who may be inclined to carry out such reckless acts."