Let me copy here [an answer][1] from Russian forum dxdy.ru that I obtained using the approach outlined in [my paper][2].

Two given rows of a $3\times N$ matrix define a permutation of order $N$. Let $c_i$ ($i=1,2,\dots,N$) be the number of cycles of length $i$ in this permutation (in particular, $c_1$ is the number of fixed points, which is 0 iff given permutations form a [derangement][3]).

Then the number of different third rows that form derangements with respect to each of the first two rows equals
$$\sum_{j=0}^n (-1)^j\cdot (n-j)!\cdot [z^j]\ F(z),$$
where $[z^j]$ is the operator of taking the coefficient of $z^j$ and
$$F(z) = (1+z)^{c_1}\cdot \prod_{i=2}^n \left( \left(\frac{1+\sqrt{1+4z}}2\right)^{2i} + \left(\frac{1-\sqrt{1+4z}}2\right)^{2i} \right)^{c_i}.$$

Particular cases:

 - For $c_1=n$ (i.e., two given rows are equal), we get just the number derangements.
 - For $c_n=1$, we get menage numbers [A000179][4](n).
 - For $n=2m$ and $c_2=m$, we get [A000316][5](m) = [A000459][6](m)$\cdot 2^m$.

This question inspired me to add the following new sequences to the OEIS:  [A277256][7], [A277257][8], and [A277265][9].

  [1]: http://dxdy.ru/post1157940.html#p1157940
  [2]: https://arxiv.org/abs/1510.07926
  [3]: https://en.wikipedia.org/wiki/Derangement
  [4]: http://oeis.org/A000179
  [5]: http://oeis.org/A000316
  [6]: http://oeis.org/A000459
  [7]: http://oeis.org/A277256
  [8]: http://oeis.org/A277257
  [9]: http://oeis.org/A277265