// File: /workspace/github_cpp_makefile_star2_O3/vividvilla[P]hackerrank/src/grid-walking.cpp
// Function: getWays(std::vector<int, std::allocator<int>>, std::vector<int, std::allocator<int>>, int)
int getWays(vector<int> x, vector<int> d, int M) {
    int N = d.size();

    long long md[M + 1][N + 1];
    for (int i = 0; i < N; i++) {
        long long m[d[i] + 1][M + 1];
        int D = d[i];

        for (int j = 1; j <= D; j++)
            m[j][0] = 1;

        for (int n = 1; n <= M; n++) {
            for (int j = 1; j <= D; j++) {
                m[j][n] = 0;
                if (j - 1 > 0)
                    m[j][n] = (m[j][n] + m[j - 1][n - 1]) % MOD;
                if (j + 1 <= D)
                    m[j][n] = (m[j][n] + m[j + 1][n - 1]) % MOD;
            }
        }

        md[0][i + 1] = 1;
        for (int j = 1; j <= M; j++)
            md[j][i + 1] = m[x[i]][j];
    }

    long long c[M + 1][M + 1];
    for (int i = 0; i <= M; i++) {
        c[i][0] = 1;
        c[i][i] = 1;
    }
    for (int i = 1; i <= M; i++)
        for (int j = 1; j < i; j++)
        c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % MOD;

    long long mdt[M + 1][N + 1];
    for (int i = 0; i <= M; i++)
        mdt[i][1] = md[i][1];
    for (int i = 0; i <= N; i++)
        mdt[0][i] = 1;

    for (int i = 2; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            mdt[j][i] = 0;
            for (int k = 0; k <= j; k++)
                mdt[j][i] =
                    (mdt[j][i] +
                     ((c[j][j - k] *
                       ((mdt[k][i - 1] * md[j - k][i]) % MOD)) % MOD)) % MOD;
        }
    }

    return mdt[M][N];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %edx, %r15d
movq %rdi, -0x58(%rbp)
movq 0x8(%rsi), %rax
movq %rsi, -0x68(%rbp)
subq (%rsi), %rax
movq %rax, -0x90(%rbp)
movq %rax, %rcx
sarq $0x2, %rcx
leal 0x1(%r15), %r12d
leal 0x1(%rcx), %eax
movq %rax, -0x60(%rbp)
imulq %r12, %rax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
movq %rsp, %r11
movq %rax, -0x30(%rbp)
subq %rax, %r11
movq %r11, %rsp
movabsq $-0x768fa0ceed5d701b, %r14 # imm = 0x89705F3112A28FE5
movq %rcx, -0x38(%rbp)
testl %ecx, %ecx
movq %r15, -0x80(%rbp)
movq %r12, -0x88(%rbp)
jle 0x150b
movq -0x38(%rbp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, -0x78(%rbp)
movq %r12, %rbx
shlq $0x4, %rbx
leaq (,%r12,8), %r9
movq -0x60(%rbp), %rax
leaq (,%rax,8), %r10
leaq (%r11,%rax,8), %rax
addq $0x8, %rax
movq %rax, -0x40(%rbp)
xorl %ecx, %ecx
movdqa 0xd0c(%rip), %xmm0      # 0x2010
movdqa 0xd14(%rip), %xmm1      # 0x2020
pcmpeqd %xmm2, %xmm2
movq %r11, -0x48(%rbp)
movq %rsp, %r8
movq -0x68(%rbp), %rdx
movq (%rdx), %rax
movl (%rax,%rcx,4), %eax
incl %eax
imulq %r12, %rax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
movq %rsp, %rsi
subq %rax, %rsi
movq %rsi, -0x50(%rbp)
movq %rsi, %rsp
movq (%rdx), %rax
movslq (%rax,%rcx,4), %r13
testq %r13, %r13
jle 0x13d6
leal 0x1(%r13), %eax
andl $-0x2, %eax
leaq -0x1(%r13), %rdx
movq %rdx, %xmm3
pshufd $0x44, %xmm3, %xmm3     # xmm3 = xmm3[0,1,0,1]
pxor %xmm1, %xmm3
movq -0x50(%rbp), %rdx
xorl %esi, %esi
movq %rsi, %xmm4
pshufd $0x44, %xmm4, %xmm4     # xmm4 = xmm4[0,1,0,1]
por %xmm0, %xmm4
pxor %xmm1, %xmm4
movdqa %xmm4, %xmm5
pcmpgtd %xmm3, %xmm5
pcmpeqd %xmm3, %xmm4
pshufd $0xf5, %xmm4, %xmm6     # xmm6 = xmm4[1,1,3,3]
pand %xmm5, %xmm6
pshufd $0xf5, %xmm5, %xmm4     # xmm4 = xmm5[1,1,3,3]
por %xmm6, %xmm4
movd %xmm4, %edi
notl %edi
testb $0x1, %dil
je 0x13b3
movq $0x1, (%rdx,%r12,8)
pxor %xmm2, %xmm4
pextrw $0x4, %xmm4, %edi
testb $0x1, %dil
je 0x13ca
movq $0x1, (%rdx,%rbx)
addq $0x2, %rsi
addq %rbx, %rdx
cmpq %rsi, %rax
jne 0x136f
testl %r15d, %r15d
jle 0x14e6
movq %r8, -0x70(%rbp)
leal 0x1(%r13), %r11d
movl $0x1, %edi
movq -0x50(%rbp), %rsi
testl %r13d, %r13d
jle 0x1479
movl $0x1, %r12d
movq %rsi, %r15
movq $0x0, 0x8(%r15,%r9)
cmpq $0x2, %r12
jb 0x143c
movq (%r15), %r8
movq %r8, %rax
imulq %r14
addq %r8, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x1d, %rdx
addq %rax, %rdx
imulq $0x3b9aca07, %rdx, %rax # imm = 0x3B9ACA07
subq %rax, %r8
movq %r8, 0x8(%r15,%r9)
jmp 0x143f
xorl %r8d, %r8d
cmpq %r13, %r12
jge 0x146e
addq (%r15,%rbx), %r8
movq %r8, %rax
imulq %r14
addq %r8, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x1d, %rdx
addq %rax, %rdx
imulq $0x3b9aca07, %rdx, %rax # imm = 0x3B9ACA07
subq %rax, %r8
movq %r8, 0x8(%r15,%r9)
incq %r12
addq %r9, %r15
cmpq %r12, %r11
jne 0x1402
incq %rdi
addq $0x8, %rsi
movq -0x88(%rbp), %r12
cmpq %r12, %rdi
jne 0x13f0
leaq 0x1(%rcx), %rax
movq -0x48(%rbp), %r11
movq $0x1, 0x8(%r11,%rcx,8)
movq -0x80(%rbp), %r15
testl %r15d, %r15d
jle 0x14f7
movq -0x58(%rbp), %rdx
movq (%rdx), %rdx
movslq (%rdx,%rcx,4), %rdx
imulq %r12, %rdx
movq -0x50(%rbp), %rsi
leaq (%rsi,%rdx,8), %rdx
movq -0x40(%rbp), %rsi
leaq (%rsi,%rcx,8), %rcx
movl $0x1, %esi
movq -0x70(%rbp), %r8
movq (%rdx,%rsi,8), %rdi
movq %rdi, (%rcx)
incq %rsi
addq %r10, %rcx
cmpq %rsi, %r12
jne 0x14d2
jmp 0x14fb
movq $0x1, 0x8(%r11,%rcx,8)
incq %rcx
movq %rcx, %rax
jmp 0x14fb
movq -0x70(%rbp), %r8
movq %r8, %rsp
movq %rax, %rcx
cmpq -0x78(%rbp), %rax
jne 0x1314
movq %r12, %rax
imulq %r12, %rax
movq %rsp, %r9
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r9
movq %r9, %rsp
testl %r15d, %r15d
js 0x15cc
leaq (,%r12,8), %rcx
leaq 0x8(,%r12,8), %rax
movl $0x1, %edx
movq %r12, %rsi
movq %r9, %rdi
movq %r9, %r8
movq %rdx, (%rdi)
movq %rdx, (%r8)
addq %rax, %r8
addq %rcx, %rdi
decq %rsi
jne 0x154e
testl %r15d, %r15d
sete -0x40(%rbp)
je 0x15e0
movq %r11, %r13
leaq 0x8(%r9), %rdi
movl $0x1, %r8d
xorl %r10d, %r10d
movq %r10, %r11
movq %rdi, %rbx
cmpq $0x2, %r8
jb 0x15b9
movq (%rbx), %rsi
addq -0x8(%rbx), %rsi
movq %rsi, %rax
imulq %r14
addq %rsi, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x1d, %rdx
addq %rax, %rdx
imulq $0x3b9aca07, %rdx, %rax # imm = 0x3B9ACA07
subq %rax, %rsi
movq %rsi, (%rbx,%rcx)
addq $0x8, %rbx
decq %r11
jne 0x1584
incq %r8
addq %rcx, %rdi
incq %r10
cmpq %r12, %r8
jne 0x1578
movq %r13, %r11
jmp 0x15e4
movq %rsp, %rax
subq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq %rax, %rsp
movb $0x1, -0x40(%rbp)
jmp 0x161b
movb $0x1, -0x40(%rbp)
movq %rsp, %rdi
subq -0x30(%rbp), %rdi
movq %rdi, %rsp
testl %r15d, %r15d
movq %rdi, -0x30(%rbp)
js 0x161b
movq -0x60(%rbp), %rax
leaq (,%rax,8), %rax
movl $0x8, %ecx
movq %r12, %rdx
movq (%r11,%rcx), %rsi
movq %rsi, (%rdi,%rcx)
addq %rax, %rcx
decq %rdx
jne 0x160b
cmpl $0x0, -0x38(%rbp)
js 0x183e
movq -0x38(%rbp), %rax
leal 0x1(%rax), %r10d
leaq 0x1(%r10), %rax
andq $-0x2, %rax
leaq -0x1(%r10), %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x9c5(%rip), %xmm1      # 0x2010
xorl %ecx, %ecx
movdqa 0x9cb(%rip), %xmm2      # 0x2020
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x9cb(%rip), %xmm4      # 0x2030
movq -0x30(%rbp), %rsi
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %edx
notl %edx
testb $0x1, %dl
je 0x16a2
movq $0x1, (%rsi,%rcx,8)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %edx
testb $0x1, %dl
je 0x16b9
movq $0x1, 0x8(%rsi,%rcx,8)
addq $0x2, %rcx
paddq %xmm4, %xmm1
cmpq %rcx, %rax
jne 0x1669
cmpl $0x2, -0x38(%rbp)
jl 0x183e
movq -0x38(%rbp), %rax
incl %eax
movq %rax, -0x38(%rbp)
leaq 0x8(,%r12,8), %rax
movq %rax, -0x78(%rbp)
leaq (%r9,%r12,8), %rax
addq $0x8, %rax
movq %rax, -0x98(%rbp)
leaq (%r11,%r10,8), %rax
addq $0x10, %rax
movq %rax, -0x58(%rbp)
shlq $0x3, %r10
movq %r10, %r15
negq %r15
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x68(%rbp)
movl $0x2, %eax
movq %rax, -0x48(%rbp)
cmpb $0x0, -0x40(%rbp)
jne 0x181f
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
leaq (%rax,%rcx,8), %r9
movl $0x2, %r8d
movl $0x1, %eax
movq -0x58(%rbp), %rbx
movq -0x98(%rbp), %rdi
movq -0x60(%rbp), %rcx
movq %rax, -0x50(%rbp)
imulq %rax, %rcx
movq $0x0, (%r9,%rcx,8)
movq -0x68(%rbp), %r13
movq %rbx, -0x70(%rbp)
xorl %r12d, %r12d
xorl %r11d, %r11d
movq (%rbx), %rsi
imulq (%r13), %rsi
movq %rsi, %rax
imulq %r14
addq %rsi, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x1d, %rdx
addq %rax, %rdx
imulq $0x3b9aca07, %rdx, %rax # imm = 0x3B9ACA07
subq %rax, %rsi
imulq (%rdi,%r12,8), %rsi
movq %rsi, %rax
imulq %r14
addq %rsi, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x1d, %rdx
addq %rax, %rdx
imulq $0x3b9aca07, %rdx, %rax # imm = 0x3B9ACA07
subq %rax, %rsi
addq %r11, %rsi
movq %rsi, %rax
imulq %r14
addq %rsi, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x1d, %rdx
addq %rax, %rdx
imulq $0x3b9aca07, %rdx, %rax # imm = 0x3B9ACA07
subq %rax, %rsi
decq %r12
addq %r15, %rbx
movq %rsi, (%r9,%rcx,8)
addq %r10, %r13
movq %r8, %rax
movq %rsi, %r11
addq %r12, %rax
jne 0x176e
movq -0x50(%rbp), %rax
incq %rax
incq %r8
addq -0x78(%rbp), %rdi
movq -0x70(%rbp), %rbx
addq %r10, %rbx
cmpq -0x88(%rbp), %rax
jne 0x174c
movq -0x48(%rbp), %rcx
incq %rcx
addq $0x8, -0x58(%rbp)
addq $0x8, -0x68(%rbp)
movq %rcx, -0x48(%rbp)
cmpq -0x38(%rbp), %rcx
jne 0x1720
movslq -0x80(%rbp), %rax
movq -0x60(%rbp), %rcx
imulq %rax, %rcx
movq -0x30(%rbp), %rax
leaq (%rax,%rcx,8), %rax
movq -0x90(%rbp), %rcx
shlq $0x1e, %rcx
sarq $0x1d, %rcx
movl (%rcx,%rax), %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq