بسم الله الرحمن الرحيم
In the above mentioned case of death in accident, it is correct to take compensation from the company. If all the heirs of the deceased person give some amount from this amount to the mosque then it is allowed to take it and use for the mosque. As this amount is like diyat (blood money) in which the all the heirs have their rights and this amount shall be distributed among all the heirs as per their shares.
Allah knows Best!
Darul Uloom Deoband
This answer was collected from the official ifta website of Darul Uloom Deoband in India.