Let's say that three segments on a plane form a {\it K-shaped figure} if:
\begin{shortitems}
\item two of them share a common endpoint;
\item this common endpoint lies strictly inside the third segment;
\item these two segments are located on the same side with respect to the third one;
\item all three segments are pairwise not collinear.
\end{shortitems} 

\begin{center}
  \begin{tabular}{c|c}
    \includegraphics{kShaped-1.eps} & \includegraphics{kShaped-2.eps}\\
    \small{Valid K-shaped figures} & \small{Invalid K-shaped figures}
  \end{tabular}
\end{center}

You are given a collection of $n$ segments on the plane. Find the number of triples of segments from this collection that form a K-shaped figure.