It starts with the fact that the railways are first and foremost responsible to manage the safety of their operations, as any transportation operator is. You need a strong framework with regulations, and then you need strong oversight to make sure companies are able to identify the things that can go wrong, take steps to make sure they don't lead to adverse consequences, and make sure the steps they've taken fix the issue they've identified.
That is how you reduce risk in operations. Reducing risk reduces the chance of accidents.