0

CJam, 73 bytes
0ri:Re!Rm*{:Tz0=R,=[R,Te_]m!{~ff{T==}e_}/=&},{:T,e!{:PPff{T==P#}}%$}%Q|,+
The time complexity of the above code is worse than O(n!n).
Input n=4 is already to much for the online interpreter.
Using the Java interpreter, input n=5 could be possible, if you have enough RAM and pa...