#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int N = 1e5 + 7;
const long long mod = 1e9 + 7;
int s1[N], s2[N];
int same[N];
long long qpow(long long a, long long b) {
  long long r = 1;
  while (b) {
    if (b & 1) r = r * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return r;
}
long long inv(long long n) { return qpow(n, mod - 2); }
int main(void) {
  register int n, m, i;
  while (~scanf("%d%d", &n, &m)) {
    for (i = 1; i <= n; ++i) {
      scanf("%d", &s1[i]);
    }
    for (i = 1; i <= n; ++i) {
      scanf("%d", &s2[i]);
    }
    for (i = 1; i <= n; ++i) {
      if (s1[i] && s2[i] && (s1[i] != s2[i]))
        same[i] = 0;
      else
        same[i] = 1;
    }
    register long long invm = qpow(m, mod - 2);
    register long long invsig2 = 1LL * (m - 1) * qpow(2LL * m, mod - 2) % mod;
    register long long P_p = 1;
    register long long Ans = 0;
    for (i = 1; i <= n; ++i) {
      long long P_g = 0;
      if (s1[i] && s2[i]) {
        P_g = (s1[i] > s2[i]);
      } else if (!s1[i] && s2[i]) {
        P_g = (P_g + 1LL * (m - s2[i]) * invm % mod);
      } else if (s1[i] && !s2[i]) {
        P_g = (P_g + 1LL * (s1[i] - 1) * invm) % mod;
      } else {
        P_g = (P_g + invsig2) % mod;
      }
      Ans += P_p * P_g;
      Ans %= mod;
      if (!s1[i] || !s2[i]) P_p = P_p * invm % mod;
      if (s1[i] && s2[i] && (s1[i] != s2[i])) break;
    }
    printf("%I64d\n", Ans);
  }
  return 0;
}
