The circuitous nature of your design will make it very complicated to do everything, you want to start with pharaoh,
you want the palace to spawn pharoah,
and pharoah to spawn the palace,
and the palace to destroy pharaoh.
You also want pharoah to be able to settle in cities.
You also want the palace to be buildable without pharoah,
but also be restricted in location... How many places do you want to restrict it to, 1 or more?
Is this a scenario with pre-placed cities, or do you found all cities yourself (with ai too)
Basically the answer to these quetions will change how you go about making it all work.
For example, if you want to restrict the palace location to 1 place, then a building prereq is what you want. (if the scenario has a prebuilt city location)
If you want it to be only buildable in a town that was originally founded by egypt, I have some python that should be moddable to do that, by adding a prereq building to every town that egypt founds.
If you use a resource prereq, then when that resource is connected to the trade network, you will be able to build the palace anywhere that is connected to the trade route, potentially even greece.