Praise be to Allah.
Purity is one of the conditions of prayer being valid. Whatever a person does without being in a state of purity is not regarded as prayer and will not be accepted from him, even if he does all the actions and says all the words of the prayer. The Messenger of Allah (blessings and peace of Allah be upon him) said: “No prayer will be accepted without purification.” Narrated by Muslim (224) from the hadeeth of Ibn ‘Umar.
And he (blessings and peace of Allah be upon him) said: “Allah will not accept the prayer of any one of you if he breaks wind, until he does wudoo’.” Narrated by al-Bukhaari (6440) and Muslim (330).
If purity is lost during the prayer, what the worshipper must do is stop praying so that he can renew his purification, then he should repeat the prayer from the beginning.
If he can leave in order to do wudoo’ then catch up with the prayer in congregation, even if it is only one rak‘ah of it, he must leave, so that he can catch up with the congregation, especially in the case of Jumu‘ah prayer, and it will not matter if he passes through the rows, because the sutrah of the imam is the sutrah of those who are behind him.
But if he breaks wind in the last part of the prayer, and he knows that if he goes out, walking between the rows, he will not be able to catch up with the prayer in congregation with the imam, and going out is difficult for him, it is permissible for him to wait until the prayer ends, so that he can do wudoo’ and start his prayer again from the beginning.
Shaykh Saalih al-Fawzaan (may Allah preserve him) was asked:
If a person is praying in the front rows in the mosque, and he breaks his wudoo’ during the prayer, but he cannot go out because there are many rows in the mosque, should he complete the prayer without reciting, rather bowing and prostrating and standing silently? Or should he sit until the prayer ends, even if he is in the middle of the row?
What is prescribed in the case of one who breaks his wudoo’ during the prayer is to leave, as the Prophet (blessings and peace of Allah be upon him) said: “He should not leave unless he hears a sound or notices a smell.” Narrated by al-Bukhaari in al-Saheeh, 1/43. This indicates that the one who breaks his wudoo’ for sure should leave and should not stay there.
If you do not leave because of what you mention of difficulty or there being too many rows, it is not permissible for one to continue praying. If he is able to leave then he should leave; this is what is indicated by the evidence. If he cannot leave, he should sit until he finds an opportunity to leave. And Allah knows best.
Al-Muntaqa min Fataawa al-Fawzaan.