Facer's Chocolate Dream

Time Limit: 1000MS

Memory Limit: 65536K


"It is so sweet to have chocolates on St. Valentine's Day!" Little Facer was so excited when he receives a box of self-made chocolates from his girlfriend, and he decided to eat these chocolates in a special way to commemorate this important day. Suppose that there are N types of chocolates in the box, it can be easily calculated that there are totally {{n}\choose{3}} different combinations if we choose 3 chocolates of different types to make a dish. Facer will first make one dish for every of these combination, so there will be {{n}\choose{3}} dishes in all with 3×{{n}\choose{3}} chocolates totally. Then both Facer and his girlfriend choose some chocolates of different types as their original chocolates set. After that, Facer choose exactly M dishes of three-type-mixed-chocolates which he made in the first step and add them into his original chocolates set. Finally, Facer continues eatting two chocolates of the same type until he cannot find any pair of chocolates of the same type (which means for each type of chocolates, there remains at most one chocolate). Facer wishes that his remaining chocolates set could be identical with his girlfriend's original chocolates set after the steps mentioned above, but he does not know the number of ways to choose dishes. Could please tell him the answer?


The input consists of multiple test cases. Each test case starts with two integers, N and M, which are the number of different types of chocolates and the number of three-type-mixed-chocolates dishes Facer will choose, it is guaranteed that 1≤N≤1000, 0≤M≤1000. The following two lines contain one N-bit binary integer each, which represents Facer's and his girlfriend's original chocolates set. A case with N=0 and M=0 indicates the end of the input file, which should not be processed.


For each test case, print one line containing one single integer, which represents the total number of ways to choose dishes. Since the number can be extremely big, you are only required to output the answer % 10007.

Sample Input

4 3
3 1
5 3
0 0

Sample Output



This problem is inspired from 2008 Regional Hangzhou but is more difficult.
A naive print-table-algorithm will not pass.


POJ Monthly Contest – 2009.02.22, Facer