Question: why pmos substrate is connected to vdd?
Answer: Because the voltage between the Ground and the Source in the NMOS transistor has to be positive, so the logical choice is to connect the Source to the ground. In PMOS, the voltage between the Gate and the Source has to be negative, so you connect the Source to VDD.