The GOP's tax bill, which squeaked through the Senate 51-49 in the wee hours of Saturday morning, is supposed to be a boon for businesses. Stock markets were surging all year anticipating that it would become law, and hit new record highs on Monday. "With the great vote on Cutting Taxes, this could be a big day for the Stock Market — and YOU!" President Trump crowed from his Twitter feed.

But as the week wore on, the stock market cooled off. When it closed Thursday afternoon, the S&P 500 was down around 23 points (0.8 percent) from its Monday peak. The Dow Jones was down just over 280 points (1.6 percent), and the Nasdaq was down 79 points (1.1 percent). Corporate accountants and tax lawyers were gnashing their teeth.

What's the problem? It turns out the Senate bill contains a $329 billion screw-up — and one that could undercut the GOP's pro-business efforts.

Under current law, U.S. corporations officially pay a tax of 35 percent on their profits. But that tax is loaded with all sorts of deductions, credits, and carveouts, so a lot of companies actually wind up paying far less than the headline rate. This is where something called the corporate alternative minimum tax, or corporate AMT, comes in.

The corporate AMT is supposed to set a threshold below which a company's tax rate won't fall. As Slate's Jordan Weissmann explained, it's "basically a parallel tax code meant to prevent companies from zeroing out their IRS bills." Companies calculate what they'd pay under both taxes, and if what they'd pay under the official corporate tax is lower than what they'd pay under the AMT, they pay the AMT.

Republicans want to change this. They plan to clear out all the credits and deductions and exemptions, then use the additional revenue to lower the overall corporate tax rate to something like 20 percent.

Senate Republicans didn't want to get rid of all the breaks, however: There's a very popular tax credit for research and development that they wanted to preserve, for instance. And they wanted to give a special low tax rate — or no taxes at all — to corporate profits from business done overseas, in order to encourage companies to bring that money back into the country.

Since companies would have far fewer opportunities to game the official rate, the need for the corporate AMT would seem far less acute under this logic. That's what House Republicans thought, anyway: Their version of the tax bill eliminated the corporate AMT entirely. The Senate bill did as well ... at least at first.

But in all the frenzied deal-making that characterized the Senate bill's final hours before passage, and in a desperate search for some additional revenue to make the math work, Senate Republicans stuck the corporate AMT back in.

Here's the problem: The new official corporate tax rate in the Senate bill is 20 percent. The corporate AMT rate in the Senate bill is also 20 percent.

There's no room to maneuver. No matter what, corporations are going to wind up paying a 20 percent tax on their profits. All the tax breaks the GOP wanted to preserve, like the R&D credit and the new territorial system for profits overseas? All of those are effectively wiped out.

It's also probably a much bigger wipeout than Republicans realized. The Joint Committee on Taxation had estimated that reinstating the corporate AMT would give senators an extra $40 billion to play with. But that was in the middle of a flurry of last-minute changes to the bill. Weissmann spoke with Lily Batchelder, a New York University law professor and tax expert: She looked at how the reintroduced corporate AMT interacts with the final bill, and estimated it raises $329 billion instead.

That's $289 billion more than lawmakers thought they'd be removing from corporate coffers. Needless to say, lots of businesspeople and investors are not happy about this.

"With many tech companies such as Broadcom, Apple, Microsoft, Cisco, and Oracle, among others, significantly ramping up R&D this would be a major stomach punch to tech players," Daniel Ives, the head of technology research at GBH Insights, told CNBC. The coal industry — which President Trump has repeatedly promised to revive — is also freaking out. Robert Murray, the CEO of Murray Energy, estimated his company's tax bill would rise by $60 million annually. "We won't have enough cash flow to exist. It wipes us out," Murray told CNNMoney.

This is all bitterly hilarious. The GOP tax bill was one giant smash-and-grab operation to benefit corporations and their wealthy owners. To pay for the cut in the corporate rate, the bill would hike taxes on, and upend the finances of, whole swaths of U.S. society. Plenty of middle-class families would see their tax bill rise as early as 2018 — and just about everyone making under $75,000 would pay more in taxes by 2027 — all to pay for the corporate rate cut.

But Senate Republicans jammed the bill through so fast, with so few hearings, and so little scrutiny, that they shot themselves in the foot.

Granted, the big companies that organize themselves as pass-through businesses won't have to deal with this problem. That includes a lot of oil and gas operations, not to mention Trump's own real estate business. But all the big C-corporations that pay the corporate profits tax are going to be mad. And that's going to create headaches.

The House and Senate versions have to meld into one final piece of legislation before President Trump can sign it into law. House Majority Leader Kevin McCarthy (R-Calif.) is already on the warpath: "[The corporate AMT] should be eliminated, for sure,” he said on Monday. At the same time, the revenue from reinstating the corporate AMT was necessary for the Senate GOP to both meet procedural rules, and to keep some of their members on board.

The Senate's original bill passed with 51 votes. The GOP can only afford one more defector. More than that, and their tax overhaul dies.

Is it any wonder Wall Street is freaking out?