I don't have time to analyze the proof in Diamond-Shurman, but for what it is worth, here is my own treatment as I teach it to my students.

**Theorem.** The cusps $\frac{u_1}{v_1},\frac{u_2}{v_2}\in\mathbb{Q}\cup\{\infty\}$, given in lowest terms,
are equivalent under $\Gamma_0(q)$ if and only if there exists $v\mid q$ such that
$(q,v_1)=(q,v_2)=v$ and $u_1v_1\equiv u_2v_2\pmod{(q,v^2)}$.

**Proof.** Fixing arbitrary elements
$\begin{pmatrix}u_1&\overline v_1 \cr v_1&\overline u_1\end{pmatrix},
\begin{pmatrix}u_2&\overline v_2 \cr v_2&\overline u_2\end{pmatrix}\in\mathrm{SL}_2(\mathbb{Z})$ we need to examine the
statement
$$ \exists \gamma\in\Gamma_0(q) : \begin{pmatrix}u_1&\overline v_1 \cr v_1&\overline u_1\end{pmatrix}\infty
=\gamma\begin{pmatrix}u_2&\overline v_2 \cr v_2&\overline u_2\end{pmatrix}\infty. $$
This is clearly
\begin{align*}
&\Longleftrightarrow \exists \gamma\in\Gamma_0(q) : \exists  n\in\mathbb{Z} : \begin{pmatrix}1&n \cr 0&1\end{pmatrix}=
\begin{pmatrix}u_1&\overline v_1 \cr v_1&\overline u_1\end{pmatrix}^{-1}
\gamma\begin{pmatrix}u_2&\overline v_2 \cr v_2&\overline u_2\end{pmatrix} \cr
&\Longleftrightarrow \exists  n\in\mathbb{Z} :
\begin{pmatrix}u_1&\overline v_1 \cr v_1&\overline u_1\end{pmatrix}
\begin{pmatrix}1&n \cr 0&1\end{pmatrix}
\begin{pmatrix}u_2&\overline v_2 \cr v_2&\overline u_2\end{pmatrix}^{-1}\in\Gamma_0(q) \cr
&\Longleftrightarrow \exists  n\in\mathbb{Z} :
\begin{pmatrix}u_1&\overline v_1+nu_1 \cr v_1&\overline u_1+nv_1\end{pmatrix}
\begin{pmatrix}\overline u_2&-\overline v_2 \cr -v_2&u_2\end{pmatrix}\in\Gamma_0(q) \cr
&\Longleftrightarrow \exists  n\in\mathbb{Z} :
\overline u_2 v_1-\overline u_1 v_2-nv_1v_2\equiv 0\pmod{q} \cr
&\Longleftrightarrow \exists  m,n\in\mathbb{Z} :
\overline u_2 v_1-\overline u_1 v_2=qm+nv_1v_2 \cr  
&\Longleftrightarrow \overline u_2 v_1\equiv\overline u_1 v_2\pmod{(q,v_1v_2)}.
\end{align*}
Let us examine the last condition. Observe that by $(\overline u_1,v_1)=(\overline u_2,v_2)=1$
the congruence forces $(q,v_1)\mid v_2$ and $(q,v_2)\mid v_1$, i.e. $(q,v_1)=(q,v_2)$. Denoting this common value by $v$ and writing $q=vw$, the congruence
becomes $\overline u_2 v_1/v\equiv\overline u_1 v_2/v\pmod{(w,v_1v_2/v)}$.
Note that $(v_1/v,q/v)=(v_1,q)/v=1$ and $(v_2/v,q/v)=(v_2,q)/v=1$, i.e. both
$v_1/v$ and $v_2/v$ are coprime to $w$. In particular, $v_1v_2/v=v(v_1/v)(v_2/v)$
shows that $(w,v_1v_2/v)=(w,v)$ and the congruence further simplifies to
$\overline u_2 v_1/v\equiv\overline u_1 v_2/v\pmod{(w,v)}$. Note also that $u_1\overline u_1$ and $u_2\overline u_2$
are $\equiv 1\pmod{v}$, hence the congruence is equivalent to
$u_1 v_1/v\equiv u_2 v_2/v\pmod{(w,v)}$. Multiplying this by $v$ we obtain the congruence
in the theorem.

**Corollary.** The number of inequivalent cusps of $\Gamma_0(q)$ equals $\sum_{q=vw}\varphi((v,w))$.

**Proof.** For any decomposition $q=vw$ and any reduced residue class $u'\bmod(v,w)$
we pick some $u\in\mathbb{Z}$ such that $(u,v)=1$ and $u\equiv u'\pmod{(v,w)}$. This exists by the
Chinese remainder theorem, e.g. we can take $u\in\mathbb{Z}$ such that $u\equiv 1\pmod{p}$ for any prime $p\mid v$
with $p\nmid w$ and also $u\equiv u'\pmod{(v,w)}$. By the above Theorem (or its proof), the resulting rational numbers $\frac{u}{v}$ represent the $\Gamma_0(q)$-orbits of $\mathbb{Q}\cup\{\infty\}$, and their number equals
$\sum_{q=vw}\varphi((v,w))$.