// File: /workspace/github_cpp_makefile_star2_O2/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
movq 0x8(%rsi), %rax
movq %rsi, -0x60(%rbp)
subq (%rsi), %rax
movq %rax, -0x90(%rbp)
movq %rax, %rsi
shrq $0x2, %rsi
leal 0x1(%rdx), %ebx
leal 0x1(%rsi), %ecx
movq %rcx, %rax
imulq %rbx, %rax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
movq %rsp, %r14
movq %rax, -0x48(%rbp)
subq %rax, %r14
movq %r14, %rsp
xorl %r8d, %r8d
testl %edx, %edx
movl $0x0, %eax
movq %rdx, -0x88(%rbp)
cmovgl %edx, %eax
testl %esi, %esi
movl $0x0, %edx
movq %rsi, -0x50(%rbp)
cmovgl %esi, %edx
movq %rdx, -0x68(%rbp)
movq %rax, -0x58(%rbp)
incl %eax
movq %rax, -0x80(%rbp)
leaq (,%rbx,8), %r13
movq %rbx, -0x30(%rbp)
shlq $0x4, %rbx
leaq (,%rcx,8), %r11
movq %rcx, -0x70(%rbp)
leaq (%r14,%rcx,8), %r9
addq $0x8, %r9
movl $0x3b9aca07, %r15d      # imm = 0x3B9ACA07
movq %r14, -0x98(%rbp)
cmpq -0x68(%rbp), %r8
je 0x1411
movq %rsp, -0x38(%rbp)
movq -0x60(%rbp), %rcx
movq (%rcx), %rax
movl (%rax,%r8,4), %eax
incl %eax
imulq -0x30(%rbp), %rax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
movq %rsp, %r10
subq %rax, %r10
movq %r10, %rsp
movq (%rcx), %rax
movslq (%rax,%r8,4), %rsi
testl %esi, %esi
movl $0x0, %ecx
cmovgl %esi, %ecx
leaq (%r10,%r13), %rax
movq %rcx, %rdx
subq $0x1, %rdx
jb 0x134c
movq $0x1, (%rax)
addq %r13, %rax
jmp 0x133a
movq %r9, -0x40(%rbp)
movq %r8, -0x78(%rbp)
negq %rcx
movq %r10, %r14
pushq $0x1
popq %r12
cmpq -0x80(%rbp), %r12
je 0x13b9
movq %r14, %r9
pushq $0x1
popq %r8
leaq (%rcx,%r8), %rax
cmpq $0x1, %rax
je 0x13b0
andq $0x0, 0x8(%r9,%r13)
cmpq $0x2, %r8
jb 0x1393
movq (%r9), %rax
cqto
idivq %r15
movq %rdx, %rax
movq %rdx, 0x8(%r9,%r13)
jmp 0x1395
xorl %eax, %eax
cmpq %rsi, %r8
jge 0x13a8
addq (%r9,%rbx), %rax
cqto
idivq %r15
movq %rdx, 0x8(%r9,%r13)
addq %r13, %r9
incq %r8
jmp 0x136b
incq %r12
addq $0x8, %r14
jmp 0x135e
movq -0x78(%rbp), %r8
leaq 0x1(%r8), %rax
movq -0x98(%rbp), %r14
movq $0x1, 0x8(%r14,%r8,8)
addq $0x8, %r10
movq -0x40(%rbp), %r9
movq %r9, %rcx
movq -0x58(%rbp), %rdx
subq $0x1, %rdx
jb 0x1401
movq (%rdi), %rsi
movslq (%rsi,%r8,4), %rsi
imulq %r13, %rsi
movq (%r10,%rsi), %rsi
movq %rsi, (%rcx)
addq %r11, %rcx
addq $0x8, %r10
jmp 0x13e0
movq -0x38(%rbp), %rsp
addq $0x8, %r9
movq %rax, %r8
jmp 0x12ed
movq -0x30(%rbp), %rdx
movq %rdx, %rax
imulq %rdx, %rax
movq %rsp, %rcx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, %rsp
movslq -0x88(%rbp), %r15
leaq 0x8(,%rdx,8), %rax
movq %rax, -0x40(%rbp)
xorl %eax, %eax
pushq $0x1
popq %rsi
movq %rcx, %rdx
movq %rcx, %rdi
cmpq %r15, %rax
jg 0x1466
movq %rsi, (%rdx)
movq %rsi, (%rdi)
incq %rax
addq -0x40(%rbp), %rdi
addq %r13, %rdx
jmp 0x144f
leaq 0x8(%rcx), %rdi
xorl %r8d, %r8d
movl $0x3b9aca07, %r9d       # imm = 0x3B9ACA07
movq %rdi, %r10
movq %r8, %rbx
cmpq -0x80(%rbp), %rsi
je 0x14a6
subq $0x1, %rbx
jb 0x149b
movq (%r10), %rax
addq -0x8(%r10), %rax
cqto
idivq %r9
movq %rdx, (%r10,%r13)
addq $0x8, %r10
jmp 0x147f
incq %rsi
incq %r8
addq %r13, %rdi
jmp 0x1473
movq %rsp, %rdi
subq -0x48(%rbp), %rdi
movq %rdi, %rsp
pushq $0x8
popq %rax
xorl %edx, %edx
cmpq %r15, %rdx
jg 0x14ca
movq (%r14,%rax), %rsi
movq %rsi, (%rdi,%rax)
incq %rdx
addq %r11, %rax
jmp 0x14b5
movslq -0x50(%rbp), %rdx
xorl %eax, %eax
cmpq %rdx, %rax
jg 0x14e2
movq $0x1, (%rdi,%rax,8)
incq %rax
jmp 0x14d0
movq %r15, -0x48(%rbp)
leaq 0x8(%rdi), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
leaq (%rcx,%rax,8), %rax
addq $0x8, %rax
movq %rax, -0x50(%rbp)
movq -0x70(%rbp), %rax
leaq (%r14,%rax,8), %rax
addq $0x10, %rax
movq %r11, %r13
negq %r13
pushq $0x2
popq %rcx
movl $0x3b9aca07, %r15d      # imm = 0x3B9ACA07
movq %rdi, -0x60(%rbp)
movq %rdx, -0x58(%rbp)
cmpq %rdx, %rcx
jg 0x15d6
movq %rcx, -0x68(%rbp)
leaq (%rdi,%rcx,8), %rbx
movq %rax, -0x30(%rbp)
movq %rax, %r10
movq -0x50(%rbp), %r8
pushq $0x1
popq %rsi
pushq $0x2
popq %r9
cmpq -0x80(%rbp), %rsi
je 0x15b5
movq -0x70(%rbp), %rdi
imulq %rsi, %rdi
andq $0x0, (%rbx,%rdi,8)
movq %r10, -0x78(%rbp)
movq -0x38(%rbp), %r14
xorl %r12d, %r12d
xorl %ecx, %ecx
movq %r9, %rax
addq %r12, %rax
je 0x15a2
movq (%r10), %rax
imulq (%r14), %rax
cqto
idivq %r15
movq %rdx, %rax
imulq (%r8,%r12,8), %rax
cqto
idivq %r15
addq %rdx, %rcx
movq %rcx, %rax
cqto
idivq %r15
movq %rdx, %rcx
movq %rdx, (%rbx,%rdi,8)
decq %r12
addq %r11, %r14
addq %r13, %r10
jmp 0x1564
incq %rsi
incq %r9
addq -0x40(%rbp), %r8
movq -0x78(%rbp), %r10
addq %r11, %r10
jmp 0x1544
movq -0x68(%rbp), %rcx
incq %rcx
addq $0x8, -0x38(%rbp)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rdx
jmp 0x1521
movq -0x70(%rbp), %rax
imulq -0x48(%rbp), %rax
leaq (%rdi,%rax,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