Donald Trump claimed the 2016 US election was rigged against him, alleging without evidence massive voter fraud. Democrats have said Russian interference stacked the odds against Hillary Clinton.

In fact, in a week in which the supreme court said it would examine whether gerrrymandering violates the constitution, a new analysis of boundary lines drawn before the race shows the Republicans had a real advantage in state and national contests.

The AP scrutinized the outcomes of all 435 US House races and about 4,700 state House and Assembly seats up for election last year, using a new statistical method of calculating partisan advantage designed to detect cases in which one party may have won, widened or retained its grip on power through political gerrymandering.

The analysis found four times as many states with Republican-skewed state House or Assembly districts than Democratic ones. Among the two dozen most-populated states that determine the vast majority of Congress, there were nearly three times as many with Republican-tilted US House districts.

Traditional battlegrounds such as Michigan, North Carolina, Pennsylvania, Wisconsin, Florida and Virginia were among those with significant Republican advantages in their US or state House races. All had districts drawn by Republicans after the last census in 2010.

The AP analysis also found that Republicans won as many as 22 additional US House seats over what would have been expected based on the average vote share in congressional districts across the country. That helped provide the GOP with a comfortable majority over Democrats instead of a narrow one.

Republicans held several advantages heading into the 2016 election. They had more incumbents, which carried weight even in a year of “outsider” candidates. Republicans also had a geographical advantage because their voters were spread more widely across suburban and rural America instead of being highly concentrated, as Democrats generally are, in big cities.

Yet the data suggest that even if Democrats had turned out in larger numbers, their chances of substantial legislative gains were limited by gerrymandering.

“The outcome was already cooked in, if you will, because of the way the districts were drawn,” said John McGlennon, professor of government and public policy at the College of William & Mary in Virginia who ran unsuccessfully for Congress as a Democrat in the 1980s.

A separate statistical analysis conducted for the AP by the Princeton University Gerrymandering Project found that the extreme Republican advantages in some states were no fluke. The Republican edge in Michigan’s state House districts had only a 1-in-16,000 probability of occurring by chance; in Wisconsin’s Assembly districts, there was a mere 1-in-60,000 likelihood of it happening randomly, the analysis found.

The AP’s findings are similar to recent ones from the Brennan Center for Justice at the New York University School of Law, which used three statistical tests to analyze the 2012-2016 congressional elections. Its report found a persistent Republican advantage and “clear evidence that aggressive gerrymandering is distorting the nation’s congressional maps”, posing a “threat to democracy”. The Brennan Center did not analyze state legislative elections.

The AP’s analysis was based on a formula developed by University of Chicago law professor Nick Stephanopoulos and Eric McGhee, a researcher at the nonpartisan Public Policy Institute of California. Their mathematical model was cited last fall as “corroborative evidence” by a federal appeals court panel that struck down Wisconsin’s state Assembly districts as an intentional partisan gerrymander in violation of Democratic voters’ rights to representation.

A dissenting judge ridiculed the Wisconsin ruling for creating a “phantom constitutional right” of proportional political representation. Wisconsin’s attorney general has argued on appeal that the ruling could “throw states across the country into chaos”.

Although judges have commonly struck down districts because of unequal populations or racial gerrymandering, the courts until now have been reluctant to define exactly when partisan map manipulation crosses the line and becomes unconstitutional.

The US supreme court has agreed to hear arguments on the Wisconsin case this fall. If upheld, it could dramatically change the way legislative districts are drawn across the US – just in advance of the next round of redistricting after the 2020 census.

If partisan gerrymandering “goes unchecked” said Sam Wang, director of the Princeton Gerrymandering Project, “i’s going to be worse – no matter who’s in charge.”