#include <bits/stdc++.h>
using namespace std;
const int N_MAX = 1e6 + 5;
const int MOD = 998244353;
long long N;
long long factorial[N_MAX], inv_factorial[N_MAX];
long long mod_pow(long long a, long long b) {
  long long result = 1;
  while (b > 0) {
    if (b & 1) result = result * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return result;
}
long long mod_inv(long long a, long long m = MOD) {
  return a == 1 ? 1 : m - mod_inv(m % a, a) * m / a;
}
long long choose(long long n, long long r) {
  return factorial[n] * inv_factorial[r] % MOD * inv_factorial[n - r] % MOD;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N;
  factorial[0] = inv_factorial[0] = 1;
  for (int i = 1; i <= N; i++) {
    factorial[i] = i * factorial[i - 1] % MOD;
    inv_factorial[i] = mod_inv(i) * inv_factorial[i - 1] % MOD;
  }
  long long total = 0;
  for (int x = 1; x <= N; x++)
    total = (total + 2 * (x % 2 == 1 ? +1 : -1) * mod_pow(3, x) * choose(N, x) %
                         MOD * mod_pow(3, N * (N - x))) %
            MOD;
  for (int rows = 1; rows <= N; rows++) {
    long long value = mod_pow(mod_pow(3, N - rows) - 1, N);
    value = (value - mod_pow(3, N * (N - rows))) % MOD;
    value = (value * 3 * choose(N, rows)) % MOD;
    total = (total + (rows % 2 == 1 ? +1 : -1) * value) % MOD;
  }
  total = (total % MOD + MOD) % MOD;
  cout << total << '\n';
  return 0;
}
