A semiconductor factory is very expensive to build and run in terms of the actual building (vibration resistant, advanced ventilation with purified air etc) and the equipment inside (some of the most expensive machines you can buy). It's all extremely expensive. It is also fairly automated and requires few people to run compared to the value it outputs. So the cost of labor is low in comparison and thus it's not a big deal to put it in a high cost country.