From (REF )
\(K\leqslant \# \lbrace  (d_1,d_2)\in {\mathcal {D}}:~d_1-d_2=f\rbrace .\) 
If \(d_1,d_2\in {\mathcal {D}}\)  satisfy \(d_1-d_2=f\) , then
\(d_1^2-d_2^2 - f^2 = (d_1-d_2)^2 + 2d_1d_2 - 2d_2^2 - f^2= 2d_2 (d_1-d_2) = 2d_2 f\) 
and
some algebraic manipulations show
\((2jd_1^2-2jd_2^2-2jf^2)^2=8j f^2(2j d_2^2).\) 
Since \(0\notin {\mathcal {D}}\) , for each \(d\in {\mathcal {D}}\) , by (REF ) and [1]} there exists \(m_d,n_d\)  satisfying
\(\begin{split}&2jd^2\equiv m_d^{-1}n_d \bmod {q}, \qquad |n_d|\ll \frac{N^2}{\Delta ^2}, \\& |m_d|\ll \frac{N}{\Delta ^2}, \qquad \gcd (m_d,n_d)=1.\end{split}\) 
Let \(I(f)\)  count the number of solutions to the congruence
\(\left(n_{d_1}m_{d_1}^{-1}-n_{d_2}m_{d_2}^{-1}-2jf^2\right)^2\equiv 8jf^2n_{d_2}m_{d_2}^{-1} \bmod {q},\) 
with \(d_1,d_2\in {\mathcal {D}}\) .
The above and (REF ) imply
\(K\leqslant I(f).\) 
Rearranging (REF ) we obtain
\(\left(m_{d_2}n_{d_1}-m_{d_1}n_{d_2} -2jf^2m_{d_1}m_{d_2}\right)^2\equiv 8jf^2m_{d_1}^2m_{d_2}n_{d_2} \bmod {q}.\) 
This implies that \(I(f)\)  is bounded by the number of solutions to
\(\begin{split}(n_{d_1}m_{d_2}-n_{d_2}m_{d_1})^2 & -4jf^2m_{d_1}m_{d_2}(n_{d_1}m_{d_2}+n_{d_2}m_{d_1})\\& \qquad \qquad +4j^2f^4(m_{d_1}m_{d_2})^2\equiv 0 \bmod {q},\end{split}\) 
with \(d_1,d_2\in {\mathcal {D}}\) . Let \({\mathcal {L}}\)  denote the lattice
\({\mathcal {L}}=\lbrace  (m,n,\ell )\in \mathbb {Z}^{3}:~ m+njf^2+\ell j^2f^4\equiv 0 \bmod {q}\rbrace ,\) 
and \(B\)  the convex body
\(B=\left\lbrace  (x,y,z)\in \mathbb {R}^3:~ |x|\leqslant \frac{CN^6}{\Delta ^8}, \ |y|\leqslant \frac{CN^5}{\Delta ^8}, \ |z|\leqslant \frac{CN^4}{\Delta ^8}\right\rbrace .\) 
for a suitable absolute constant \(C\) . By (REF ) and (REF )
\(\begin{split}\bigl ((n_{d_1}m_{d_2}-n_{d_2}m_{d_1})^2, -4m_{d_1}m_{d_2}(n_{d_1}m_{d_2}+n_{d_2}m_{d_1}),&\\4(m_{d_1}m_{d_2})^2\bigr )&\in {\mathcal {L}}\cap B.\end{split}\) 
Let \(\lambda _1,\lambda _2\)  denote the first and second successive minima of \({\mathcal {L}}\)  with respect to \(B\) . Assuming that \(K\geqslant 1\)  we have \(\lambda _1\leqslant 1\) .
Suppose that
\(\lambda _1\leqslant 1, \quad \lambda _2>1.\) 
Then there exists some \((a_0,b_0,c_0)\in {\mathcal {L}}\cap B\)  such that for any \(d_1,d_2\in {\mathcal {D}}\)  satisfying (REF ) we have
\(\left((n_{d_1}m_{d_2}-n_{d_2}m_{d_1})^2, -4m_{d_1}m_{d_2}(n_{d_1}m_{d_2}+n_{d_2}m_{d_1}),(m_{d_1}m_{d_2})^2\right)&\\=m(a_0,b_0,&c_0),\) 

for some \(m\in \mathbb {Z}\) . Note from (REF ) for each \(d_1,d_2\in {\mathcal {D}}\)  we have \(m_{d_1}m_{d_2}\ne 0\)  and hence \(c_0\ne 0\) . This implies
\(& \left(\frac{n_{d_1}}{m_{d_1}}-\frac{n_{d_2}}{m_{d_2}}\right)^2=\frac{a_0}{c_0},\\& \frac{n_{d_1}}{m_{d_1}}+\frac{n_{d_2}}{m_{d_2}}=\frac{b_0}{c_0}.\) 

Hence
\(K\leqslant \#\biggl \lbrace  (d_1,d_2)\in {\mathcal {D}}\times {\mathcal {D}}: ~ \frac{n_{d_1}}{m_{d_1}}-\frac{n_{d_2}}{m_{d_2}}&=\pm \left(\frac{a_0}{c_0}\right)^{1/2}, \\& \frac{n_{d_1}}{m_{d_1}}+\frac{n_{d_2}}{m_{d_2}}=\frac{b_0}{c_0} \biggr \rbrace  \leqslant 4,\) 

since once \(n_{d_1}/m_{d_1}\)  is fixed, due to the coprimality condition
in (REF ), \(d^2_1\)  is uniquely defined, and similarly for \(d^2_2\) .
This implies (REF ).
Suppose next that
\(\lambda _1\leqslant 1, \quad \lambda _2\leqslant 1.\) 
Let \(J(\ell ,m,n)\)  count the number of solutions to
\(m_{1}m_{2}=\ell , \quad n_{1}m_{2}+n_{2}m_{1}=m, \quad n_{1}m_{2}-n_{2}m_{1}=n,\) 
with
\(|m_1|, |m_2|\ll \frac{N}{\Delta ^2}, \quad |n_1|,|n_2|\ll \frac{N^2}{\Delta ^2}, \quad \quad m_1m_2n_1n_2\ne 0,\) 
so that
\(I(f)\ll \sum _{\begin{array}{c}|m|,|n|\leqslant CN^3/\Delta ^4 \\ |\ell |\leqslant CN^2/\Delta ^4 \\ 4j^2f^4\ell ^2 -4jf^2\ell m+ n^2 \equiv 0 \bmod {q}\end{array}}J(\ell ,m,n),\) 
for some absolute constant \(C\) . We next show that
\(J(\ell ,m,n)=N^{o(1)}.\) 
Estimates for the divisor function imply the number of solutions to
\(m_1m_2=\ell , \quad \text{$m_1,m_2$ satisfying~(\ref {eq:n12m12})},\) 
is at most \(N^{o(1)}\) . For each such \(m_1,m_2\)  there exists at most one solution to the system
\(n_1m_2-n_2m_1=n, \quad n_1m_2+n_2m_1=m, \quad \text{$n_1,n_2$ satisfying~(\ref {eq:n12m12})},\) 
which establishes (REF ). By (REF ) and (REF )
\(K \leqslant \# \lbrace (\ell ,m,n) \in \mathbb {Z}^3&:~ |\ell |\leqslant CN^2/\Delta ^4 , \ |m|,|n| \leqslant CN^3/\Delta ^4, \ \\ & \quad n^2-4jf^2\ell m+4j^2f^4\ell ^2\equiv 0 \bmod {q} \rbrace  N^{o(1)},\) 

and hence
\(\begin{split}K\leqslant \# \Bigl \lbrace (\ell ,m,n) \in \mathbb {Z}^3&:\\|\ell | \leqslant 2CN^2/\Delta ^4, &\ |m|\leqslant 4C^2N^5/\Delta ^8, \ |n| \leqslant CN^3/\Delta ^4, \\ &n^2+jf^2m+j^2f^4\ell ^2\equiv 0 \bmod {q}\Bigr \rbrace  N^{o(1)}.\end{split}\) 
By (REF ), for each \(\ell ,n \in \mathbb {Z}\) , there exists at most one value of \(|m|\ll N^5/\Delta ^8\)  satisfying
\(n^2+jf^2m+j^2f^4\ell ^2\equiv 0 \bmod {q}.\) 
For any \((\ell _1,m_1,n_1)\)  and \((\ell _2,m_2,n_2)\)  satisfying the conditions of (REF ), there exists some \(|m|\ll N^5/\Delta ^8\)  such that
\(n_1^2+n_2^2-2jf^2m+j^2f^4(\ell ^2_1+\ell _2^2)\equiv 0 \bmod {q}.\) 
Define the lattice
\({\mathcal {L}}=\lbrace  (n,m,\ell )\in \mathbb {Z}^3 : ~n+jf^2m +j^2f^4\ell \equiv 0 \bmod {q} \rbrace ,\) 
and the convex body
\(B=\lbrace  (n,m,\ell )\in \mathbb {R}^3 :~ |n|&\leqslant C_0N^6/\Delta ^4, \\& |m|\leqslant C_0N^5/\Delta ^8, \ |\ell |\leqslant C_0N^4/\Delta ^8 \rbrace ,\) 

for a suitable constant \(C_0\) . Since for any integer \(r\)
\(\# \lbrace  n_1,n_2\in \mathbb {Z}:~ n_1^2+n_2^2=r\rbrace  \leqslant r^{o(1)},\) 
we see that (REF ) implies
\(K^2\leqslant \# \left({\mathcal {L}}\cap B\right) N^{o(1)}.\) 
By (REF ), (REF ) and Lemma REF , there exists \((\ell ,m,n)\ne (0,0,0)\)  satisfying
\(|\ell |\leqslant \frac{N^{11+o(1)}}{\Delta ^{16}K^2}, \qquad |m|\leqslant \frac{N^{10+o(1)}}{\Delta ^{16}K^2}, \qquad |n|\leqslant \frac{N^{9+o(1)}}{\Delta ^{16}K^2},\) 
and
\(jf^2n\equiv m \bmod {q}, \quad j^2f^4n\equiv \ell \bmod {q}.\) 
Note we may assume
\(\gcd (\ell , m,n)=1.\) 
Recall (REF )
\(\begin{split}I(f)\leqslant \#\lbrace  (d_1,d_2) & \in {\mathcal {D}}^2 :~ (n_{d_1}m_{d_2}-n_{d_2}m_{d_1})^2\\& -4jf^2m_{d_1}m_{d_2}(n_{d_1}m_{d_2}+n_{d_2}m_{d_1}) \\& \qquad \qquad +4j^2f^4(m_{d_1}m_{d_2})^2\equiv 0 \bmod {q}\rbrace .\end{split}\) 
If \(d_1,d_2\)  satisfy the conditions in (REF ), then by (REF )
\(& n(n_{d_1}m_{d_2}-n_{d_2}m_{d_1})^2-4mm_{d_1}m_{d_2}(n_{d_1}m_{d_2}+n_{d_2}m_{d_1}) \\ & \quad \qquad \qquad \qquad \qquad \qquad \qquad +4\ell (m_{d_1}m_{d_2})^2\equiv 0 \bmod {q},\) 

and hence from (REF ), assuming that \(N\)  is large enough, we derive
\(\begin{split}n(n_{d_1}m_{d_2}-n_{d_2}m_{d_1})^2-4mm_{d_1}m_{d_2}(n_{d_1}&m_{d_2}+n_{d_2}m_{d_1}) \\ &+4\ell (m_{d_1}m_{d_2})^2=0.\end{split}\) 
Similarly by (REF ) and (REF ) we have \(m^2\equiv n\ell \bmod {q}\)  and again (REF ) ensures that
\(m^2=n\ell .\) 
Therefore (REF ) implies the following equation
\(\left(\frac{n_{d_1}}{m_{d_1}}-\frac{n_{d_2}}{m_{d_2}}\right)^2-4\left(\frac{n_{d_1}}{m_{d_1}}+\frac{n_{d_2}}{m_{d_2}}\right)\left(\frac{m}{n}\right)+4\left(\frac{m}{n}\right)^{2}=0.\) 
We see that
\(\frac{m}{n}=\frac{1}{2}\left(\frac{n_{d_1}}{m_{d_1}}+\frac{n_{d_2}}{m_{d_2}}\right)\pm \frac{\sqrt{n_{d_1}m_{d_1}n_{d_2}m_{d_2}}}{m_{d_1}m_{d_2}}.\) 
Hence from (REF ) and (REF ), there exists some constant \(C\)  such that
\(I(f)&\leqslant \#\biggl \lbrace  \left(m_{d_1},m_{d_2},n_{d_1},n_{d_2} \right)\in \mathbb {Z}^4:\\& \qquad \qquad |m_{d_1}|,|m_{d_2}|\leqslant \frac{CN}{\Delta ^2}, \ |n_{d_1}|,|n_{d_2}|\leqslant \frac{CN^2}{\Delta ^2},\\ & \qquad \qquad \qquad \qquad m_{d_1}m_{d_2}n_{d_1}n_{d_2}\ne 0,\ \text{and~(\ref {eq:nm123123123}) holds} \biggr \rbrace .\) 

Summing the above over \(f\in {\mathcal {F}}\) , using (REF ) and noting that for each \(\ell ,m, n\)  satisfying (REF ) there exists \(O(1)\)  values of \(f\)  satisfying (REF ), we see that \(K\# {\mathcal {F}}\)  is bounded by the number of solutions to the equation (REF ) with integer variables satisfying
\(|m_{d_1}|,|m_{d_2}|\leqslant \frac{CN}{\Delta ^2}, \qquad |n_{d_1}|,|n_{d_2}|\leqslant \frac{CN^2}{\Delta ^2}, \qquad n_{d_1}n_{d_2}m_{d_1}m_{d_2}\ne 0.\) 
We see from (REF )
that \(n_{d_1}m_{d_1}n_{d_2}m_{d_2}=r^2\)  for some \(r\in \mathbb {Z}\)  and hence a bound on the divisor function,
see [2]}, implies
\(K\#{\mathcal {F}}\leqslant N^{o(1)}\#\left\lbrace  \ell \leqslant C^4 \frac{N^6}{\Delta ^8} :~\ell =r^2 \ \text{for some $r \in \mathbb {Z}$}\right\rbrace \leqslant \frac{N^{3+o(1)}}{\Delta ^4},\) 
which completes the proof.
