#include <bits/stdc++.h>
using namespace std;
long long Get() {
  long long ret = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    ret = (ret << 1) + (ret << 3) + c - '0';
    c = getchar();
  }
  return ret * f;
}
const long long mod = 998244353, N = 1e6 + 5;
long long n, k;
long long ans1 = 0, ans2 = 0;
long long inv[N], fact[N];
long long Pow(long long a, long long b) {
  long long ans = 1, base = a;
  while (b) {
    if (b & 1) ans = ans * base % mod;
    base = base * base % mod;
    b >>= 1;
  }
  return ans;
}
void Init() {
  fact[0] = 1;
  for (long long i = 1; i <= n; i++) fact[i] = fact[i - 1] * i % mod;
  inv[n] = Pow(fact[n], mod - 2);
  for (long long i = n - 1; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1) % mod;
}
inline long long C(long long x, long long y) {
  return ((fact[x] * inv[y]) % mod * inv[x - y]) % mod;
}
signed main() {
  n = Get();
  k = 3;
  Init();
  for (long long i = 1; i <= n; i++) {
    ans1 = (((Pow(k, i) * C(n, i)) % mod * Pow(k, n * (n - i))) % mod *
                Pow(-1, i + 1) % mod +
            ans1) %
           mod;
  }
  for (long long i = 1; i <= n; i++) {
    long long q = Pow(k, n - i);
    ans2 = (ans2 +
            (((Pow(-1, i + 1) * C(n, i)) % mod * k % mod) % mod *
             (Pow(q - 1, n) - Pow(q, n) + mod) % mod) %
                mod +
            mod) %
           mod;
  }
  cout << (ans1 * 2 % mod + ans2 + mod) % mod << "\n";
}
