Source: https://cemc.uwaterloo.ca/contests/past_contests/2017/2017PascalSolution.pdf