fork(1) download
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class TaskD
  5. {
  6. public static void main(String[] args)
  7. {
  8. new TaskD(System.in, System.out);
  9. }
  10.  
  11. static class Solver implements Runnable
  12. {
  13. static final long MOD = (long) 1e9 + 7;
  14. int n, m;
  15. int[] a, b;
  16. long[] unknown;
  17. InputReader in;
  18.  
  19. void solve() throws IOException
  20. {
  21. n = in.nextInt();
  22. m = in.nextInt();
  23. a = in.nextIntArray(n);
  24. b = in.nextIntArray(n);
  25. unknown = new long[n];
  26.  
  27. for (int i = n - 1; i >= 0; i--)
  28. {
  29. if (i < n - 1)
  30. unknown[i] = unknown[i + 1];
  31.  
  32. if (a[i] == 0)
  33. unknown[i]++;
  34.  
  35. if (b[i] == 0)
  36. unknown[i]++;
  37. }
  38.  
  39. long p = 0;
  40. long q = CMath.modPower(m, unknown[0], MOD);
  41. int bothZero = 0;
  42.  
  43. for (int i = 0; i < n; i++)
  44. {
  45. // System.out.println("i : " + i + ", a[i] : " + a[i] + ", b[i] : " + b[i]);
  46.  
  47. if (a[i] == 1/* && b[i] == 0*/)
  48. {
  49. p = 0;
  50.  
  51. break;
  52. }
  53. else if (a[i] != 0 && b[i] != 0 && a[i] <= b[i])
  54. break;
  55. else if (a[i] == 0 && b[i] == 0)
  56. {
  57. long x = CMath.mod((m - 1) * (m - 2) / 2, MOD);
  58. long add = CMath.mod(x * (i < n - 1 ? CMath.modPower(m, unknown[i + 1], MOD) : 1), MOD);
  59.  
  60. add = CMath.mod(add * CMath.modPower(m, bothZero, MOD), MOD);
  61. p = CMath.mod(p + add, MOD);
  62. bothZero++;
  63. }
  64. else if (a[i] == 0)
  65. {
  66. long add = CMath.mod((m - b[i]) * (i < n - 1 ? CMath.modPower(m, unknown[i + 1], MOD) : 1), MOD);
  67.  
  68. add = CMath.mod(add * CMath.modPower(m, bothZero, MOD), MOD);
  69. p = CMath.mod(p + add, MOD);
  70. }
  71. else if (b[i] == 0)
  72. {
  73. long add = CMath.mod((a[i] - 1) * (i < n - 1 ? CMath.modPower(m, unknown[i + 1], MOD) : 1), MOD);
  74.  
  75. add = CMath.mod(add * CMath.modPower(m, bothZero, MOD), MOD);
  76. p = CMath.mod(p + add, MOD);
  77. }
  78. else if (a[i] > b[i])
  79. {
  80. long add = (i < n - 1 ? CMath.modPower(m, unknown[i + 1], MOD) : 1);
  81.  
  82. add = CMath.mod(add * CMath.modPower(m, bothZero, MOD), MOD);
  83. p = CMath.mod(p + add, MOD);
  84. }
  85. // System.out.println("\ti : " + i + ", p : " + p);
  86. }
  87.  
  88. System.out.println("p : " + p + ", q : " + q);
  89.  
  90. if (p == 0)
  91. out.println(0);
  92. else
  93. {
  94. long gcd = CMath.gcd(p, q);
  95.  
  96. p /= gcd;
  97. q /= gcd;
  98. q = CMath.moduloInverse(q, MOD);
  99. out.println(CMath.mod(p * q, MOD));
  100. }
  101. }
  102.  
  103. public Solver(InputReader in, PrintWriter out)
  104. {
  105. this.in = in;
  106. this.out = out;
  107. }
  108.  
  109. @Override public void run()
  110. {
  111. try
  112. {
  113. solve();
  114. }
  115. catch (IOException e)
  116. {
  117. e.printStackTrace();
  118. }
  119. }
  120.  
  121. }
  122.  
  123. static class InputReader
  124. {
  125. private InputStream stream;
  126. private byte[] buf = new byte[1024];
  127. private int curChar;
  128. private int numChars;
  129.  
  130. public int read()
  131. {
  132. if (numChars == -1)
  133. throw new InputMismatchException();
  134.  
  135. if (curChar >= numChars)
  136. {
  137. curChar = 0;
  138. try
  139. {
  140. numChars = stream.read(buf);
  141. }
  142. catch (IOException e)
  143. {
  144. throw new InputMismatchException();
  145. }
  146. if (numChars <= 0)
  147. return -1;
  148. }
  149.  
  150. return buf[curChar++];
  151. }
  152.  
  153. public int nextInt()
  154. {
  155. int c = read();
  156.  
  157. while (isSpaceChar(c))
  158. c = read();
  159.  
  160. int sgn = 1;
  161.  
  162. if (c == '-')
  163. {
  164. sgn = -1;
  165. c = read();
  166. }
  167.  
  168. int res = 0;
  169.  
  170. do
  171. {
  172. if (c < '0' || c > '9')
  173. throw new InputMismatchException();
  174.  
  175. res *= 10;
  176. res += c & 15;
  177.  
  178. c = read();
  179. } while (!isSpaceChar(c));
  180.  
  181. return res * sgn;
  182. }
  183.  
  184. public int[] nextIntArray(int arraySize)
  185. {
  186. int array[] = new int[arraySize];
  187.  
  188. for (int i = 0; i < arraySize; i++)
  189. array[i] = nextInt();
  190.  
  191. return array;
  192. }
  193.  
  194. public boolean isSpaceChar(int c)
  195. {
  196. return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  197. }
  198.  
  199. public void close()
  200. {
  201. try
  202. {
  203. stream.close();
  204. }
  205. catch (IOException e)
  206. {
  207. e.printStackTrace();
  208. }
  209. }
  210.  
  211. public InputReader(InputStream stream)
  212. {
  213. this.stream = stream;
  214. }
  215.  
  216. }
  217.  
  218. static class CMath
  219. {
  220. static long modPower(long number, long power, long mod)
  221. {
  222. if (number == 1 || number == 0 || power == 0)
  223. return 1;
  224.  
  225. number = mod(number, mod);
  226.  
  227. if (power == 1)
  228. return number;
  229.  
  230. long square = mod(number * number, mod);
  231.  
  232. if (power % 2 == 0)
  233. return modPower(square, power / 2, mod);
  234. else
  235. return mod(modPower(square, power / 2, mod) * number, mod);
  236. }
  237.  
  238. static long moduloInverse(long number, long mod)
  239. {
  240. return modPower(number, mod - 2, mod);
  241. }
  242.  
  243. static long mod(long number, long mod)
  244. {
  245. return number - (number / mod) * mod;
  246. }
  247.  
  248. static long gcd(long a, long b)
  249. {
  250. if (b == 0)
  251. return a;
  252. else
  253. return gcd(b, a % b);
  254. }
  255.  
  256. }
  257.  
  258. public TaskD(InputStream inputStream, OutputStream outputStream)
  259. {
  260. InputReader in = new InputReader(inputStream);
  261. PrintWriter out = new PrintWriter(outputStream);
  262. Thread thread = new Thread(null, new Solver(in, out), "TaskD", 1 << 29);
  263.  
  264. try
  265. {
  266. thread.start();
  267. thread.join();
  268. }
  269. {
  270. e.printStackTrace();
  271. }
  272. finally
  273. {
  274. in.close();
  275. out.flush();
  276. out.close();
  277. }
  278. }
  279.  
  280. }
  281.  
  282.  
Success #stdin #stdout 0.06s 27884KB
stdin
7 26
0 15 12 9 13 0 14
11 1 0 13 15 12 0
stdout
p : 288652, q : 456976
897929001