Is it legal to put coins on a railroad track?

It’s not new that kids have been placing coins on train tracks for as long as there have been trains and pennies. The legend says that flatten coins bring good luck.
While that might sound like a lot of fun, it is in fact very dangerous. Doing so is potentially dangerous to you and people around you. The flat and sharp coins might

squeeze out of the train’s wheels

, at an astonishing speed,  causing property damage or injury.
Placing pennies on a railroad track is in fact illegal. The railroad tracks are private property, so doing that is considered trespassing.
Railroads have their own security, and that is Railroad police. Railway police  are persons, certified state law enforcement officers, responsible for the protection of railroad (or railway) properties, facilities, revenue and personnel, as well as carried passengers and cargo. They have the power to investigate and arrest on and off railroad property in most states.
If you are seen placing a penny on the railway, the police might find you in violation of the law.
Sadly but true, according to Railroads.dot.gov, 400 people die each year from trespassing along railroad rights-of-way and approximately as many injuries.
Additionally, you are interfering with national currency, because the coin will be damaged. It is also illegal to deface, or destroy any valid United States currency.

Can a penny left on the track derail the train?

A coin left on a track does not typically derail a train, but you don’t want to find out. The penny it might simply be too light to derail an average 200 ton locomotive.
Derailments have numerous causes, including objects on the track such as snow, ice, coal, equipment failures, human error.
For your own safety and the safety of others, don’t place anything on the tracks. There are some wonderful safety tips on https://www.amtrakcascades.com:

Expect a train always.

Look twice in both directions, trains can come from either direction
Track walking is trespassing. It’s extremely dangerous to walk, run, or drive down the railroad tracks or even alongside them.
Trains can’t stop quickly. By the time a train engineer sees you on the tracks it’s usually too late to stop the train. It takes more than a mile for most trains to stop

Your life is important. Stay safe!