CAIRO -- Elders of a village in Egypt's Sinai Peninsula where militants killed 305 people in a mosque had been warned by Islamic State of Iraq and Syria (ISIS) operatives to stop collaborating with security forces and to suspend rituals associated with Islam's mystical Sufi movement, security officials and residents said.

The latest warning came as recently as a week ago, telling villagers in Al-Rawdah not to hold Sufi rituals on Nov. 29-30 to commemorate the birth of the Prophet Muhammad, according to residents and the officials who work for security and military intelligence agencies operating in Sinai.

Local operatives of the ISIS group's affiliate in Sinai consider Sufis to be heretics who should be killed. Ahmed Saqr, an expert on the Sinai insurgency, said militants had publicly identified the mosque, which also serves as a Sufi center, as a target months ago.

Get Breaking News Delivered to Your Inbox

He wrote on Saturday in a Facebook post that the selection of the Al-Rawdah mosque as a target "raises questions about those who read, analyze and prepare in our security agencies" and whether anything could have been done to prevent the "untold horrors."

Mohammed Ibrahim, a university student from the village, said militants had warned residents a few days before Friday's attack not to collaborate with security forces. The warning, he told The Associated Press by telephone Sunday from the nearby town of Bir al-Abd, followed the detention three weeks ago by villagers of three suspected militants who were handed over to security forces.

The militants also distributed leaflets several times ordering villagers not to cooperate with security forces and to abandon Sufism, he said.

Another Al-Rawdah resident, Mohammed Darwish, 30, said militants stormed the home of village tribal leader Sheikh Hussein al-Jerirr twice this year.

"They threatened him not to hold Sufi gatherings, and last week, they came on motorbikes and asked residents not to participate in Sufi rituals," Darwish said.

Egypt's military and security forces have been waging a tough and costly campaign against militants in the towns, villages and desert mountains of northern Sinai. Across the country, thousands have been arrested in a crackdown on suspected Islamists and other dissenters and government critics.

In the past year, militants have bombed churches in the capital of Cairo and other cities, killing dozens of Christians. The ISIS affiliate is also believed to be behind the 2016 downing of a Russian passenger jet that killed all 224 people on board, decimating Egypt's vital tourism sector.

The bloodshed at the Al-Rawdah mosque was the first major militant attack on a Muslim congregation, and it eclipsed violence in the country dating back to an Islamic militant insurgency in the 1990s.

The mosque is frequented by Sufis and is the local headquarters of the prominent Sufi sect, or tareeqah, founded by the local al-Jerir clan, a branch of the powerful Al-Sawarkah tribe.

The deadliest attack by Muslim extremists in Egypt's modern history came less than a week before the Prophet Muhammad's birthday on Thursday. Celebrations climax on Wednesday and Thursday, when millions of the Sufi movement's followers are expected to gather for rituals in mosques, shrines and squares across much of this majority Muslim country.

Militants from the ISIS affiliate have targeted Sufis before. Last year, they beheaded a leading Sufi religious figure, Sheikh Suleiman Abu Heraz, who was believed to be over 90 years old. They posted photos of the killing online.

ISIS propaganda often denounces Sufis. In the January edition of an ISIS online magazine, a figure purporting to be a high-level official in the group's Sinai affiliate vowed to target Sufis, accusing them of idolatry and heretical "innovation" in religion, warning that the group will "not permit (their) presence" in Sinai or Egypt.

Perhaps as many as 15 million Egyptians belong to Sufi orders, which hold sessions with chanting, poetry readings and ritual dancing meant to draw the faithful closer to God. Sufis also revere the tombs of holy men.

Islamic hard-liners view such practices as improper, even heretical, and militants in the region often destroy Sufi shrines, saying they encourage idolatry because people pray to those buried there.

The austere, one-story Al-Rawdah mosque sits on a main road. Its Friday prayers routinely attract villagers, travelers and workers from a nearby salt factory. About 500 men and boys were thought to have been in the mosque and an adjacent plaza at the time of the attack.

According to official figures, 305 people, including 27 children, were killed, with 128 wounded.

Security officials say ISIS militants infiltrated the area of Bir al-Abd and Al-Rawdah several months ago, opening a new front in the mostly desert region away from the main theater of operations in el-Arish and the towns of Rafah and Sheikh Zuwail to the east.

They have since waged deadly attacks on police and military forces, although the region has yet to attract the kind of heavy security seen elsewhere in the trouble spots of northern Sinai, according to the officials, who spoke on condition of anonymity because they were not authorized to talk to the media.

"The expansion of the militants to Bir al-Abd shows that they can go anywhere," said Hassan Khalaf, a tribal leader in the Suez Canal city of Ismailia, where some of the wounded from Friday's attack were taken.

"There are security flaws. ... We need to wage an all-out offensive to crush them."