Canada and USA now have a new Customs Pre-Clearance Agreement that covers railroads in addition to airports.
https://www.dhs.gov/news/2019/08/15/united-states-and-canada-implement-preclearance-agreement
https://www.dhs.gov/news/2019/08/15/united-states-and-canada-implement-preclearance-agreement