The prayer in this manner which the questioner has described is permissible, meaning that it is valid. The toilet being in front of the worshippers does not nullify the prayer due to the generality of the saying of the Prophet (sallallaahualaihi wasallam), “The earth was made a praying place and a means of purification for me.”
However, it may be that the toilet has a bad smell and thus affects the one who is praying and troubles him. So if can be avoided due to this reason then it is better, since anything that harms the one praying, then it is legislated for him to distance himself from it. And it is established from the Prophet (sallallaahualaihi wasallam) that he prayed one day on a khameesah (a type of clothing) which had patterns on it, so the Prophet (sallallaahualaihi wasallam) glanced at the patterns. When he finished the prayer he said, “Take this khameesah of mine to Abu Jahl and bring me his cloak.” This is because he took a glance at the patterns and this distracted him in the prayer, so because of that the Prophet (sallallaahualaihi wasallam) ordered that this khameesah be given to Abu Jahl and his abjaaneeyah taken.
So the benefit of this hadeeth is that anything that distracts the praying person from his prayer and occupies him, then it is necessary to avoid it.