How many right triangles can be built from points
There is an $\mathcal{O}(N^2\log N)$-time algorithm, coming from an ability to compute, for each point $P$, the number of right angles at $P$, in $\mathcal{O}(N\log N)$ time. This can be done by considering the (other) points in the polar coordinate system with the origin at $P$, and sorting by polar angle, after which the counting is easy. (Actually there's no need to compute the polar angles - this is just an idea to be used indirectly.)