#include <bits/stdc++.h>
using namespace std;
long long mpow(long long a, long long x) {
  long long ret = 1;
  while (x) {
    if (x & 1) ret = ret * a % ((int)1e9 + 7);
    x >>= 1;
    a = a * a % ((int)1e9 + 7);
  }
  return ret;
}
long long n, m;
long long s1[100010], s2[100010];
long long kp[200010];
int main() {
  scanf("%lld %lld", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%lld", &s1[i]);
  for (int i = 1; i <= n; i++) scanf("%lld", &s2[i]);
  int pos = -1;
  int tot = 0;
  for (int i = 1; i <= n; i++) {
    if (s1[i] && s2[i] && s1[i] != s2[i]) {
      pos = i;
      break;
    }
    if (s1[i] == 0) tot++;
    if (s2[i] == 0) tot++;
  }
  int ad;
  if (pos != -1 && s1[pos] > s2[pos])
    ad = 1;
  else
    ad = 0;
  kp[0] = 1;
  for (int i = 1; i <= tot; i++) kp[i] = kp[i - 1] * m % ((int)1e9 + 7);
  long long inv2 = mpow(2, ((int)1e9 + 7) - 2);
  long long zi = 0, sum = 0;
  sum = kp[tot];
  if (pos == -1) pos = n;
  int tot_equal = 0;
  for (int i = 1; i <= pos; i++) {
    if (s1[i] == 0 && s2[i] == 0 && tot == 2) {
      tot -= 2;
      zi = (zi + ((m * m % ((int)1e9 + 7) - m) % ((int)1e9 + 7) * inv2 %
                      ((int)1e9 + 7) +
                  m * ad) %
                     ((int)1e9 + 7) * kp[tot_equal] % ((int)1e9 + 7)) %
               ((int)1e9 + 7) +
           1 - 1;
      break;
    } else if (s1[i] == 0 && tot == 1) {
      tot--;
      zi = (zi + (m - s2[i] + ad) % ((int)1e9 + 7) * kp[tot_equal] %
                     ((int)1e9 + 7)) %
           ((int)1e9 + 7);
      break;
    } else if (s2[i] == 0 && tot == 1) {
      tot--;
      zi = (zi + (s1[i] - 1 + ad) % ((int)1e9 + 7) * kp[tot_equal] %
                     ((int)1e9 + 7)) %
           ((int)1e9 + 7);
      break;
    } else if (s1[i] && s2[i]) {
      if (s1[i] == s2[i]) continue;
      if (s1[i] > s2[i]) {
        zi = (zi + kp[tot_equal]) % ((int)1e9 + 7);
      }
      break;
    } else if (s1[i] == 0 && s2[i] == 0) {
      tot -= 2;
      zi = (zi + (m * m % ((int)1e9 + 7) - m) % ((int)1e9 + 7) * inv2 %
                     ((int)1e9 + 7) * kp[tot + tot_equal] % ((int)1e9 + 7)) %
           ((int)1e9 + 7);
      tot_equal++;
    } else if (s1[i] == 0) {
      tot--;
      zi = (zi + (m - s2[i]) % ((int)1e9 + 7) * kp[tot + tot_equal] %
                     ((int)1e9 + 7)) %
           ((int)1e9 + 7);
    } else if (s2[i] == 0) {
      tot--;
      zi = (zi + (s1[i] - 1) % ((int)1e9 + 7) * kp[tot + tot_equal] %
                     ((int)1e9 + 7)) %
           ((int)1e9 + 7);
    }
  }
  long long ans = zi * mpow(sum, ((int)1e9 + 7) - 2) % ((int)1e9 + 7);
  ans = (ans + ((int)1e9 + 7)) % ((int)1e9 + 7);
  cout << ans << endl;
}
