#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
scalar_t__ calloc(int, int);
int msort(int *, int /*<<< orphan*/, int, int *);
int /*<<< orphan*/ printf(char *, ...);
long long rank(int *, int *, int *, int);
long long rank2(int *, int *, int *, int);
int /*<<< orphan*/ scanf(char *, int *, ...);
int main(void) {
  int t;
  scanf("%d", &t);
  int a;
  int *fact = (int *)calloc(100001, 4);
  fact[0] = 1;
  for (a = 1; a < 100001; a++)
    fact[a] = (fact[a - 1] * (long long)a) % 1000000007;
  for (a = 0; a < t; a++) {
    int n, k;
    scanf("%d %d", &n, &k);
    int p[n], q[n], dup[n];
    int countp[n], countq[n];
    int nextp[n], nextq[n];
    int i;
    int prev, first;
    scanf("%d", &prev);
    first = p[0] = prev;
    for (i = 1; i < n; i++) {
      scanf("%d", p + i);
      nextp[prev - 1] = p[i];
      prev = p[i];
    }
    nextp[prev - 1] = first;
    countp[0] = countq[0] = 0;
    scanf("%d", &prev);
    first = q[0] = dup[0] = prev;
    for (i = 1; i < n; i++) {
      scanf("%d", q + i);
      nextq[prev - 1] = dup[i] = q[i];
      countp[i] = countq[i] = 0;
      prev = q[i];
    }
    nextq[prev - 1] = first;
    if (k == n) {
      int flag = 1;
      for (i = 0; i < n; i++) {
        if (nextp[i] != nextq[i]) {
          flag = 0;
          break;
        }
      }
      if (flag == 1)
        printf("%d\n", q[0]);
      else
        printf("-1\n");
    } else {
      int parityq = msort(dup, 0, n - 1, countq);
      if (k % 2 == 0) {
        long long int r = rank(q, countq, fact, n);
        printf("%lld\n", r);
      } else {
        int parityp = msort(p, 0, n - 1, countp);
        if (parityp == parityq) {
          long long int m = rank2(q, countq, fact, n);
          printf("%lld\n", m);
        } else
          printf("-1\n");
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1080 <scanf@plt>
mov    $0x186a1,%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x1,-0xc(%rbp)
cmpl   $0x186a1,-0xc(%rbp)
jge    11f1 <main+0x91>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movslq (%rax,%rcx,4),%rax
movslq -0xc(%rbp),%rcx
imul   %rcx,%rax
mov    $0x3b9aca07,%ecx
cqto
idiv   %rcx
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11a8 <main+0x48>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1653 <main+0x4f3>
lea    0xdf5(%rip),%rdi
xor    %eax,%eax
lea    -0x1c(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1080 <scanf@plt>
mov    -0x1c(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x98(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xc8(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xc0(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x1c(%rbp),%eax
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xb8(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
mov    -0x1c(%rbp),%eax
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xb0(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
mov    -0x1c(%rbp),%eax
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xa8(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x58(%rbp)
mov    -0x1c(%rbp),%eax
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xa0(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x60(%rbp)
lea    0xce6(%rip),%rdi
lea    -0x68(%rbp),%rsi
mov    $0x0,%al
call   1080 <scanf@plt>
mov    -0x98(%rbp),%rcx
mov    -0x68(%rbp),%eax
mov    %eax,(%rcx)
mov    %eax,-0x6c(%rbp)
movl   $0x1,-0x64(%rbp)
mov    -0x64(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    13a3 <main+0x243>
mov    -0x98(%rbp),%rsi
movslq -0x64(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xca0(%rip),%rdi
mov    $0x0,%al
call   1080 <scanf@plt>
mov    -0xa8(%rbp),%rcx
mov    -0x98(%rbp),%rax
movslq -0x64(%rbp),%rdx
mov    (%rax,%rdx,4),%esi
mov    -0x68(%rbp),%edx
sub    $0x1,%edx
movslq %edx,%rdx
mov    %esi,(%rcx,%rdx,4)
movslq -0x64(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x68(%rbp)
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    133e <main+0x1de>
mov    -0xb8(%rbp),%rax
mov    -0xb0(%rbp),%rcx
mov    -0xa8(%rbp),%rdx
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%esi
sub    $0x1,%esi
movslq %esi,%rsi
mov    %edi,(%rdx,%rsi,4)
movl   $0x0,(%rcx)
movl   $0x0,(%rax)
lea    0xc29(%rip),%rdi
lea    -0x68(%rbp),%rsi
mov    $0x0,%al
call   1080 <scanf@plt>
mov    -0xc0(%rbp),%rdx
mov    -0xc8(%rbp),%rcx
mov    -0x68(%rbp),%eax
mov    %eax,(%rdx)
mov    %eax,(%rcx)
mov    %eax,-0x6c(%rbp)
movl   $0x1,-0x64(%rbp)
mov    -0x64(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    149e <main+0x33e>
mov    -0xc8(%rbp),%rsi
movslq -0x64(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xbda(%rip),%rdi
mov    $0x0,%al
call   1080 <scanf@plt>
mov    -0xc0(%rbp),%rdi
mov    -0xa0(%rbp),%rsi
mov    -0xb0(%rbp),%rdx
mov    -0xb8(%rbp),%rcx
mov    -0xc8(%rbp),%rax
movslq -0x64(%rbp),%r8
mov    (%rax,%r8,4),%r8d
movslq -0x64(%rbp),%r9
mov    %r8d,(%rdi,%r9,4)
mov    -0x68(%rbp),%edi
sub    $0x1,%edi
movslq %edi,%rdi
mov    %r8d,(%rsi,%rdi,4)
movslq -0x64(%rbp),%rsi
movl   $0x0,(%rdx,%rsi,4)
movslq -0x64(%rbp),%rdx
movl   $0x0,(%rcx,%rdx,4)
movslq -0x64(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x68(%rbp)
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    1404 <main+0x2a4>
mov    -0xa0(%rbp),%rax
mov    -0x6c(%rbp),%edx
mov    -0x68(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    1554 <main+0x3f4>
movl   $0x1,-0x70(%rbp)
movl   $0x0,-0x64(%rbp)
mov    -0x64(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    151b <main+0x3bb>
mov    -0xa0(%rbp),%rcx
mov    -0xa8(%rbp),%rax
movslq -0x64(%rbp),%rdx
mov    (%rax,%rdx,4),%eax
movslq -0x64(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
je     1508 <main+0x3a8>
movl   $0x0,-0x70(%rbp)
jmp    151b <main+0x3bb>
jmp    150d <main+0x3ad>
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    14ce <main+0x36e>
cmpl   $0x1,-0x70(%rbp)
jne    1541 <main+0x3e1>
mov    -0xc8(%rbp),%rax
mov    (%rax),%esi
lea    0xad1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    154f <main+0x3ef>
lea    0xac2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    163e <main+0x4de>
mov    -0xb0(%rbp),%rcx
mov    -0xc0(%rbp),%rdi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1040 <msort@plt>
mov    %eax,-0x74(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    15bb <main+0x45b>
mov    -0xb0(%rbp),%rsi
mov    -0xc8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <rank@plt>
mov    %rax,-0x80(%rbp)
mov    -0x80(%rbp),%rsi
lea    0xa5f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1639 <main+0x4d9>
mov    -0xb8(%rbp),%rcx
mov    -0x98(%rbp),%rdi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1040 <msort@plt>
mov    %eax,-0x84(%rbp)
mov    -0x84(%rbp),%eax
cmp    -0x74(%rbp),%eax
jne    1626 <main+0x4c6>
mov    -0xb0(%rbp),%rsi
mov    -0xc8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1070 <rank2@plt>
mov    %rax,-0x90(%rbp)
mov    -0x90(%rbp),%rsi
lea    0x9f4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1634 <main+0x4d4>
lea    0x9dd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1639 <main+0x4d9>
jmp    163e <main+0x4de>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11f8 <main+0x98>
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
lea    0xe7b(%rip),%rdi
lea    -0x48(%rbp),%rsi
xor    %eax,%eax
call   1090 <scanf@plt>
mov    $0x186a1,%edi
mov    $0x4,%esi
call   1070 <calloc@plt>
mov    %rax,%r14
movl   $0x1,(%rax)
mov    $0x1,%eax
mov    $0x2,%esi
movabs $0x89705f3112a28fe5,%rdi
5f 70 89
nop
lea    -0x1(%rsi),%rcx
imul   %rax,%rcx
mov    %rcx,%rax
imul   %rdi
add    %rcx,%rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x1d,%rdx
add    %rax,%rdx
imul   $0x3b9aca07,%rdx,%rax
sub    %rax,%rcx
mov    %ecx,-0x4(%r14,%rsi,4)
imul   %rsi,%rcx
mov    %rcx,%rax
imul   %rdi
add    %rcx,%rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x1d,%rdx
add    %rax,%rdx
imul   $0x3b9aca07,%rdx,%rax
sub    %rax,%rcx
mov    %ecx,(%r14,%rsi,4)
add    $0x2,%rsi
mov    %rcx,%rax
cmp    $0x186a2,%rsi
jne    11c0 <main+0x50>
cmpl   $0x0,-0x48(%rbp)
jle    1501 <main+0x391>
lea    0xdcb(%rip),%r12
xor    %eax,%eax
mov    %r14,-0x68(%rbp)
jmp    1267 <main+0xf7>
mov    (%r15),%esi
lea    0xdbc(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    -0x34(%rbp),%eax
mov    -0x40(%rbp),%rcx
mov    %rcx,%rsp
add    $0x1,%eax
cmp    -0x48(%rbp),%eax
jge    1501 <main+0x391>
mov    %eax,-0x34(%rbp)
lea    0xd8f(%rip),%rdi
lea    -0x2c(%rbp),%rsi
lea    -0x44(%rbp),%rdx
xor    %eax,%eax
call   1090 <scanf@plt>
mov    %rsp,-0x40(%rbp)
mov    -0x2c(%rbp),%eax
mov    %rsp,%r14
lea    0xf(,%rax,4),%rbx
and    $0xfffffffffffffff0,%rbx
mov    %r14,%rax
sub    %rbx,%rax
mov    %rax,-0x70(%rbp)
mov    %rax,%rsp
neg    %rbx
mov    -0x2c(%rbp),%eax
mov    %rsp,%rcx
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x90(%rbp)
mov    %rcx,%rsp
mov    %rsp,%rcx
sub    %rax,%rcx
mov    %rcx,-0x80(%rbp)
mov    %rcx,%rsp
mov    %rsp,%r13
sub    %rax,%r13
mov    %r13,%rsp
mov    %rsp,%rcx
sub    %rax,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rsp,%r15
sub    %rax,%r15
mov    %r15,%rsp
mov    %rsp,%rcx
sub    %rax,%rcx
mov    %rcx,-0x78(%rbp)
mov    %rcx,%rsp
mov    %r12,%rdi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
call   1090 <scanf@plt>
mov    -0x30(%rbp),%eax
mov    %eax,(%r14,%rbx,1)
cmpl   $0x2,-0x2c(%rbp)
mov    %eax,-0x50(%rbp)
mov    %r12,%r14
jl     135c <main+0x1ec>
mov    -0x70(%rbp),%rax
lea    0x4(%rax),%rbx
mov    $0x1,%r12d
nopl   (%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1090 <scanf@plt>
mov    (%rbx),%eax
movslq -0x30(%rbp),%rcx
mov    %eax,-0x4(%r15,%rcx,4)
mov    %eax,-0x30(%rbp)
add    $0x1,%r12
movslq -0x2c(%rbp),%rcx
add    $0x4,%rbx
cmp    %rcx,%r12
jl     1330 <main+0x1c0>
cltq
mov    %r15,-0x88(%rbp)
mov    -0x50(%rbp),%ecx
mov    %ecx,-0x4(%r15,%rax,4)
mov    -0x58(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,0x0(%r13)
mov    %r14,%rdi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
call   1090 <scanf@plt>
mov    -0x30(%rbp),%ecx
mov    -0x80(%rbp),%rdi
mov    %ecx,(%rdi)
mov    -0x90(%rbp),%r15
mov    %ecx,(%r15)
mov    -0x2c(%rbp),%edx
mov    %ecx,%eax
cmp    $0x2,%edx
mov    %r14,%r12
jl     142e <main+0x2be>
mov    %ecx,-0x5c(%rbp)
mov    $0x1,%ebx
mov    $0x4,%r12d
mov    %r13,-0x50(%rbp)
mov    -0x58(%rbp),%r13
mov    -0x78(%rbp),%r14
nopl   0x0(%rax,%rax,1)
lea    (%r15,%r12,1),%rsi
lea    0xc28(%rip),%rdi
xor    %eax,%eax
call   1090 <scanf@plt>
mov    -0x80(%rbp),%rdi
mov    (%r15,%r12,1),%eax
mov    %eax,(%rdi,%r12,1)
movslq -0x30(%rbp),%rcx
mov    %eax,-0x4(%r14,%rcx,4)
movl   $0x0,0x0(%r13,%r12,1)
mov    -0x50(%rbp),%rcx
movl   $0x0,(%rcx,%r12,1)
mov    %eax,-0x30(%rbp)
add    $0x1,%rbx
movslq -0x2c(%rbp),%rdx
add    $0x4,%r12
cmp    %rdx,%rbx
jl     13d0 <main+0x260>
lea    0xbdc(%rip),%r12
mov    -0x50(%rbp),%r13
mov    -0x5c(%rbp),%ecx
cltq
mov    -0x78(%rbp),%rbx
mov    %ecx,-0x4(%rbx,%rax,4)
cmp    %edx,-0x44(%rbp)
jne    1470 <main+0x300>
test   %edx,%edx
mov    -0x88(%rbp),%rsi
jle    1240 <main+0xd0>
mov    %edx,%eax
xor    %ecx,%ecx
mov    (%rsi,%rcx,4),%edx
cmp    (%rbx,%rcx,4),%edx
jne    14f0 <main+0x380>
add    $0x1,%rcx
cmp    %rcx,%rax
jne    1450 <main+0x2e0>
jmp    1240 <main+0xd0>
nopw   0x0(%rax,%rax,1)
add    $0xffffffff,%edx
xor    %esi,%esi
mov    -0x58(%rbp),%rbx
mov    %rbx,%rcx
call   1050 <msort@plt>
mov    -0x2c(%rbp),%ecx
testb  $0x1,-0x44(%rbp)
jne    14a0 <main+0x330>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    -0x68(%rbp),%rdx
call   1040 <rank@plt>
jmp    14cd <main+0x35d>
nopl   0x0(%rax,%rax,1)
mov    %eax,%r14d
add    $0xffffffff,%ecx
mov    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    %ecx,%edx
mov    %r13,%rcx
call   1050 <msort@plt>
cmp    %r14d,%eax
jne    14f0 <main+0x380>
mov    -0x2c(%rbp),%ecx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    -0x68(%rbp),%rdx
call   1080 <rank2@plt>
lea    0xb36(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1251 <main+0xe1>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xb19(%rip),%rdi
call   1030 <puts@plt>
jmp    1251 <main+0xe1>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [6 x i8] c"%d %d\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [4 x i8] c"-1\0A\00"
@.str.4 = [6 x i8] c"%lld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i64
%21 = alloca i32
%22 = alloca i64
store i32 0, i32* %1
%23 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%24 = call i64 @calloc(i32 100001, i32 4)
%25 = inttoptr i64 %24 to i32*
store i32* %25, i32** %4
%26 = load i32*, i32** %4
%27 = getelementptr inbounds i32, i32* %26, i64 0
store i32 1, i32* %27
store i32 1, i32* %3
br label %28
28: ; preds = %48, %0
%29 = load i32, i32* %3
%30 = icmp slt i32 %29, 100001
br i1 %30, label %31, label %51
31: ; preds = %28
%32 = load i32*, i32** %4
%33 = load i32, i32* %3
%34 = sub nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %32, i64 %35
%37 = load i32, i32* %36
%38 = sext i32 %37 to i64
%39 = load i32, i32* %3
%40 = sext i32 %39 to i64
%41 = mul nsw i64 %38, %40
%42 = srem i64 %41, 1000000007
%43 = trunc i64 %42 to i32
%44 = load i32*, i32** %4
%45 = load i32, i32* %3
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %44, i64 %46
store i32 %43, i32* %47
br label %48
48: ; preds = %31
%49 = load i32, i32* %3
%50 = add nsw i32 %49, 1
store i32 %50, i32* %3
br label %28
51: ; preds = %28
store i32 0, i32* %3
br label %52
52: ; preds = %223, %51
%53 = load i32, i32* %3
%54 = load i32, i32* %2
%55 = icmp slt i32 %53, %54
br i1 %55, label %56, label %226
56: ; preds = %52
%57 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %5, i32* %6)
%58 = load i32, i32* %5
%59 = zext i32 %58 to i64
%60 = call i8* @llvm.stacksave()
store i8* %60, i8** %7
%61 = alloca i32, i64 %59
store i64 %59, i64* %8
%62 = load i32, i32* %5
%63 = zext i32 %62 to i64
%64 = alloca i32, i64 %63
store i64 %63, i64* %9
%65 = load i32, i32* %5
%66 = zext i32 %65 to i64
%67 = alloca i32, i64 %66
store i64 %66, i64* %10
%68 = load i32, i32* %5
%69 = zext i32 %68 to i64
%70 = alloca i32, i64 %69
store i64 %69, i64* %11
%71 = load i32, i32* %5
%72 = zext i32 %71 to i64
%73 = alloca i32, i64 %72
store i64 %72, i64* %12
%74 = load i32, i32* %5
%75 = zext i32 %74 to i64
%76 = alloca i32, i64 %75
store i64 %75, i64* %13
%77 = load i32, i32* %5
%78 = zext i32 %77 to i64
%79 = alloca i32, i64 %78
store i64 %78, i64* %14
%80 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %16)
%81 = load i32, i32* %16
%82 = getelementptr inbounds i32, i32* %61, i64 0
store i32 %81, i32* %82
store i32 %81, i32* %17
store i32 1, i32* %15
br label %83
83: ; preds = %104, %56
%84 = load i32, i32* %15
%85 = load i32, i32* %5
%86 = icmp slt i32 %84, %85
br i1 %86, label %87, label %107
87: ; preds = %83
%88 = load i32, i32* %15
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds i32, i32* %61, i64 %89
%91 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %90)
%92 = load i32, i32* %15
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds i32, i32* %61, i64 %93
%95 = load i32, i32* %94
%96 = load i32, i32* %16
%97 = sub nsw i32 %96, 1
%98 = sext i32 %97 to i64
%99 = getelementptr inbounds i32, i32* %76, i64 %98
store i32 %95, i32* %99
%100 = load i32, i32* %15
%101 = sext i32 %100 to i64
%102 = getelementptr inbounds i32, i32* %61, i64 %101
%103 = load i32, i32* %102
store i32 %103, i32* %16
br label %104
104: ; preds = %87
%105 = load i32, i32* %15
%106 = add nsw i32 %105, 1
store i32 %106, i32* %15
br label %83
107: ; preds = %83
%108 = load i32, i32* %17
%109 = load i32, i32* %16
%110 = sub nsw i32 %109, 1
%111 = sext i32 %110 to i64
%112 = getelementptr inbounds i32, i32* %76, i64 %111
store i32 %108, i32* %112
%113 = getelementptr inbounds i32, i32* %73, i64 0
store i32 0, i32* %113
%114 = getelementptr inbounds i32, i32* %70, i64 0
store i32 0, i32* %114
%115 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %16)
%116 = load i32, i32* %16
%117 = getelementptr inbounds i32, i32* %67, i64 0
store i32 %116, i32* %117
%118 = getelementptr inbounds i32, i32* %64, i64 0
store i32 %116, i32* %118
store i32 %116, i32* %17
store i32 1, i32* %15
br label %119
119: ; preds = %149, %107
%120 = load i32, i32* %15
%121 = load i32, i32* %5
%122 = icmp slt i32 %120, %121
br i1 %122, label %123, label %152
123: ; preds = %119
%124 = load i32, i32* %15
%125 = sext i32 %124 to i64
%126 = getelementptr inbounds i32, i32* %64, i64 %125
%127 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %126)
%128 = load i32, i32* %15
%129 = sext i32 %128 to i64
%130 = getelementptr inbounds i32, i32* %64, i64 %129
%131 = load i32, i32* %130
%132 = load i32, i32* %15
%133 = sext i32 %132 to i64
%134 = getelementptr inbounds i32, i32* %67, i64 %133
store i32 %131, i32* %134
%135 = load i32, i32* %16
%136 = sub nsw i32 %135, 1
%137 = sext i32 %136 to i64
%138 = getelementptr inbounds i32, i32* %79, i64 %137
store i32 %131, i32* %138
%139 = load i32, i32* %15
%140 = sext i32 %139 to i64
%141 = getelementptr inbounds i32, i32* %73, i64 %140
store i32 0, i32* %141
%142 = load i32, i32* %15
%143 = sext i32 %142 to i64
%144 = getelementptr inbounds i32, i32* %70, i64 %143
store i32 0, i32* %144
%145 = load i32, i32* %15
%146 = sext i32 %145 to i64
%147 = getelementptr inbounds i32, i32* %64, i64 %146
%148 = load i32, i32* %147
store i32 %148, i32* %16
br label %149
149: ; preds = %123
%150 = load i32, i32* %15
%151 = add nsw i32 %150, 1
store i32 %151, i32* %15
br label %119
152: ; preds = %119
%153 = load i32, i32* %17
%154 = load i32, i32* %16
%155 = sub nsw i32 %154, 1
%156 = sext i32 %155 to i64
%157 = getelementptr inbounds i32, i32* %79, i64 %156
store i32 %153, i32* %157
%158 = load i32, i32* %6
%159 = load i32, i32* %5
%160 = icmp eq i32 %158, %159
br i1 %160, label %161, label %191
161: ; preds = %152
store i32 1, i32* %18
store i32 0, i32* %15
br label %162
162: ; preds = %178, %161
%163 = load i32, i32* %15
%164 = load i32, i32* %5
%165 = icmp slt i32 %163, %164
br i1 %165, label %166, label %181
166: ; preds = %162
%167 = load i32, i32* %15
%168 = sext i32 %167 to i64
%169 = getelementptr inbounds i32, i32* %76, i64 %168
%170 = load i32, i32* %169
%171 = load i32, i32* %15
%172 = sext i32 %171 to i64
%173 = getelementptr inbounds i32, i32* %79, i64 %172
%174 = load i32, i32* %173
%175 = icmp ne i32 %170, %174
br i1 %175, label %176, label %177
176: ; preds = %166
store i32 0, i32* %18
br label %181
177: ; preds = %166
br label %178
178: ; preds = %177
%179 = load i32, i32* %15
%180 = add nsw i32 %179, 1
store i32 %180, i32* %15
br label %162
181: ; preds = %176, %162
%182 = load i32, i32* %18
%183 = icmp eq i32 %182, 1
br i1 %183, label %184, label %188
184: ; preds = %181
%185 = getelementptr inbounds i32, i32* %64, i64 0
%186 = load i32, i32* %185
%187 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %186)
br label %190
188: ; preds = %181
%189 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %190
190: ; preds = %188, %184
br label %221
191: ; preds = %152
%192 = load i32, i32* %5
%193 = sub nsw i32 %192, 1
%194 = call i32 @msort(i32* %67, i32 0, i32 %193, i32* %73)
store i32 %194, i32* %19
%195 = load i32, i32* %6
%196 = srem i32 %195, 2
%197 = icmp eq i32 %196, 0
br i1 %197, label %198, label %204
198: ; preds = %191
%199 = load i32*, i32** %4
%200 = load i32, i32* %5
%201 = call i64 @rank(i32* %64, i32* %73, i32* %199, i32 %200)
store i64 %201, i64* %20
%202 = load i64, i64* %20
%203 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i64 %202)
br label %220
204: ; preds = %191
%205 = load i32, i32* %5
%206 = sub nsw i32 %205, 1
%207 = call i32 @msort(i32* %61, i32 0, i32 %206, i32* %70)
store i32 %207, i32* %21
%208 = load i32, i32* %21
%209 = load i32, i32* %19
%210 = icmp eq i32 %208, %209
br i1 %210, label %211, label %217
211: ; preds = %204
%212 = load i32*, i32** %4
%213 = load i32, i32* %5
%214 = call i64 @rank2(i32* %64, i32* %73, i32* %212, i32 %213)
store i64 %214, i64* %22
%215 = load i64, i64* %22
%216 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i64 %215)
br label %219
217: ; preds = %204
%218 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %219
219: ; preds = %217, %211
br label %220
220: ; preds = %219, %198
br label %221
221: ; preds = %220, %190
%222 = load i8*, i8** %7
br label %223
223: ; preds = %221
%224 = load i32, i32* %3
%225 = add nsw i32 %224, 1
store i32 %225, i32* %3
br label %52
226: ; preds = %52
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [6 x i8] c"%d %d\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.4 = [6 x i8] c"%lld\0A\00"
@str.5 = [3 x i8] c"-1\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %1 to i8*
%6 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%7 = call i64 @calloc(i32 100001, i32 4)
%8 = inttoptr i64 %7 to i32*
store i32 1, i32* %8
br label %16
9: ; preds = %16
%10 = load i32, i32* %1
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %138
12: ; preds = %9
%13 = bitcast i32* %2 to i8*
%14 = bitcast i32* %3 to i8*
%15 = bitcast i32* %4 to i8*
br label %30
16: ; preds = %16, %0
%17 = phi i64 [ 1, %0 ], [ %25, %16 ]
%18 = phi i64 [ 1, %0 ], [ %28, %16 ]
%19 = mul nsw i64 %17, %18
%20 = srem i64 %19, 1000000007
%21 = trunc i64 %20 to i32
%22 = getelementptr inbounds i32, i32* %8, i64 %18
store i32 %21, i32* %22
%23 = add nuw nsw i64 %18, 1
%24 = mul nsw i64 %20, %23
%25 = srem i64 %24, 1000000007
%26 = trunc i64 %25 to i32
%27 = getelementptr inbounds i32, i32* %8, i64 %23
store i32 %26, i32* %27
%28 = add nuw nsw i64 %18, 2
%29 = icmp eq i64 %28, 100001
br i1 %29, label %9, label %16
30: ; preds = %12, %134
%31 = phi i32 [ 0, %12 ], [ %135, %134 ]
%32 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %2, i32* %3)
%33 = load i32, i32* %2
%34 = zext i32 %33 to i64
%35 = call i8* @llvm.stacksave()
%36 = alloca i32, i64 %34
%37 = load i32, i32* %2
%38 = zext i32 %37 to i64
%39 = alloca i32, i64 %38
%40 = alloca i32, i64 %38
%41 = alloca i32, i64 %38
%42 = alloca i32, i64 %38
%43 = alloca i32, i64 %38
%44 = alloca i32, i64 %38
%45 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
%46 = load i32, i32* %4
store i32 %46, i32* %36
%47 = load i32, i32* %2
%48 = icmp sgt i32 %47, 1
br i1 %48, label %49, label %62
49: ; preds = %30, %49
%50 = phi i64 [ %58, %49 ], [ 1, %30 ]
%51 = getelementptr inbounds i32, i32* %36, i64 %50
%52 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %51)
%53 = load i32, i32* %51
%54 = load i32, i32* %4
%55 = add nsw i32 %54, -1
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %43, i64 %56
store i32 %53, i32* %57
store i32 %53, i32* %4
%58 = add nuw nsw i64 %50, 1
%59 = load i32, i32* %2
%60 = sext i32 %59 to i64
%61 = icmp slt i64 %58, %60
br i1 %61, label %49, label %62
62: ; preds = %49, %30
%63 = phi i32 [ %46, %30 ], [ %53, %49 ]
%64 = add nsw i32 %63, -1
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i32, i32* %43, i64 %65
store i32 %46, i32* %66
store i32 0, i32* %42
store i32 0, i32* %41
%67 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
%68 = load i32, i32* %4
store i32 %68, i32* %40
store i32 %68, i32* %39
%69 = load i32, i32* %2
%70 = icmp sgt i32 %69, 1
br i1 %70, label %71, label %87
71: ; preds = %62, %71
%72 = phi i64 [ %83, %71 ], [ 1, %62 ]
%73 = getelementptr inbounds i32, i32* %39, i64 %72
%74 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %73)
%75 = load i32, i32* %73
%76 = getelementptr inbounds i32, i32* %40, i64 %72
store i32 %75, i32* %76
%77 = load i32, i32* %4
%78 = add nsw i32 %77, -1
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i32, i32* %44, i64 %79
store i32 %75, i32* %80
%81 = getelementptr inbounds i32, i32* %42, i64 %72
store i32 0, i32* %81
%82 = getelementptr inbounds i32, i32* %41, i64 %72
store i32 0, i32* %82
store i32 %75, i32* %4
%83 = add nuw nsw i64 %72, 1
%84 = load i32, i32* %2
%85 = sext i32 %84 to i64
%86 = icmp slt i64 %83, %85
br i1 %86, label %71, label %87
87: ; preds = %71, %62
%88 = phi i32 [ %68, %62 ], [ %75, %71 ]
%89 = phi i32 [ %69, %62 ], [ %84, %71 ]
%90 = add nsw i32 %88, -1
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds i32, i32* %44, i64 %91
store i32 %68, i32* %92
%93 = load i32, i32* %3
%94 = icmp eq i32 %93, %89
br i1 %94, label %95, label %114
95: ; preds = %87
%96 = icmp sgt i32 %89, 0
br i1 %96, label %97, label %109
97: ; preds = %95
%98 = zext i32 %89 to i64
br label %101
99: ; preds = %101
%100 = icmp eq i64 %108, %98
br i1 %100, label %109, label %101
101: ; preds = %97, %99
%102 = phi i64 [ 0, %97 ], [ %108, %99 ]
%103 = getelementptr inbounds i32, i32* %43, i64 %102
%104 = load i32, i32* %103
%105 = getelementptr inbounds i32, i32* %44, i64 %102
%106 = load i32, i32* %105
%107 = icmp eq i32 %104, %106
%108 = add nuw nsw i64 %102, 1
br i1 %107, label %99, label %112
109: ; preds = %99, %95
%110 = load i32, i32* %39
%111 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %110)
br label %134
112: ; preds = %101
%113 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str.5, i64 0, i64 0))
br label %134
114: ; preds = %87
%115 = add nsw i32 %89, -1
%116 = call i32 @msort(i32* %40, i32 0, i32 %115, i32* %42)
%117 = load i32, i32* %3
%118 = and i32 %117, 1
%119 = icmp eq i32 %118, 0
%120 = load i32, i32* %2
br i1 %119, label %121, label %124
121: ; preds = %114
%122 = call i64 @rank(i32* %39, i32* %42, i32* %8, i32 %120)
%123 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i64 %122)
br label %134
124: ; preds = %114
%125 = add nsw i32 %120, -1
%126 = call i32 @msort(i32* %36, i32 0, i32 %125, i32* %41)
%127 = icmp eq i32 %126, %116
br i1 %127, label %128, label %132
128: ; preds = %124
%129 = load i32, i32* %2
%130 = call i64 @rank2(i32* %39, i32* %42, i32* %8, i32 %129)
%131 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i64 %130)
br label %134
132: ; preds = %124
%133 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str.5, i64 0, i64 0))
br label %134
134: ; preds = %121, %132, %128, %109, %112
%135 = add nuw nsw i32 %31, 1
%136 = load i32, i32* %1
%137 = icmp slt i32 %135, %136
br i1 %137, label %30, label %138
138: ; preds = %134, %9
ret i32 0
}