import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main { private static int gx; private static int gy; private static int[][] crystalArray; private static List resultSelectPatternList; private static final BigInteger DIVISER = new BigInteger("1000000007"); public static void main(String[] args) { Scanner sc = new Scanner(System.in); gx = Integer.parseInt(sc.next()); gy = Integer.parseInt(sc.next()); int k = Integer.parseInt(sc.next()); /* k種のクリスタル。[k][0]=x移動量, [k][1]=y移動量, [k][2]=個数 */ crystalArray = new int[k][3]; for (int i = 0; i < k; i++) { crystalArray[i][0] = Integer.parseInt(sc.next()); crystalArray[i][1] = Integer.parseInt(sc.next()); crystalArray[i][2] = Integer.parseInt(sc.next()); } sc.close(); /* ナップサック問題のように、k種目のクリスタルを0個使う場合~N(k)個使う場合に、ゴールまでの相対座標を求める。 */ resultSelectPatternList = new ArrayList(); selectCrystal(0, new int[k], new int[2]); /* 何番目のクリスタルを何個使った時にゴールにたどり着くかの全パターンが求まるので、あとは各パターンごとにクリスタルを使う順が何通りあるかを求め、足し合わせる */ BigInteger result = BigInteger.ZERO; for (int[] selectPattern : resultSelectPatternList) { result.add(permutation(selectPattern)); while (result.compareTo(DIVISER) > 0) { result = result.mod(DIVISER); } } System.out.println(result); } private static void selectCrystal(int index, int[] selected, int[] position) { if (index == crystalArray.length) { if (position[0] == gx && position[1] == gy) { resultSelectPatternList.add(selected); } return; } for (int i = 0; i <= crystalArray[index][2]; i++) { int[] currentSelect = Arrays.copyOf(selected, selected.length); int[] currentPosition = Arrays.copyOf(position, position.length); currentSelect[index] = i; currentPosition[0] += crystalArray[index][0] * i; currentPosition[1] += crystalArray[index][1] * i; selectCrystal(index + 1, currentSelect, currentPosition); } } private static BigInteger permutation(int[] pattern) { int total = 0; for (int i = 0; i < pattern.length; i++) { total = total += pattern[i]; } BigInteger totalFact = factorialBI(total); for (int i = 0; i < pattern.length; i++) { totalFact = totalFact.divide(factorialBI(pattern[i])); } while (totalFact.compareTo(DIVISER) > 0) { totalFact = totalFact.mod(DIVISER); } return totalFact; } private static BigInteger factorialBI(int n) { BigInteger bi = BigInteger.ONE; for (int i = 2; i <= n; i++) { bi = bi.multiply(new BigInteger(String.valueOf(i))); } return bi; } }