Saudi Arabia might seem like an important ally in the world's fight against violent extremism. It joined the US-led coalition against ISIS and before that fought al-Qaeda, dismantling and infiltrating its networks inside Saudi Arabia and abroad and helping disrupt more than one al-Qaeda plot against the United States. Both ISIS and al-Qaeda loathe the Saudi royal family and have carried out major attacks inside the kingdom — including a sustained, bloody campaign by al-Qaeda in the mid-2000s to bring down the Saudi regime, which took years to defeat.

Under Crown Prince Mohammed bin Nayef, the "prince of counterterrorism" who personally survived assassination attempts by Islamist militants, Saudi Arabia made it harder for private citizens to finance terrorist organizations, used state-run media outlets and regime-appointed religious leaders to push a strong counter-jihadist message, and instituted an innovative yet controversial program aimed at reintegrating and rehabilitating jihadists.

Yet for all its efforts to fight ISIS and other extremists, the Saudi regime frequently does things that actually strengthen these same extremists, making the problem worse. In some ways this is deliberate, and in others it's not, but in either case the actions are often driven by the country's own difficult internal politics. And it makes Saudi Arabia a real contributor to the problem of violent religious extremism — even as that problem threatens the kingdom as well.

How Saudi Arabia promotes an ideology that drives extremism

The Saudi government has for decades promoted a strict, fundamentalist, highly intolerant strain of Islam known as Wahhabism, which it spreads through its official state-sanctioned mosques and through the "madrassas" (in this context, Islamic religious schools) it established all over the world. Because many of the teachings of Wahhabism line up with the ideology of ISIS and the jihadist groups that came before it, the spread of Wahhabism has given those groups more fertile ground to grow and recruit.

In a world in which Saudi Arabia had never spread Wahhabism, ISIS leader Abu Bakr al-Baghdadi may very well still have developed his toxic and hateful ideology. But he would not have been able to draw on the Wahhabi mosques and schools whose alumni are already taught to be sympathetic to many of his ideas.

Wahhabism grew out of the teachings of an 18th-century reformer named Ibn Abd al-Wahhab, who argued for "purifying" Islam by getting rid of the "innovations" that had snuck in over the centuries as Islam spread to new lands and mixed with indigenous beliefs and practices. Wahhab believed these practices, such as praying at the tombs of revered prophets or wearing special charms to ward off evil, violated the central tenet of Islam: that only God is worthy of worship. This is known in Islam as the concept of "tawhid," or the "absolute oneness of God." For Wahhab (and for most Muslims), violating that central tenet is the worst sin one can commit, and Wahhab declared those who do it to be false Muslims. Where Wahhab differed from the mainstream was mainly in the sheer number of behaviors and practices he deemed to be violations of tawhid and thus the number of people who could potentially be accused of apostasy.

The doctrine we know today as "Wahhabism" is much more rigid and extreme than Wahhab’s original teachings — and ISIS’s ideology is even more extreme and violent than traditional Wahhabism. For instance, although Wahhab viewed Shias as non-Muslims, he didn't say they should necessarily be killed, but rather educated about their incorrect beliefs and shown the right path.

Later groups such as ISIS took some of the ideas of Wahhabism — a preoccupation with apostates and with purifying Islam — to new extremes, targeting Shia and other "apostates" with brutal violence. This went far beyond anything that Wahhab had preached, but it is nonetheless a big part of why some jihadist groups are so focused on the idea of purifying Islam. Jihadists have targeted Shias for their religious beliefs as well as the governments of Muslim-majority countries whose leaders had, in the eyes of jihadists, unacceptably transgressed. And the leaders of Saudi Arabia itself have long been at the top of such lists of targets.

Why Saudi Arabia is stuck with an ideology that threatens even itself

Since the spread of Wahhabism helped create and fuel ISIS, al-Qaeda, and the broader jihadist movement, which views the Saudi regime as an illegitimate apostate regime that should be brought down, you might wonder why the Saudi regime would keep promoting Wahhabism. Wouldn't the first step in defeating these groups be to stop furthering the ideology that fuels them?

Yes, it would. But the Saudi regime believes it can't do this without risking everything. The legitimacy of the Saudi royal family rests almost entirely on its religious credentials, which it mainly gets from the support of the country's ultra-conservative Wahhabi religious establishment.

This goes back to the 18th century, when the House of Saud made a strategic pact with the Wahhabis in which the latter would endorse the political legitimacy of the House of Saud in return for the Saudi royal family endorsing and spreading the Wahhabi interpretation of Islam. The modern state of Saudi Arabia was built on this alliance, and it remains a critical one to this day. Because the actual members of the Saudi royal family are much less religiously conservative than are most Saudi citizens (sometimes scandalously so), the regime needs the endorsement of the Wahhabis to stay in power.

So it's a Catch-22: The Saudi regime promotes Wahhabism in order to stay in power, but the more it promotes Wahhabism, the more it indirectly bolsters ISIS and other jihadists, who want to remove the Saudi royal family from power.

Which is how you get bizarre situations like the Saudi regime issuing a royal decree that makes it illegal for Saudi citizens to give "moral or material aid to groups including Islamic State and al Qaeda's official offshoot in Syria, the Nusra Front," while more than 50 hard-line Saudi clerics issue a statement calling on Sunni Muslims to unite against Russia, Iran, and the Assad regime in Syria and referring to those fighting the Syrian regime as "holy warriors" — widely seen as an endorsement of the extremists fighting there.

How Saudi Arabia drives sectarianism, which drives extremism

Saudi Arabia and Iran have for years been in a sort of proxy struggle for dominance of the Middle East. They also happen to be divided by religion: Saudi Arabia's government is officially Sunni, and Iran's is officially Shia. And both countries have at times exploited this by pushing a sectarian worldview of Sunni versus Shia.

For Saudi leaders, it's not that they hate Shias so much as that they see this as a tool for countering Iran by motivating Sunnis in the region to fear and resist Iranian influence. But this helps motivate both Sunni and Shia Muslims to take up arms to defend their respective sides wherever they are perceived to be under threat. Iran has indulged this as well, for example by backing Shia militias who have targeted Sunnis. Both have thus helped to drive Sunni-Shia conflict across the region.

ISIS and other Sunni jihadist groups can use this to find more recruits for fighting the Shias they consider apostates. They can capitalize on the wider regional instability to establish a foothold and ignite the passions of fighters on both sides of the sectarian divide, increasing the number of recruits and producing bigger, longer, bloodier wars.

This also inspires sectarian terrorist attacks within the kingdom of Saudi Arabia itself. Most of the recent terrorist attacks that have taken place in Saudi Arabia have been carried out by ISIS or ISIS-affiliated individuals and have targeted Shia.

Many of the conflicts currently raging in the Middle East — from Yemen to Syria to Iraq — have been exacerbated or even initiated by this struggle between Iran and Saudi Arabia. It's a struggle in which, to be clear, both sides share tremendous blame for sowing violence and sectarianism. And there's more going on in the Saudi-Iran struggle than just sectarianism. But the point is that sectarianism is a narrative that helps ISIS, and sectarian conflict gives it fertile ground for fighting and recruiting.

Wherever there is war, there is chaos, and wherever there is chaos, there is ISIS. For instance, although Yemen has long had an al-Qaeda presence, it wasn't until the Saudis got involved in Yemeni civil war — in part to push back perceived Iranian influence — that ISIS gained a foothold in Yemen and began carrying out terrorist attacks targeting Shias.

Saudi Arabia erases the "third option" between "tyrants or terrorists"

Saudi Arabia is also helping drive young people into the arms of ISIS by contributing to a political climate in which Middle East citizens only have two choices: dictatorship or violent opposition to dictatorship. As long as those are the only two options, some number of people will choose the latter — thus helping to legitimize violent extremism.

The Saudi regime fears that popular democratic movements could spread to its own country. To prevent this, it's fought those movements where they start, bankrolling Arab Spring counter-revolutions throughout the region. The Saudis supported the military coup in Egypt that overthrew the democratically elected Muslim Brotherhood government. It intervened militarily to put down a nascent uprising in neighboring Bahrain. It's helping to shore up the Jordanian monarchy, and it's cracking down hard on dissent of any kind at home.

By propping up Arab dictators, closing all avenues of public dissent, and eliminating other legitimate avenues of political engagement, the Saudis have validated the longstanding argument of violent extremists like al-Qaeda and ISIS that violence is the only way to effect change in the Middle East. The Saudis are forcing a choice between corrupt, repressive dictators and violent extremism. Not because they love violent extremism, but because they fear that if Saudi citizens are offered a third choice — democracy — they'll choose that and eject the monarchy.

As the exiled Middle Eastern pro-democracy activist Iyad el-Baghdadi explained in my recent interview with him, "The menu of ideas in the Arab world only has tyrants or terrorists. It doesn't have a third option. It's a very narrow menu." There is, he said, no "third option." And Saudi Arabia would like to keep it that way.