#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 5;
const long long mod = 1e9 + 7;
long long zongshu = 1, da = 0, deng = 1;
long long extended_euclid(long long a, long long b, long long& x,
                          long long& y) {
  long long d = a;
  if (b) {
    d = extended_euclid(b, a % b, y, x);
    y = (y - (a / b) * x) % mod;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}
void solve(long long a, long long b, long long d, long long& x, long long& y) {
  long long gcd = extended_euclid(a, b, x, y);
  x = x * d;
  long long t = b;
  x = (x % t + t) % t;
  y = (d - a * x) / b;
}
int main() {
  long long n, m, x, y, s1_gr_s2 = 0;
  int s1[maxn], s2[maxn];
  cin >> n >> m;
  for (long long i = 0; i < n; i++) {
    scanf("%d", &s1[i]);
  }
  for (long long i = 0; i < n; i++) {
    scanf("%d", &s2[i]);
  }
  for (long long i = 0; i < n; i++) {
    if (s1[i] == 0) {
      if (s2[i] == 0) {
        da = (((da * m % mod) * m) % mod +
              ((m * (m - 1) / 2 % mod) * deng) % mod) %
             mod;
        zongshu = (((zongshu * m) % mod) * m) % mod;
        deng = deng * m % mod;
        continue;
      }
      zongshu = zongshu * m % mod;
      da = (da * m % mod + deng * (m - s2[i]) % mod) % mod;
      continue;
    }
    if (s2[i] == 0) {
      da = (da * m % mod + deng * (s1[i] - 1) % mod) % mod;
      zongshu = zongshu * m % mod;
      continue;
    }
    if (s1[i] > s2[i]) {
      da = (da + deng) % mod;
      break;
    }
    if (s1[i] < s2[i]) {
      if (!da) {
        cout << 0 << endl;
        return 0;
      }
      break;
    }
  }
  long long ans, c;
  solve(zongshu, mod, da, ans, c);
  cout << ans << endl;
  return 0;
}
