Problem p03959 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define LL long long
using namespace std;
int main(int argc, const char *argv[]) {
  int N;
  LL ans = 1;
  cin >> N;
  LL mode = 1;
  for (int i = 0; i < 9; i++)
    mode *= 10;
  mode += 7;
  LL x[10020] = {0}, y[10020] = {0};
  int ml = 0, mr = N - 1;
  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> y[i];
  }
  for (int i = N - 1; i >= 1; i--) {
    if (x[i] > x[i - 1]) {
      ml = i;
      break;
    }
  }
  for (int i = 0; i < N - 1; i++) {
    if (y[i] > y[i + 1]) {
      mr = i;
      break;
    }
  }
  for (int i = 1; i < N; i++) {
    if (x[i] < x[i - 1]) {
      cout << "0" << endl;
      return 0;
    }
  }
  for (int i = N - 2; i >= 0; i--) {
    if (y[i] < y[i + 1]) {
      cout << "0" << endl;
      return 0;
    }
  }
  if (ml > mr) {
    cout << "0" << endl;
    return 0;
  }
  bool err = false;
  for (int i = 0; i < ml; i++) {
    if (x[i] > x[i + 1] || y[i] < x[i]) {
      err = true;
      break;
    }
  }
  if (err) {
    cout << "0" << endl;
    return 0;
  }
  for (int i = N - 2; i > mr; i--) {
    if (y[i] < y[i + 1] || x[i] < y[i]) {
      err = true;
      break;
    }
  }
  if (err) {
    cout << "0" << endl;
    return 0;
  }
  for (int i = ml; i <= mr; i++) {
    if (x[i] != y[i]) {
      cout << "0" << endl;
      return 0;
    }
  }

  for (int i = 1; i < ml; i++) {
    if (x[i] != x[i - 1]) {
      continue;
    }
    ans = (ans * x[i]) % mode;
  }
  LL t = x[ml];
  for (int i = ml + 1; i <= mr - 1; i++) {
    ans = (ans * t) % mode;
  }
  for (int i = N - 2; i > mr; i--) {
    if (y[i] != y[i + 1]) {
      continue;
    }
    ans = (ans * y[i]) % mode;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define LL long long
using namespace std;
int main(int argc, const char *argv[]) {
  int N;
  LL ans = 1;
  cin >> N;
  LL mode = 1;
  for (int i = 0; i < 9; i++)
    mode *= 10;
  mode += 7;
  LL x[N], y[N];
  int ml = 0, mr = N - 1;
  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> y[i];
  }
  for (int i = N - 1; i >= 1; i--) {
    if (x[i] > x[i - 1]) {
      ml = i;
      break;
    }
  }
  for (int i = 0; i < N - 1; i++) {
    if (y[i] > y[i + 1]) {
      mr = i;
      break;
    }
  }
  for (int i = 1; i < N; i++) {
    if (x[i] < x[i - 1]) {
      cout << "0" << endl;
      return 0;
    }
  }
  for (int i = N - 2; i >= 0; i--) {
    if (y[i] < y[i + 1]) {
      cout << "0" << endl;
      return 0;
    }
  }
  if (ml > mr) {
    cout << "0" << endl;
    return 0;
  }
  bool err = false;
  for (int i = 0; i < ml; i++) {
    if (x[i] > x[i + 1] || y[i] < x[i]) {
      err = true;
      break;
    }
  }
  if (err) {
    cout << "0" << endl;
    return 0;
  }
  for (int i = N - 2; i > mr; i--) {
    if (y[i] < y[i + 1] || x[i] < y[i]) {
      err = true;
      break;
    }
  }
  if (err) {
    cout << "0" << endl;
    return 0;
  }
  for (int i = ml; i <= mr; i++) {
    if (x[i] != y[i]) {
      cout << "0" << endl;
      return 0;
    }
  }

  for (int i = 1; i < ml; i++) {
    if (x[i] != x[i - 1]) {
      continue;
    }
    ans = (ans * x[i]) % mode;
  }
  LL t = x[ml];
  for (int i = ml + 1; i <= mr - 1; i++) {
    ans = (ans * t) % mode;
  }
  for (int i = N - 2; i > mr; i--) {
    if (y[i] != y[i + 1]) {
      continue;
    }
    ans = (ans * y[i]) % mode;
  }
  cout << ans << endl;
  return 0;
}