You are given a non-empty list of positive integers [x1,x2,…,xN]. The power tower of this list is the number
x1x2⋅⋅⋅xN
The exponents are evaluated from the top down, so the value is x1(x2(⋯xN)).
A power tower grows very large even when the list holds only a few small integers. The power tower of [2,3,2] is 232=29=512, and the power tower of [5,2,3,2] is 5512, which has 358 decimal digits.
Compute each power tower modulo a given positive integer M.