Iran and Saudi Arabia have agreed to restore diplomatic relations that were broken in early 2016.

Embassies will be reopened in Tehran and Riyadh within two months. The countries agreed to respect state sovereignty and not interfere in each other’s internal affairs. A security cooperation agreement from 2001 will be reactivated.

The accord was announced in talks in Beijing, brokered by China’s top diplomat Wang Yi.. Iran is represented by Ali Shamkhani, the Secretary of the National Security Council.

Iran and Saudi Arabia have been long-standing rivals in the Middle East. Tensions worsened in September 2015 when 464 Iranians were among more than 2,000 killed in a stampede in Mecca, and two months later with the Saudi execution of Shia cleric Sheikh Nimr al-Nimr.

Days later, a crowd attacked and set fire to the Saudi Embassy in Tehran, prompting Riyadh to break relations.