Mg(OH)2 is the correct chemical formula because it takes two Hydroxide ions to bond with Magnesium normally it would be written with a subscript number 2 rather than the parenthesis and number 2
2. 3H2 + N2 means you have 6 Hs and 2 Ns. NH3 has one N and 3 Hs, so you need 2 NH3s in order to have the 2 and 6 of each that you need on both sides of the reaction.