#include <bits/stdc++.h>
using namespace std;
long long Fac[100010], Inv[100010], inv[100010];
void ini() {
  Fac[0] = Inv[0] = Fac[1] = Inv[1] = inv[1] = 1;
  for (int i = 2; i < 100010; i++) {
    Fac[i] = Fac[i - 1] * i % 1000000007;
    inv[i] = inv[1000000007 % i] * (1000000007 - 1000000007 / i) % 1000000007;
    Inv[i] = Inv[i - 1] * inv[i] % 1000000007;
  }
}
long long C(int n, int m) {
  return Fac[n] * Inv[m] % 1000000007 * Inv[n - m] % 1000000007;
}
int a[100010], b[100010];
int sa[100010], sb[100010];
int pwm[200010];
int main() {
  ini();
  int n, m;
  scanf("%d %d", &n, &m);
  pwm[0] = 1;
  for (int i = 1; i <= 2 * n; i++) {
    pwm[i] = (long long)pwm[i - 1] * m % 1000000007;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    sa[i] = sa[i - 1] + (a[i] == 0);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    sb[i] = sb[i - 1] + (b[i] == 0);
  }
  long long pre = 1;
  long long p = 0;
  long long q = 1;
  for (int i = 1; i <= sb[n] + sa[n]; i++) {
    q = q * inv[m] % 1000000007;
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] != 0 && b[i] != 0) {
      if (a[i] != b[i]) {
        if (a[i] > b[i]) {
          p += pwm[sb[n] - sb[i] + sa[n] - sa[i]] % 1000000007 * pre %
               1000000007;
          p %= 1000000007;
        }
        break;
      }
    } else {
      if (a[i] == b[i] && a[i] == 0) {
        p += (long long)(1 + (m - 1)) * (m - 1) / 2 % 1000000007 *
             pwm[sb[n] - sb[i] + sa[n] - sa[i]] % 1000000007 * pre % 1000000007;
        p %= 1000000007;
        pre = pre * m % 1000000007;
      } else if (a[i] == 0) {
        p += (long long)(m - b[i]) * pwm[sb[n] - sb[i] + sa[n] - sa[i]] %
             1000000007 * pre % 1000000007;
        p %= 1000000007;
      } else {
        p += (long long)(a[i] - 1) * pwm[sb[n] - sb[i] + sa[n] - sa[i]] %
             1000000007 * pre % 1000000007;
        p %= 1000000007;
      }
    }
  }
  cout << p * q % 1000000007;
  return 0;
}
