How to measure the length of all railway track per country?

I want to generate a table like this List of countries by rail transport network size - Wikipedia but using the length of tracks in OSM. How to do this?

Perhaps the overpass API can help but I could not find a good introduction to it. Suggestions are welcome.

You can use Ohsome site while click/tap on “length” in “measure”

Notice the length of railway track showed is what mapped on OSM at that time in UTC, railway with double or multiple tracks will be count separately, and wrongly mapped “railways” also includes, unmapped railway tracks are not included.