NEW YORK (Fortune) -- Guess who's helping Bank of America pay for its $4.1 billion purchase of Countrywide Financial? Answer: The taxpayers of the United States.

That's because Bank of America (BAC, Fortune 500), which is solidly profitable, will be able to use some of Countrywide's losses to offset its own taxable income. The tax break could total about half a billion dollars over the first five years, according to an estimate by tax guru Robert Willens, who left Lehman Brothers Friday after a 20-year run and will be in business as Robert Willens LLC starting next week. The losses could be worth considerably more to Bank of America starting in the sixth year, depending on how big Countrywide's losses are when Bank of America formally acquires it.

At this point, of course, no one knows how much in losses Countrywide has run up since the junk mortgage market began souring and defaults accelerated. Countrywide (CFC, Fortune 500) itself probably doesn't know. But it seems almost certain to ultimately be in the billions.

In tax circles, Bank of America is famous for its 1988 purchase of the failed FirstRepublic Bank of Dallas, which was being auctioned off by federal regulators. Bank of America, then known as NCNB Corp., the parent of North Carolina National Bank, discovered a way to structure the deal to save $1 billion of taxes, using a convoluted strategy that none of the other bidders knew about. That allowed NCNB to outbid its rivals for the bank, and still come out way ahead.

The Countrywide tax break isn't in that league, but it would still be worth a lot of money. Willens estimates that Bank of America will be able to deduct $270 million of Countrywide's losses annually for the first five years it owns the firm.

That's based on a $6 billion purchase price - $4 billion to Countrywide's common stockholders, plus the $2 billion of preferred stock that Countrywide sold to Bank of America in August. Willens says that you multiply that $6 billion by 4.49 percent - the so-called "long-term tax-exempt rate" - to calculate how much of Countrywide's losses Bank of America can deduct annually for five years after the purchase.

A $270 million annual deduction would save Bank of America something more than $100 million a year in federal and state income taxes. The long-term tax-exempt rate, which is based on Treasury rates and other things so complicated that they make my teeth hurt. The rate changes each year, Willens says, but not by much. When I asked how it's calculated, Willens, a master of tax arcana, threw up his hands. (Metaphorically, of course.) "It's like the formula for Coca-Cola," he said, "no one outside the circle knows it" and it's so complicated that, "no one else wants to find out."

So over the first five years, Bank of America can use a total of $1.35 billion of Countrywide's losses to shelter its income. (That's five years of $270 million annual losses.) If Countrywide's embedded losses when Bank of America buys it exceed $1.35 billion, Willens says, the bank will be able to deduct the rest of the losses, without limit, starting in the sixth year.

Isn't life fun?