Praise be to Allah.
Whoever reviles Allah, may He be exalted, or His Book or any of His angels or Messengers is a disbeliever who is beyond the pale of Islam; it is not permissible to pray behind him because the ummah is unanimously agreed that it is not permissible to pray behind a disbeliever, whether he was originally a disbeliever or is an apostate. The one who prays behind him when he is aware of his situation has to repeat the prayer.
Imam ash-Shaafa‘i (may Allah have mercy on him) said:
The disbeliever cannot be an imam under any circumstances, whereas the believer may be an imam in all circumstances, except that he should not pray unless he is in a state of purity.
Similarly, if a man is a Muslim and he apostatises, then he leads the prayer whilst he is an apostate, the prayer of those who prayed behind him is not acceptable unless he openly expresses his repentance verbally before leading them in prayer. If he openly expresses his repentance verbally before leading them in prayer, then it is acceptable for them to pray with him.
End quote from al-Umm (1/195)
Ash-Shiraazi said in al-Muhadhdhab (1/183):
It is not valid for a disbeliever to lead the prayer, because he is not one of those who are obliged to pray; if he goes forward and leads people in prayer, he is not regarded as having become Muslim, because that is one of the minor issues of faith, so he does not become Muslim by doing that. The same ruling applies if he were to fast Ramadan or give zakaah on his wealth. As for the one who prays behind him, if he is aware of his situation, then his prayer is not valid, because he connected his prayer to a prayer that is invalid…. End quote.
Ibn Qudaamah (may Allah have mercy on him) said:
It is not valid to pray behind a disbeliever under any circumstances, whether one becomes aware of his being a disbeliever after finishing the prayer or before that. The one who prayed behind him has to repeat his prayer. This is the view of ash-Shaafa‘i and ashaab ar-ra’y. Abu Thawr and al-Muzani said: The one who prayed behind him and was not aware of the situation does not have to repeat his prayer, because he followed the prayer of one whose situation he was unaware of, so his case is like that of one who followed in prayer a person who is in a state of minor impurity.
End quote from al-Mughni (2/16)
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said:
It is not valid to pray behind a disbeliever in any situation, whether his disbelief is in terms of beliefs, words, actions or things that he fails to do.
In terms of belief e.g., if he believes that there is another god besides Allah.
In terms of words e.g., if he ridicules Allah or His Messenger or His religion. Whoever ridicules Allah or His Messenger or His religion is a disbeliever, even if he prays. … We know that no Muslim can pray behind a disbeliever, but if we assume that someone prayed behind a man and was not aware that he was a disbeliever until after the prayer was over, does he have to repeat the prayer or not?
The answer is that some of the scholars said that he does not have to repeat the prayer, because he is excused.
And some scholars said that he does have to repeat the prayer, because one of the conditions of leading the prayer being valid is that the imam should be a Muslim.
If someone were to say: can we differentiate and say that if the signs of disbelief are obvious in his case, then the prayer is not valid, and there is no excuse for ignorance because there is circumstantial evidence, otherwise the prayer is not invalid?
Our answer is: that may be the case. The more correct scholarly view concerning this issue is that if he is unaware (of the imam being an apostate), then his prayer is valid.
End quote from ash-Sharh al-Mumti‘ (4/220).
Based on that, the prayers that you offered behind that evildoer imam are not valid on his part, primarily because of his being an apostate, or on your part, because your prayer was connected to the prayer of an apostate. So you have to repeat all of these prayers, and strive hard to work out how many they are, until you think it most likely that you have done what you have to do. This is the view of the majority of scholars.
Ibn Qudaamah said in al-Mughni (1/439):
If the number of missed prayers is great, he should try hard to make them up, unless that will cause him physical or financial hardship. As for physical hardship, that is when he may get too tired or may fear sickness. As for financial hardship, that is when it could lead to him becoming unable to dispose of his wealth (and run his business), to the point that it keeps him away from earning a living, or he may be harmed by that. Ahmad stated something to this effect. If he does not know how many prayers he owes, he should keep making them up until he is certain that he has made up all that he missed. Ahmad said, according to the report of Saalih, concerning a man who had missed prayers: he should keep making them up until he is sure that he has made up all that he missed. End quote.
This case is not like that of one who is committing an innovation that could put him beyond the pale of Islam, because he may be excused by some as he is basing his innovation on some sort of ijtihaad or misinterpretation or the like, and even if he is to be regarded as a disbeliever, his disbelief is not like that of one who has the audacity to revile Allah, may He be glorified and exalted. So how about if he combines these two evil deeds? How could such a person pray to the Lord of the Worlds, let alone become an imam?!
And Allah knows best.