Fortunately, we and the Iraqis finally figured that out, and Iraq has a much better government today. But the U.S. keeps repeating the same mistake in the Middle East: overestimating the power of religious ideology and underappreciating the impact of bad governance.

As Sarah Chayes, who long worked in Afghanistan and has written an important book — “Thieves of State: Why Corruption Threatens Global Security” — puts it: “Nothing feeds extremism more than the in-your-face corruption and injustice” that some of America’s closest Middle East allies administer daily to their people.

The third ISIS faction was composed of the true religious ideologues, led by al-Baghdadi. They have their own apocalyptic version of Islam. But it would not have resonated so far and wide were it not for the first two factors listed above.

And that leads us back to Trump and his foreign policy. Trump has never met a dictator he did not like. He is blind to the fact that the next al-Baghdadi is being incubated today in some prison in Egypt, where President Abdel Fattah el-Sisi of Egypt, whom Trump once actually called “my favorite dictator,’’ is not only rounding up violent Jihadists but liberal nonviolent journalists, activists and politicians. Their only crime is that they want to have a say in their country’s future and help to create an environment where they can realize their full potential — so they will not have to look for dignity, power, a job or a girl’s hand from extremist groups like ISIS.

When Trump praises Baghdadi as his favorite victim and el-Sisi as his favorite dictator, all he is doing is walking in place. We’re actually getting nowhere.

And that brings me back to Syria. Syrian Sunnis supported ISIS for the same reason Iraqi Sunnis did. Iran, the pro-Iranian Hezbollah militia, the Shiite-Alawite Syrian regime of Bashar Assad and Russia have all collaborated to create a pro-Iranian Shiite minority government in Damascus. Of course they gave Trump a free pass to kill Baghdadi! His death just makes it that much easier for them to rule Syria without sharing power with the Sunnis. As long as that’s the case, there will be no stability there.

Finally, Trump kept going on and on in his news conference about how he, in his infinite wisdom, was keeping U.S. troops in Syria to protect the oil fields there so maybe U.S. oil companies could exploit them. He even boasted that while he was against the Iraq war, we should have taken over all of Iraq’s oil fields to pay for it.