Construction of the Musjid

Building above the Musjid

Q: Is it permissible to build a musjid with three flats on the top. One for the Imaam, one for the Mu'azzin and one to give on rent as income for the Musjid. If other accommodation is later on found for the Imaam and Mu'azzin, and the place becomes vacant, can the three flats be given on rent
for the income of the Musjid?