#include <bits/stdc++.h>
using namespace std;
const long long FACT_MAX = 1000000;
long long MOD = 998244353;
long long famod[FACT_MAX + 1];
long long simod[FACT_MAX + 1];
long long fimod[FACT_MAX + 1];
void create_fact_mod(int num) {
  famod[0] = 1;
  famod[1] = 1;
  long long i;
  for (i = 2; i <= num; i++) {
    famod[i] = famod[i - 1] * i % MOD;
  }
}
void create_seq_inv_mod(int num) {
  simod[0] = 1;
  simod[1] = 1;
  long long i;
  for (i = 2; i <= num; i++) {
    simod[i] = (MOD - MOD / i) * simod[MOD % i] % MOD;
  }
}
void create_fact_inv_mod(int num) {
  fimod[0] = 1;
  fimod[1] = 1;
  long long i;
  for (i = 2; i <= num; i++) {
    fimod[i] = fimod[i - 1] * simod[i] % MOD;
  }
}
void create_mod_tables(int num) {
  create_fact_mod(num);
  create_seq_inv_mod(num);
  create_fact_inv_mod(num);
}
long long comb_mod(int n, int k) {
  return famod[n] * fimod[n - k] % MOD * fimod[k] % MOD;
}
long long perm_mod(int n, int k) { return famod[n] * fimod[n - k] % MOD; }
long long power_mod(long long num, long long power) {
  long long prod = 1;
  num %= MOD;
  while (power > 0) {
    if ((power & 1) == 1) {
      prod = prod * num % MOD;
    }
    num = num * num % MOD;
    power >>= 1;
  }
  return prod;
}
int main() {
  long long i, j, k;
  long long N;
  cin >> N;
  create_mod_tables(N);
  long long ans = 0;
  for (i = 1; i <= N; i++) {
    long long p3 = power_mod(3, i + N * (N - i));
    long long result = comb_mod(N, i) * p3 % MOD;
    ans = (ans + result * (i % 2 == 0 ? -1 : 1) + MOD) % MOD;
  }
  ans *= 2;
  for (i = 0; i < N; i++) {
    long long p3 = power_mod(3, i);
    long long result =
        (power_mod(MOD + 1 - p3, N) - power_mod(MOD - p3, N) + MOD) % MOD;
    result = result * comb_mod(N, i) * 3 % MOD;
    ans = (ans + result * (i % 2 == 0 ? -1 : 1) + MOD) % MOD;
  }
  cout << ans << endl;
  return 0;
}
