#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
const int M = 1e9 + 7;
int powmod(int a, int b) {
  if (b == 0) {
    return 1;
  }
  if (b & 1) {
    return (long long)a * powmod(a, b - 1) % M;
  }
  return powmod((long long)a * a % M, b / 2);
}
int modinv(int a) { return powmod(a, M - 2); }
int addmod(int a, int b) {
  a += b;
  if (a >= M) a -= M;
  return a;
}
int mulmod(int a, int b) { return (long long)a * b % M; }
int s[2][N];
int main() {
  std::ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> s[i][j];
    }
  }
  int ans = 0;
  int curmult = 1;
  int m2inv = modinv(2LL * m);
  int minv = modinv(m);
  for (int i = 0; i < n; ++i) {
    if (s[0][i] == 0 and s[1][i] == 0) {
      int curv = mulmod(mulmod(curmult, m - 1), m2inv);
      ans = addmod(ans, curv);
      curmult = mulmod(curmult, minv);
      continue;
    }
    if (s[0][i] == 0) {
      int curv = mulmod(mulmod(curmult, m - s[1][i]), minv);
      ans = addmod(ans, curv);
      curmult = mulmod(curmult, minv);
      continue;
    }
    if (s[1][i] == 0) {
      int curv = mulmod(mulmod(curmult, s[0][i] - 1), minv);
      ans = addmod(ans, curv);
      curmult = mulmod(curmult, minv);
      continue;
    }
    if (s[0][i] < s[1][i]) {
      break;
    }
    if (s[0][i] > s[1][i]) {
      ans = addmod(ans, curmult);
      break;
    }
  }
  cout << ans << '\n';
}
