How summation is changed in Analytic number theory Consider this expression S(x, z) = $\sum_{n\leq x} \sum_{{d|n , d|P(z) } }\mu(d)  $ . I don't understand the logic behind next step and get really confused on how summation is changed.

In  next step author writes $\sum_{n\leq x} \sum_{{d|n , d|P(z) } }\mu(d)  $  = $ \sum_{d| P(z), d \leq x }\mu(d) \sum_{n\leq x, d|n } 1 $ .

My thoughts: It is clear to me that 1 variable is d and 1 is n. I understand 1st summation from left , $d| P(z) , d\leq x $  and how n$\leq x$ is used but I don't understand why author wrote d|n in 2 nd summation.
Can you please explain it in detail.

Sometimes, one way to approach switching orders of summation is to make indices completely independent by introducing functions that give conditions.
For example, we might write
$$ \sum_{n \leq x} \sum_{\substack{d \mid n \\ d \mid P(z)}} \mu(d) =
\sum_{n \leq x} \sum_{d \leq x} 1_{[d \mid n]} 1_{[d \mid P(z)]} \mu(d).
\tag{1}$$
Here, I use a form of Iverson Bracket notation of the form
$$ 1_{[\text{condition}]} = \begin{cases} 1 & \text{condition is true,} \\ 0 & \text{else.} \end{cases}$$
In this form, the two regions of summation are independent, and so we can swap the order of summation in $(1)$ without problem. So
$$
\sum_{n \leq x} \sum_{d \leq x} 1_{[d \mid n]} 1_{[d \mid P(z)]} \mu(d) =
\sum_{d \leq x} \mu(d) 1_{[d \mid P(z)]} \sum_{n \leq x} 1_{[d \mid n]}  \mu(d) =
\sum_{\substack{d \leq x \\ d \mid P(z)}} \mu(d)
\sum_{\substack{n \leq x \\ d \mid n}} 1,
$$
as the author claimed.
