Problem:
Score: 400 points

Problem Statement

Amidakuji is a traditional method of lottery in Japan.

To make an amidakuji, we first draw W parallel vertical lines, and then draw horizontal lines that connect them. The length of each vertical line is H+1 [cm], and the endpoints of the horizontal lines must be at 1, 2, 3, ..., or H [cm] from the top of a vertical line.

A valid amidakuji is an amidakuji that satisfies the following conditions:

No two horizontal lines share an endpoint.

The two endpoints of each horizontal lines must be at the same height.

A horizontal line must connect adjacent vertical lines.

Find the number of the valid amidakuji that satisfy the following condition, modulo 1\ 000\ 000\ 007: if we trace the path from the top of the leftmost vertical line to the bottom, always following horizontal lines when we encounter them, we reach the bottom of the K-th vertical line from the left.

For example, in the following amidakuji, we will reach the bottom of the fourth vertical line from the left.

Constraints

H is an integer between 1 and 100 (inclusive).

W is an integer between 1 and 8 (inclusive).

K is an integer between 1 and W (inclusive).

Input

Input is given from Standard Input in the following format:

H W K

Output

Print the number of the amidakuji that satisfy the condition, modulo 1\ 000\ 000\ 007.

Sample Input 1

1 3 2

Sample Output 1

1

Only the following one amidakuji satisfies the condition:

Sample Input 2

1 3 1

Sample Output 2

2

Only the following two amidakuji satisfy the condition:

Sample Input 3

2 3 3

Sample Output 3

1

Only the following one amidakuji satisfies the condition:

Sample Input 4

2 3 1

Sample Output 4

5

Only the following five amidakuji satisfy the condition:

Sample Input 5

7 1 1

Sample Output 5

1

As there is only one vertical line, we cannot draw any horizontal lines. Thus, there is only one amidakuji that satisfies the condition: the amidakuji with no horizontal lines.

Sample Input 6

15 8 5

Sample Output 6

437760187

Be sure to print the answer modulo 1\ 000\ 000\ 007.

Solution in C (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h> // uint64_t

#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) > (b) ? (b) : (a))

int get_int3(int *a1, int *a2, int *a3) {
  scanf("%d %d %d", a1, a2, a3);
  return 0;
}

#define HEIGHT_MAX 100
#define WIDTH_MAX 8
#define DIVISOR 1000000007
#define PATTERN_MAX (1<<(WIDTH_MAX-1))

void swap(int *a, int *b) {
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

void get_next(int pat, int size, int *out) {
    int i, bit;
    for(i = 0; i < size; i++) out[i] = i;
    for(bit = 0; bit < size-1; bit++) {
        if(!(pat & (1<<bit))) continue;
        // swap
        swap(&out[bit], &out[bit+1]);
    }
}

int main(void) {
    int height, width, goal;
    get_int3(&height, &width, &goal);
    // 1-indexed -> 0-indexed
    goal--;
    static int64_t dp[HEIGHT_MAX+2][WIDTH_MAX];
    dp[0][0] = 1;
    int h, w, pat;
    static char pat_valid[PATTERN_MAX];
    static int next_idx[WIDTH_MAX];
    for(pat = 0; pat < (1<<(width-1)); pat++) {
        int bit;
        int prev = 0;
        int flag = 1;
        for(bit = 0; bit < width-1; bit++) {
            int cur = !!(pat & (1<<bit));
            if(prev == cur && cur == 1) { flag = 0; break; }
            prev = cur;
        }
        pat_valid[pat] = flag;
#ifdef DEBUG
        int i;
        printf("%d -> %d\n", pat, pat_valid[pat]);
        get_next(pat, width, next_idx);
        for(i = 0; i < width; i++) printf(" %d", next_idx[i]);
        putchar('\n');
#endif
    }

    for(h = 1; h <= height; h++) {
        for(pat = 0; pat < (1<<(width-1)); pat++) {
            if(!pat_valid[pat]) continue;
            get_next(pat, width, next_idx);
            for(w = 0; w < width; w++) {
                int nidx = next_idx[w];
                dp[h][nidx] = (dp[h][nidx] + dp[h-1][w])%DIVISOR;
#ifdef DEBUG
                printf("[%d] dp[%d][%d] = %lld\n", w, h, nidx, dp[h][nidx]);
#endif
            }
        }
    }
    printf("%d\n", (int)dp[height][goal]);
    return 0;
}