Codeforces Round #631 (Div. 2) - Thanks, Denis aramis Shitov!

Problem : https://codeforces.com/contest/1330/problem/B

Submission: https://codeforces.com/contest/1330/submission/75419195

I’m try to compute sum of valid permutations from both sides and then checking whether these two permutations are immediate to each other.

I’d like to know whether my logic is correct. If not, how should I fix it? TIA.