// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movq %rdi, -0x40(%rbp)
movq 0x8(%rsi), %rax
movq %rsi, -0x78(%rbp)
subq (%rsi), %rax
movq %rax, -0x90(%rbp)
movq %rax, %rcx
sarq $0x2, %rcx
movq %rdx, -0x38(%rbp)
leal 0x1(%rdx), %r12d
leal 0x1(%rcx), %eax
movq %rax, -0x50(%rbp)
imulq %r12, %rax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
movq %rsp, %r15
movq %rax, -0x70(%rbp)
subq %rax, %r15
movq %r15, %rsp
movabsq $-0x768fa0ceed5d701b, %r14 # imm = 0x89705F3112A28FE5
movq %rcx, -0x30(%rbp)
testl %ecx, %ecx
movq %r12, -0x88(%rbp)
jle 0x14e8
movq -0x30(%rbp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, -0x60(%rbp)
movq %r12, %rbx
shlq $0x4, %rbx
leaq (,%r12,8), %r9
movq -0x50(%rbp), %rax
leaq (,%rax,8), %r10
leaq (%r15,%rax,8), %r8
addq $0x8, %r8
xorl %edi, %edi
movdqa 0xd13(%rip), %xmm0      # 0x2010
movdqa 0xd1b(%rip), %xmm1      # 0x2020
pcmpeqd %xmm2, %xmm2
movq %r15, -0x48(%rbp)
movq %rsp, -0x68(%rbp)
movq -0x78(%rbp), %rcx
movq (%rcx), %rax
movl (%rax,%rdi,4), %eax
incl %eax
imulq %r12, %rax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
movq %rsp, %rdx
subq %rax, %rdx
movq %rdx, -0x58(%rbp)
movq %rdx, %rsp
movq (%rcx), %rax
movslq (%rax,%rdi,4), %r15
testq %r15, %r15
jle 0x13d0
leal 0x1(%r15), %eax
andl $-0x2, %eax
leaq -0x1(%r15), %rcx
movq %rcx, %xmm3
pshufd $0x44, %xmm3, %xmm3     # xmm3 = xmm3[0,1,0,1]
pxor %xmm1, %xmm3
movq -0x58(%rbp), %rcx
xorl %edx, %edx
movq %rdx, %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, %esi
notl %esi
testb $0x1, %sil
je 0x13ad
movq $0x1, (%rcx,%r12,8)
pxor %xmm2, %xmm4
pextrw $0x4, %xmm4, %esi
testb $0x1, %sil
je 0x13c4
movq $0x1, (%rcx,%rbx)
addq $0x2, %rdx
addq %rbx, %rcx
cmpq %rdx, %rax
jne 0x1369
movq %r8, -0x80(%rbp)
cmpl $0x0, -0x38(%rbp)
jle 0x148b
leal 0x1(%r15), %r11d
movl $0x1, %esi
movq -0x58(%rbp), %r13
testl %r15d, %r15d
jle 0x1474
movl $0x1, %r12d
movq %r13, %rcx
movq $0x0, 0x8(%rcx,%r9)
cmpq $0x2, %r12
jb 0x1435
movq (%rcx), %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(%rcx,%r9)
cmpq %r15, %r12
jge 0x1469
movq (%rcx,%rbx), %r8
addq 0x8(%rcx,%r9), %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(%rcx,%r9)
incq %r12
addq %r9, %rcx
cmpq %r12, %r11
jne 0x13fd
incq %rsi
addq $0x8, %r13
movq -0x88(%rbp), %r12
cmpq %r12, %rsi
jne 0x13eb
movq -0x48(%rbp), %r15
movq $0x1, 0x8(%r15,%rdi,8)
cmpl $0x0, -0x38(%rbp)
movq -0x80(%rbp), %r8
jle 0x14d3
movq -0x40(%rbp), %rax
movq (%rax), %rax
movslq (%rax,%rdi,4), %rax
imulq %r12, %rax
movq -0x58(%rbp), %rcx
leaq (%rcx,%rax,8), %rax
movl $0x1, %ecx
movq %r8, %rdx
movq (%rax,%rcx,8), %rsi
movq %rsi, (%rdx)
incq %rcx
addq %r10, %rdx
cmpq %rcx, %r12
jne 0x14c1
movq -0x68(%rbp), %rsp
incq %rdi
addq $0x8, %r8
cmpq -0x60(%rbp), %rdi
jne 0x130d
movq %r12, %rax
imulq %r12, %rax
movq %rsp, %r8
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r8
movq %r8, %rsp
cmpl $0x0, -0x38(%rbp)
js 0x1539
leaq (,%r12,8), %rax
leaq 0x8(,%r12,8), %rcx
movl $0x1, %edx
movq %r12, %rsi
movq %r8, %rdi
movq %r8, %r9
movq %rdx, (%rdi)
movq %rdx, (%r9)
addq %rcx, %r9
addq %rax, %rdi
decq %rsi
jne 0x1528
cmpl $0x0, -0x38(%rbp)
jle 0x15a3
leaq 0x8(%r8), %rcx
leaq (,%r12,8), %rdi
movl $0x1, %r9d
xorl %r10d, %r10d
movq %r10, %r11
movq %rcx, %rbx
cmpq $0x2, %r9
jb 0x1595
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,%rdi)
addq $0x8, %rbx
decq %r11
jne 0x1560
incq %r9
addq %rdi, %rcx
incq %r10
cmpq %r12, %r9
jne 0x1554
movq %rsp, %rdi
subq -0x70(%rbp), %rdi
movq %rdi, %rsp
cmpl $0x0, -0x38(%rbp)
js 0x15d7
movq -0x50(%rbp), %rax
leaq (,%rax,8), %rax
movl $0x8, %ecx
movq %r12, %rdx
movq (%r15,%rcx), %rsi
movq %rsi, (%rdi,%rcx)
addq %rax, %rcx
decq %rdx
jne 0x15c7
cmpl $0x0, -0x30(%rbp)
js 0x167c
movq -0x30(%rbp), %rax
leal 0x1(%rax), %ecx
leaq 0x1(%rcx), %rax
andq $-0x2, %rax
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0xa0b(%rip), %xmm1      # 0x2010
xorl %ecx, %ecx
movdqa 0xa11(%rip), %xmm2      # 0x2020
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0xa11(%rip), %xmm4      # 0x2030
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 0x1658
movq $0x1, (%rdi,%rcx,8)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %edx
testb $0x1, %dl
je 0x166f
movq $0x1, 0x8(%rdi,%rcx,8)
addq $0x2, %rcx
paddq %xmm4, %xmm1
cmpq %rcx, %rax
jne 0x161f
movq %rdi, -0x40(%rbp)
cmpl $0x2, -0x30(%rbp)
jl 0x17f9
movq -0x30(%rbp), %rax
incl %eax
movq %rax, -0x30(%rbp)
leaq 0x8(,%r12,8), %rax
movq %rax, -0x78(%rbp)
leaq (%r8,%r12,8), %rax
addq $0x8, %rax
movq %rax, -0x70(%rbp)
movq -0x50(%rbp), %rax
leaq (,%rax,8), %r9
leaq (%r15,%rax,8), %rax
addq $0x10, %rax
movq %rax, -0x60(%rbp)
movq %r9, %rbx
negq %rbx
movq -0x40(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x68(%rbp)
movl $0x2, %eax
movq %rax, -0x48(%rbp)
cmpl $0x0, -0x38(%rbp)
jle 0x17da
movq -0x40(%rbp), %rax
movq -0x48(%rbp), %rcx
leaq (%rax,%rcx,8), %r8
movl $0x2, %edi
movl $0x1, %eax
movq -0x60(%rbp), %r10
movq -0x70(%rbp), %r13
movq -0x50(%rbp), %rcx
movq %rax, -0x58(%rbp)
imulq %rax, %rcx
movq $0x0, (%r8,%rcx,8)
movq -0x68(%rbp), %r15
movq %r10, -0x80(%rbp)
xorl %r12d, %r12d
xorl %r11d, %r11d
movq (%r10), %rsi
imulq (%r15), %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 (%r13,%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 %rbx, %r10
movq %rsi, (%r8,%rcx,8)
addq %r9, %r15
movq %rdi, %rax
movq %rsi, %r11
addq %r12, %rax
jne 0x1729
movq -0x58(%rbp), %rax
incq %rax
incq %rdi
addq -0x78(%rbp), %r13
movq -0x80(%rbp), %r10
addq %r9, %r10
cmpq -0x88(%rbp), %rax
jne 0x1707
movq -0x48(%rbp), %rcx
incq %rcx
addq $0x8, -0x60(%rbp)
addq $0x8, -0x68(%rbp)
movq %rcx, -0x48(%rbp)
cmpq -0x30(%rbp), %rcx
jne 0x16df
movslq -0x38(%rbp), %rax
movq -0x50(%rbp), %rcx
imulq %rax, %rcx
movq -0x40(%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