Problem:
イヅア国語辞典
あなたはイヅア国の公用語であるイヅア語の国語辞典と、イヅア語のアルファベット（文字の一覧表）
を手に入れました。イヅア語のアルファベットにはN 種類の文字があります。イヅア語の国語辞典に現れる単語の順番は、イヅア語のアルファベット順に並んでいます。
辞典を見ると、載っているどの単語もN 文字で、しかも、N 種類の文字をひとつずつ含んでいることがわかりました。さらに調べてみると、辞典にはN 種類の文字の可能な並び方がすべて書かれていることを発見しました。
この発見から、あなたはある単語が辞典の何番目に出てくるかわかるようになりました。この知識を利用してイヅア国の人を驚かせてみましょう。まず、N 種類の文字をひとつずつアルファベット順に並べます。次に、任意の２文字の順番を入れ替える操作を R 回繰り返してもらいます。あなたは、出来上がった単語がイヅア国語辞典の何番目に出てくるか当てて見せます。そのための準備として、国語辞典中の単語の場所を求めるプログラムを作成してください。ただし、アルファベット順で最初の単語を 0 番目の単語とします。
入力
入力は複数のデータセットからなる。入力の終わりはゼロ１つの行で示される。各データセットは以下の形式で与えられる。
N
R
s
1
t
1
s
2
t
2
:
s
R
t
R
１行目にアルファベットを構成する文字の数 N (1 ≤ N ≤ 100000) が与えられ、２行目に文字を入れ替えてもらう回数 R (0 ≤ R ≤ 50) が与えられる。続く R 行に、入れ替えられる文字の位置の組が与えられる。s
i
と t
i
(1 ≤ s
i
< t
i
≤ N) は、先頭から数えて s
i
番目と t
i
番目の文字を i 回目に入れ替えることを表す。s
i
と t
i
は１つの空白で区切られている。
データセットの数は100 を超えない。
出力
データセットごとに、入れ替えが終わった時点で得られた単語が国語辞典の何番目に現れるかを示す数を１行に出力する。ただし、出力すべき値は非常に大きくなりうるので、代わりに 1,000,000,007 で割った余りを出力する。
入力例
3
2
1 2
2 3
4
2
2 3
2 4
0
出力例
3
4
入出力例の最初のデータセットについて説明する。説明のため、アルファベットは小さい方から順番に、'A', 'B', 'C' の３文字からなるとする。これらの文字をアルファベット順に並べると "ABC" という文字列が得られる。この文字列の１番目の文字 'A' と２番目の文字 'B' を入れ替えると、"BAC" という文字列が得られる。次に、得られた文字列の２番目の文字 'A' と３番目の文字 'C' を入れ替えると、単語 "BCA" が得られる。辞典には、先頭から順に "ABC", "ACB", "BAC", "BCA", "CAB", "CBA" の６種類の単語が載っている。"BCA" はこの中で３番目（最初の"ABC"が０番目であることに注意）に現れるので、3 を 1,000,000,007 で割った余りである 3 が答えになる。

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;

int mul(int x, int y) {
    return (1ll * x * y) % mod;
}

const int MAXN = 1e5 + 5;

int fact[MAXN];

void precalc() {
    fact[0] = 1;
    for (int i = 1; i < MAXN; ++i) fact[i] = mul(fact[i - 1], i);
}

struct Fen {
    int n = 0;
    vector<int> f;

    Fen(int n): n(n), f(n) {}
    
    void add(int cur) {
        for (; cur < n; cur |= (cur + 1)) ++f[cur];
    }

    int get(int cur) {
        int res = 0;
        for (; cur >= 0; cur = (cur & (cur + 1)) - 1) res += f[cur];
        return res;
    }
};

signed main() {

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    precalc();

    for (int n; cin >> n;) {
        if (n == 0) break;
        vector<int> a(n);
        iota(a.begin(), a.end(), 0);
        int r;
        cin >> r;
        for (int x, y, i = 0; i < r; ++i) {
            cin >> x >> y;
            --x, --y;
            swap(a[x], a[y]);
        }
        int res = 0;
        Fen fen(n);
        for (int i = 0; i < n; ++i) {
            res = (res + mul(fact[n - i - 1], max(0, a[i] - fen.get(a[i])))) % mod;
            fen.add(a[i]);
        }
        cout << res << "\n";
    }

    return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 4228
final_rank: 1

code_snippet: #include <iostream>
#include <vector>
#include <map>

using namespace std;

const int divisor = 1000000007;

int main() {
  vector<long long> factmodary(100001);
  long long fact = 1;
  for (int i = 1; i <= 100000; i++) {
    fact *= i;
    fact %= divisor;
    factmodary[i] = fact;
  }
  
  int N, R, s, t, tmp, facttime, bubblenum;

  while (std::cin >> N && N != 0) {   
    cin >> R;
    vector<int> word(N);
    for (int i = 0; i < N; i++)
      word[i] = i + 1;
    for (int i = 0; i < R; i++) {
      cin >> s >> t;
      tmp = word[s - 1];
      word[s - 1] = word[t - 1];
      word[t - 1] = tmp;
    }
    map<int, int> bubblehist;
    bubblehist[word[0]] = 0;
    long long ans = factmodary[N - 1] * (word[0] - 1);
    for (int i = 1; i < N; i++) {
      facttime = word[i] - 1;
      map<int, int>::iterator it = bubblehist.lower_bound(word[i]);
      if (it != bubblehist.end()) {
        bubblenum = it->second;
        it--;
      } else {
        it--;
        bubblenum = it->second + 1;
      }
      pair<int, int> p;
      p.first = word[i];
      p.second = bubblenum;
      map<int, int>::iterator it2 = bubblehist.insert(it, p);
      for (++it2; it2 != bubblehist.end(); it2++)
        (it2->second)++;
      facttime -= bubblenum;
      ans += factmodary[N - (i + 1)] * facttime;
      ans %= divisor;
    }
    cout << (int)ans << "\n";
  }
  
  return 0;
}
accuracy: 1
time_ms: 2010
memory_kb: 8696
final_rank: 19

code_snippet: #include <iostream>
#include <map>
using namespace std;
const int divisor = 1000000007;
int main() {
  long long *factmodary = new long long[100001];
  long long fact = 1;
  for (int i = 1; i <= 100000; i++) {
    fact *= i;
    fact %= divisor;
    factmodary[i] = fact;
  }
  int *word = new int[100000]{0};
  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;
    for (int i = 0; i < N; i++)
      word[i] = i + 1;
    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      int tmp = word[s - 1];
      word[s - 1] = word[t - 1];
      word[t - 1] = tmp;
    }
    map<int, int> bubblehist;
    bubblehist[word[0]] = 0;
    long long ans = factmodary[N - 1] * (word[0] - 1);
    for (int i = 1; i < N; i++) {
      int facttime = word[i] - 1;
      int bubblenum;
      map<int, int>::iterator it = bubblehist.lower_bound(word[i]);
      if (it != bubblehist.end()) {
        bubblenum = it->second;
        it--;
      } else {
        it--;
        bubblenum = it->second + 1;
      }
      pair<int, int> p;
      p.first = word[i];
      p.second = bubblenum;
      map<int, int>::iterator it2 = bubblehist.insert(it, p);
      for (++it2; it2 != bubblehist.end(); it2++)
        (it2->second)++;
      facttime -= bubblenum;
      ans += factmodary[N - (i + 1)] * facttime;
    }
    cout << ans % divisor << "\n";
  }
  delete[] word;
  delete[] factmodary;
  return 0;
}
accuracy: 1
time_ms: 1840
memory_kb: 8536
final_rank: 12

code_snippet: #include <iostream>
#include <map>
using namespace std;
const int divisor = 1000000007;
int main() {
  long long *factmodary = new long long[100001];
  long long fact = 1;
  for (int i = 1; i <= 100000; i++) {
    fact *= i;
    fact %= divisor;
    factmodary[i] = fact;
  }
  int *word = new int[100000]{0};
  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;
    for (int i = 0; i < N; i++)
      word[i] = i + 1;
    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      int tmp = word[s - 1];
      word[s - 1] = word[t - 1];
      word[t - 1] = tmp;
    }
    map<int, int> bubblehist;
    bubblehist[word[0]] = 0;
    long long ans = factmodary[N - 1] * (word[0] - 1);
    for (int i = 1; i < N; i++) {
      int facttime = word[i] - 1;
      int bubblenum;
      map<int, int>::iterator it = bubblehist.lower_bound(word[i]);
      if (it != bubblehist.end()) {
        bubblenum = it->second;
        it--;
      } else {
        it--;
        bubblenum = it->second + 1;
      }
      pair<int, int> p;
      p.first = word[i];
      p.second = bubblenum;
      map<int, int>::iterator it2 = bubblehist.insert(it, p);
      for (++it2; it2 != bubblehist.end(); it2++)
        (it2->second)++;
      facttime -= bubblenum;
      ans += factmodary[N - (i + 1)] * facttime;
      ans %= divisor;
    }
    cout << (int)ans << "\n";
  }
  delete[] word;
  delete[] factmodary;
  return 0;
}
accuracy: 1
time_ms: 1830
memory_kb: 8628
final_rank: 15

code_snippet: #include <iostream>
#include <map>
using namespace std;
const int divisor = 1000000007;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long *factmodary = new long long[100001];
  long long fact = 1;
  for (int i = 1; i <= 100000; i++) {
    fact *= i;
    fact %= divisor;
    factmodary[i] = fact;
  }
  int *word = new int[100000]{0};
  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;
    for (int i = 0; i < N; i++)
      word[i] = i + 1;
    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      int tmp = word[s - 1];
      word[s - 1] = word[t - 1];
      word[t - 1] = tmp;
    }
    map<int, int> bubblehist;
    bubblehist[word[0]] = 0;
    long long ans = factmodary[N - 1] * (word[0] - 1);
    for (int i = 1; i < N; i++) {
      int facttime = word[i] - 1;
      int bubblenum;
      map<int, int>::iterator it = bubblehist.lower_bound(word[i]);
      if (it != bubblehist.end()) {
        bubblenum = it->second;
        it--;
      } else {
        it--;
        bubblenum = it->second + 1;
      }
      pair<int, int> p;
      p.first = word[i];
      p.second = bubblenum;
      map<int, int>::iterator it2 = bubblehist.insert(it, p);
      for (++it2; it2 != bubblehist.end(); it2++)
        (it2->second)++;
      facttime -= bubblenum;
      ans += factmodary[N - (i + 1)] * facttime;
      ans %= divisor;
    }
    cout << (int)ans << "\n";
  }
  delete[] word;
  delete[] factmodary;
  return 0;
}
accuracy: 1
time_ms: 1810
memory_kb: 8636
final_rank: 14

code_snippet: #include <iostream>
#include <map>
using namespace std;
const int divisor = 1000000007;
int main() {
  ios_base::sync_with_stdio(false);
  long long *factmodary = new long long[100001];
  long long fact = 1;
  for (int i = 1; i <= 100000; i++) {
    fact *= i;
    fact %= divisor;
    factmodary[i] = fact;
  }
  int *word = new int[100000]{0};
  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;
    for (int i = 0; i < N; i++)
      word[i] = i + 1;
    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      int tmp = word[s - 1];
      word[s - 1] = word[t - 1];
      word[t - 1] = tmp;
    }
    map<int, int> bubblehist;
    bubblehist[word[0]] = 0;
    long long ans = factmodary[N - 1] * (word[0] - 1);
    for (int i = 1; i < N; i++) {
      int facttime = word[i] - 1;
      int bubblenum;
      map<int, int>::iterator it = bubblehist.lower_bound(word[i]);
      if (it != bubblehist.end()) {
        bubblenum = it->second;
        it--;
      } else {
        it--;
        bubblenum = it->second + 1;
      }
      pair<int, int> p;
      p.first = word[i];
      p.second = bubblenum;
      map<int, int>::iterator it2 = bubblehist.insert(it, p);
      for (++it2; it2 != bubblehist.end(); it2++)
        (it2->second)++;
      facttime -= bubblenum;
      ans += factmodary[N - (i + 1)] * facttime;
      ans %= divisor;
    }
    cout << (int)ans << "\n";
  }
  delete[] word;
  delete[] factmodary;
  return 0;
}
accuracy: 1
time_ms: 1840
memory_kb: 8520
final_rank: 11

code_snippet: #include <iostream>
#include <map>
using namespace std;
const int divisor = 1000000007;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long *factmodary = new long long[100001];
  long long fact = 1;
  for (int i = 1; i <= 100000; i++) {
    fact *= i;
    fact %= divisor;
    factmodary[i] = fact;
  }
  int *word = new int[100000]{0};
  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;
    for (int i = 0; i < N; i++)
      word[i] = i + 1;
    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      int tmp = word[s - 1];
      word[s - 1] = word[t - 1];
      word[t - 1] = tmp;
    }
    map<int, int> bubblehist;
    bubblehist[word[0]] = 0;
    long long ans = factmodary[N - 1] * (word[0] - 1);
    for (int i = 1; i < N; i++) {
      int facttime = word[i] - 1;
      int bubblenum;
      map<int, int>::iterator it = bubblehist.lower_bound(word[i]);
      if (it != bubblehist.end()) {
        bubblenum = it->second;
        it--;
      } else {
        it--;
        bubblenum = it->second + 1;
      }
      pair<int, int> p;
      p.first = word[i];
      p.second = bubblenum;
      map<int, int>::iterator it2 = bubblehist.insert(it, p);
      for (++it2; it2 != bubblehist.end(); it2++)
        (it2->second)++;
      facttime -= bubblenum;
      ans += factmodary[N - (i + 1)] * facttime;
      ans %= divisor;
    }
    cout << (int)ans << "\n";
  }
  delete[] word;
  delete[] factmodary;
  return 0;
}
accuracy: 1
time_ms: 1820
memory_kb: 8528
final_rank: 9

code_snippet: #include <iostream>
#include <map>
using namespace std;
const int divisor = 1000000007;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long *factmodary = new long long[100001];
  long long fact = 1;
  for (int i = 1; i <= 100000; i++) {
    fact *= i;
    fact %= divisor;
    factmodary[i] = fact;
  }
  int *word = new int[100000]{0};
  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;
    for (int i = 0; i < N; i++)
      word[i] = i + 1;
    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      int tmp = word[s - 1];
      word[s - 1] = word[t - 1];
      word[t - 1] = tmp;
    }
    map<int, int> bubblehist;
    bubblehist[word[0]] = 0;
    long long ans = factmodary[N - 1] * (word[0] - 1);
    for (int i = 1; i < N; i++) {
      int facttime = word[i] - 1;
      int bubblenum;
      map<int, int>::iterator it = bubblehist.lower_bound(word[i]);
      if (it != bubblehist.end()) {
        bubblenum = it->second;
        it--;
      } else {
        it--;
        bubblenum = it->second + 1;
      }
      pair<int, int> p;
      p.first = word[i];
      p.second = bubblenum;
      map<int, int>::iterator it2 = bubblehist.insert(it, p);
      for (++it2; it2 != bubblehist.end(); it2++)
        (it2->second)++;
      facttime -= bubblenum;
      ans += factmodary[N - (i + 1)] * facttime;
      ans %= divisor;
    }
    cout << (int)ans << "\n";
  }
  delete[] word;
  delete[] factmodary;
  return 0;
}
accuracy: 1
time_ms: 1840
memory_kb: 8424
final_rank: 7

code_snippet: #include <iostream>
#include <map>
using namespace std;
const int divisor = 1000000007;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long *factmodary = new long long[100001];
  long long fact = 1;
  for (int i = 1; i <= 100000; i++) {
    fact *= i;
    fact %= divisor;
    factmodary[i] = fact;
  }
  int *word = new int[100000]{0};
  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;
    for (int i = 0; i < N; i++)
      word[i] = i + 1;
    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      int tmp = word[s - 1];
      word[s - 1] = word[t - 1];
      word[t - 1] = tmp;
    }
    map<int, int> bubblehist;
    bubblehist[word[0]] = 0;
    long long ans = factmodary[N - 1] * (word[0] - 1);
    for (int i = 1; i < N; i++) {
      int facttime = word[i] - 1;
      int bubblenum;
      map<int, int>::iterator it = bubblehist.lower_bound(word[i]);
      if (it != bubblehist.end()) {
        bubblenum = it->second;
        it--;
      } else {
        it--;
        bubblenum = it->second + 1;
      }
      pair<int, int> p;
      p.first = word[i];
      p.second = bubblenum;
      map<int, int>::iterator it2 = bubblehist.insert(it, p);
      for (++it2; it2 != bubblehist.end(); it2++)
        (it2->second)++;
      facttime -= bubblenum;
      ans += factmodary[N - (i + 1)] * facttime;
      ans %= divisor;
    }
    cout << (int)ans << "\n";
  }
  delete[] word;
  delete[] factmodary;
  return 0;
}
accuracy: 1
time_ms: 1870
memory_kb: 8636
final_rank: 16

code_snippet: #include <iostream>
#include <map>
using namespace std;
const int divisor = 1000000007;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long *factmodary = new long long[100001];
  long long fact = 1;
  for (int i = 1; i <= 100000; i++) {
    fact *= i;
    fact %= divisor;
    factmodary[i] = fact;
  }
  int *word = new int[100000]{0};
  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;
    for (int i = 0; i < N; i++)
      word[i] = i + 1;
    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      int tmp = word[s - 1];
      word[s - 1] = word[t - 1];
      word[t - 1] = tmp;
    }
    map<int, int> bubblehist;
    bubblehist[word[0]] = 0;
    long long ans = factmodary[N - 1] * (word[0] - 1);
    for (int i = 1; i < N; i++) {
      int facttime = word[i] - 1;
      int bubblenum;
      map<int, int>::iterator it = bubblehist.lower_bound(word[i]);
      if (it != bubblehist.end()) {
        bubblenum = it->second;
        it--;
      } else {
        it--;
        bubblenum = it->second + 1;
      }
      pair<int, int> p;
      p.first = word[i];
      p.second = bubblenum;
      map<int, int>::iterator it2 = bubblehist.insert(it, p);
      for (++it2; it2 != bubblehist.end(); it2++)
        (it2->second)++;
      facttime -= bubblenum;
      ans += factmodary[N - (i + 1)] * facttime;
      ans %= divisor;
    }
    cout << (int)ans << "\n";
  }
  delete[] word;
  delete[] factmodary;
  return 0;
}
accuracy: 1
time_ms: 1820
memory_kb: 8604
final_rank: 13

code_snippet: #include <iostream>
#include <map>
using namespace std;
const int divisor = 1000000007;
int main() {
  long long factmodary[100001];
  long long fact = 1;
  for (int i = 1; i <= 100000; i++) {
    fact *= i;
    fact %= divisor;
    factmodary[i] = fact;
  }
  int word[100000]{0};
  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;
    for (int i = 0; i < N; i++)
      word[i] = i + 1;
    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      int tmp = word[s - 1];
      word[s - 1] = word[t - 1];
      word[t - 1] = tmp;
    }
    map<int, int> bubblehist;
    bubblehist[word[0]] = 0;
    long long ans = factmodary[N - 1] * (word[0] - 1) % divisor;
    for (int i = 1; i < N; i++) {
      int facttime = word[i] - 1;
      auto it = bubblehist.lower_bound(word[i]);
      int bubblenum;
      if (it != bubblehist.end()) {
        bubblenum = it->second;
        --it;
      } else {
        --it;
        bubblenum = it->second + 1;
      }
      auto it2 = bubblehist.emplace_hint(it, word[i], bubblenum);
      for (++it2; it2 != bubblehist.end(); ++it2)
        ++(it2->second);
      facttime -= bubblenum;
      ans += factmodary[N - (i + 1)] * facttime % divisor;
      ans %= divisor;
    }
    cout << ans << "\n";
  }
  return 0;
}
accuracy: 1
time_ms: 1650
memory_kb: 8996
final_rank: 10

code_snippet: #include <iostream>
#include <map>
using namespace std;
const int divisor = 1000000007;
const int MAX_N = 100000;
long long factmodary[MAX_N + 1];
int word[MAX_N];
int main() {
  long long fact = 1;
  for (int i = 1; i <= MAX_N; i++) {
    fact *= i;
    fact %= divisor;
    factmodary[i] = fact;
  }
  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;
    for (int i = 0; i < N; i++)
      word[i] = i + 1;
    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      swap(word[s - 1], word[t - 1]);
    }
    map<int, int> bubblehist;
    bubblehist[word[0]] = 0;
    long long ans = factmodary[N - 1] * (word[0] - 1);
    for (int i = 1; i < N; i++) {
      int facttime = word[i] - 1;
      int bubblenum;
      auto it = bubblehist.lower_bound(word[i]);
      if (it != bubblehist.end()) {
        bubblenum = it->second;
        it--;
      } else {
        it--;
        bubblenum = it->second + 1;
      }
      pair<int, int> p(word[i], bubblenum);
      auto it2 = bubblehist.insert(it, p);
      for (++it2; it2 != bubblehist.end(); it2++)
        (it2->second)++;
      facttime -= bubblenum;
      ans += factmodary[N - (i + 1)] * facttime;
      ans %= divisor;
    }
    cout << (int)ans << "\n";
  }
  return 0;
}
accuracy: 1
time_ms: 1560
memory_kb: 9236
final_rank: 8

code_snippet: #include <iostream>
#include <map>
using namespace std;
const int divisor = 1000000007;
int main() {
  long long factmodary[100001];
  long long fact = 1;
  for (int i = 1; i <= 100000; i++) {
    fact = (fact * i) % divisor;
    factmodary[i] = fact;
  }
  int word[100000]{0};
  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;
    for (int i = 0; i < N; i++)
      word[i] = i + 1;
    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      swap(word[s - 1], word[t - 1]);
    }
    map<int, int> bubblehist;
    bubblehist[word[0]] = 0;
    long long ans = factmodary[N - 1] * (word[0] - 1);
    for (int i = 1; i < N; i++) {
      int facttime = word[i] - 1;
      auto it = bubblehist.lower_bound(word[i]);
      int bubblenum = it == bubblehist.end() ? (--it)->second + 1 : it->second;
      auto it2 = bubblehist.insert(it, {word[i], bubblenum});
      for (++it2; it2 != bubblehist.end(); it2++)
        (it2->second)++;
      facttime -= bubblenum;
      ans += factmodary[N - (i + 1)] * facttime % divisor;
      ans %= divisor;
    }
    cout << ans << "\n";
  }
  return 0;
}
accuracy: 1
time_ms: 1540
memory_kb: 9200
final_rank: 6

code_snippet: #include <iostream>
#include <map>
using namespace std;
const int divisor = 1000000007;
const int max_size = 100001; // store size in a variable
int main() {
  long long factmodary[max_size];
  long long fact = 1;
  for (int i = 1; i < max_size; i++) { // less than instead of less than or equal to
    fact *= i;
    fact %= divisor;
    factmodary[i] = fact;
  }
  int word[max_size] = {0};
  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;
    for (int i = 0; i < N; i++)
      word[i] = i + 1;
    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      int tmp = word[s - 1];
      word[s - 1] = word[t - 1];
      word[t - 1] = tmp;
    }
    map<int, int> bubblehist;
    bubblehist[word[0]] = 0;
    long long ans = factmodary[N - 1] * (word[0] - 1);
    for (int i = 1; i < N; i++) {
      int facttime = word[i] - 1;
      int bubblenum;
      auto it = bubblehist.lower_bound(word[i]); // use auto instead of map<int, int>::iterator
      if (it != bubblehist.end()) {
        bubblenum = it->second;
        it--;
      } else {
        it--;
        bubblenum = it->second + 1;
      }
      pair<int, int> p{word[i], bubblenum}; // use uniform initialization
      auto it2 = bubblehist.insert(it, p);
      for (++it2; it2 != bubblehist.end(); it2++)
        (it2->second)++;
      facttime -= bubblenum;
      ans += factmodary[N - (i + 1)] * facttime;
      ans %= divisor;
    }
    cout << static_cast<int>(ans) << "\n"; // use static_cast instead of (int)
  }
  return 0;
}
accuracy: 1
time_ms: 1460
memory_kb: 9184
final_rank: 2

code_snippet: #include <iostream>
#include <map>
using namespace std;
const int divisor = 1000000007;
long long factmodary[100001];
int word[100000];
int main() {
  long long fact = 1;
  for (int i = 1; i <= 100000; i++) {
    fact *= i;
    fact %= divisor;
    factmodary[i] = fact;
  }
  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;
    for (int i = 0; i < N; i++)
      word[i] = i + 1;
    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      int tmp = word[s - 1];
      word[s - 1] = word[t - 1];
      word[t - 1] = tmp;
    }
    map<int, int> bubblehist;
    bubblehist[word[0]] = 0;
    long long ans = factmodary[N - 1] * (word[0] - 1);
    for (int i = 1; i < N; i++) {
      int facttime = word[i] - 1;
      int bubblenum;
      auto it = bubblehist.lower_bound(word[i]);
      if (it != bubblehist.end()) {
        bubblenum = it->second;
        it--;
      } else {
        it--;
        bubblenum = it->second + 1;
      }
      pair<int, int> p;
      p.first = word[i];
      p.second = bubblenum;
      auto it2 = bubblehist.insert(it, p);
      for (++it2; it2 != bubblehist.end(); it2++)
        (it2->second)++;
      facttime -= bubblenum;
      ans += factmodary[N - (i + 1)] * facttime;
      ans %= divisor;
    }
    cout << (int)ans << "\n";
  }
  return 0;
}
accuracy: 1
time_ms: 1490
memory_kb: 9152
final_rank: 4

code_snippet: #include <iostream>
#include <map>
using namespace std;
const int divisor = 1000000007;
const int maxN = 100000;
long long factmodary[maxN + 1];
int word[maxN];
int main() {
  long long fact = 1;
  for (int i = 1; i <= maxN; i++) {
    fact *= i;
    fact %= divisor;
    factmodary[i] = fact;
  }
  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;
    for (int i = 0; i < N; i++)
      word[i] = i + 1;
    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      swap(word[s - 1], word[t - 1]);
    }
    map<int, int> bubblehist;
    bubblehist[word[0]] = 0;
    long long ans = factmodary[N - 1] * (word[0] - 1);
    for (int i = 1; i < N; i++) {
      int facttime = word[i] - 1;
      int bubblenum;
      auto it = bubblehist.lower_bound(word[i]);
      if (it != bubblehist.end()) {
        bubblenum = it->second;
        it--;
      } else {
        it--;
        bubblenum = it->second + 1;
      }
      auto [it2, success] = bubblehist.insert({word[i], bubblenum});
      it2++;
      for (; it2 != bubblehist.end(); it2++)
        (it2->second)++;
      facttime -= bubblenum;
      ans += factmodary[N - (i + 1)] * facttime;
      ans %= divisor;
    }
    cout << (int)ans << "\n";
  }
  return 0;
}
accuracy: 1
time_ms: 1790
memory_kb: 9232
final_rank: 18

code_snippet: #include <iostream>
#include <map>
using namespace std;
const int divisor = 1000000007;
int main() {
  long long factmodary[100001];
  long long fact = 1;
  for (int i = 1; i <= 100000; i++) {
    fact *= i;
    fact %= divisor;
    factmodary[i] = fact;
  }
  int word[100000]{0};
  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;
    for (int i = 0; i < N; i++)
      word[i] = i + 1;
    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      int tmp = word[s - 1];
      word[s - 1] = word[t - 1];
      word[t - 1] = tmp;
    }
    map<int, int> bubblehist;
    bubblehist[word[0]] = 0;
    long long ans = factmodary[N - 1] * (word[0] - 1);
    for (int i = 1; i < N; i++) {
      int facttime = word[i] - 1;
      int bubblenum;
      auto it = bubblehist.lower_bound(word[i]);
      if (it != bubblehist.end()) {
        bubblenum = it->second;
        it--;
      } else {
        it--;
        bubblenum = it->second + 1;
      }
      auto it2 = bubblehist.emplace_hint(it, word[i], bubblenum);
      for (++it2; it2 != bubblehist.end(); it2++)
        (it2->second)++;
      facttime -= bubblenum;
      ans += factmodary[N - (i + 1)] * facttime;
      ans %= divisor;
    }
    cout << ans << "\n";
  }
  return 0;
}
accuracy: 1
time_ms: 1480
memory_kb: 9300
final_rank: 5

code_snippet: #include <iostream>
#include <map>
using namespace std;
const int divisor = 1000000007;
const int MAXN = 100000;
long long factmodary[MAXN+1]; // Use a constant instead of a variable for the size
int word[MAXN];
int main() {
  long long fact = 1;
  for (int i = 1; i <= MAXN; i++) {
    fact = (fact * i) % divisor; // Combine multiplication and modulo operation
    factmodary[i] = fact;
  }
  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;
    for (int i = 0; i < N; i++)
      word[i] = i + 1;
    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      swap(word[s - 1], word[t - 1]); // Use swap instead of temporary variable
    }
    map<int, int> bubblehist;
    bubblehist[word[0]] = 0;
    long long ans = factmodary[N - 1] * (word[0] - 1);
    for (int i = 1; i < N; i++) {
      int facttime = word[i] - 1, bubblenum;
      auto it = bubblehist.lower_bound(word[i]);
      if (it != bubblehist.end()) {
        bubblenum = it->second;
        it--;
      } else {
        it--;
        bubblenum = it->second + 1;
      }
      auto [it2, inserted] = bubblehist.insert({word[i], bubblenum});
      for (++it2; it2 != bubblehist.end(); it2++)
        (it2->second)++;
      facttime -= bubblenum;
      ans = (ans + factmodary[N - (i + 1)] * facttime) % divisor; // Combine addition and modulo operation
    }
    cout << ans << "\n";
  }
  return 0;
}
accuracy: 1
time_ms: 1740
memory_kb: 9300
final_rank: 17

code_snippet: #include <iostream>
#include <map>
using namespace std;
const int divisor = 1000000007;
const int maxn = 100001;
long long factmodary[maxn];
void precompute() {
  long long fact = 1;
  for (int i = 1; i <= maxn-1; i++) {
    fact *= i;
    fact %= divisor;
    factmodary[i] = fact;
  }
}
int main() {
  precompute();
  int word[maxn] = {0};
  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;
    for (int i = 0; i < N; i++)
      word[i] = i + 1;
    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      int tmp = word[s - 1];
      word[s - 1] = word[t - 1];
      word[t - 1] = tmp;
    }
    map<int, int> bubblehist;
    bubblehist[word[0]] = 0;
    long long ans = factmodary[N - 1] * (word[0] - 1);
    for (int i = 1; i < N; i++) {
      int facttime = word[i] - 1;
      int bubblenum;
      auto it = bubblehist.lower_bound(word[i]);
      if (it != bubblehist.end()) {
        bubblenum = it->second;
        it--;
      } else {
        it--;
        bubblenum = it->second + 1;
      }
      pair<int, int> p;
      p.first = word[i];
      p.second = bubblenum;
      auto it2 = bubblehist.insert(it, p);
      for (++it2; it2 != bubblehist.end(); it2++)
        (it2->second)++;
      facttime -= bubblenum;
      ans += factmodary[N - (i + 1)] * facttime;
      ans %= divisor;
    }
    cout << ans << "\n";
  }
  return 0;
}
accuracy: 1
time_ms: 1470
memory_kb: 9204
final_rank: 3